ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
atomic.h File Reference

Go to the source code of this file.

Macros

#define SHOULD_EXIT()
#define CLIENT_SHOULD_EXIT()
#define ATOMIC_LOAD_BOOL(ptr)
#define ATOMIC_LOAD_UINT32(ptr)
#define ATOMIC_LOAD_UINT64(ptr)
#define ATOMIC_STORE_BOOL(ptr, value)
#define ATOMIC_STORE_UINT32(ptr, value)
#define ATOMIC_STORE_UINT64(ptr, value)
#define ATOMIC_CAS_BOOL(ptr, expected, new_value)
#define ATOMIC_CAS_UINT32(ptr, expected, new_value)
#define ATOMIC_ADD_UINT32(ptr, delta)
#define ATOMIC_SUB_UINT32(ptr, delta)

Macro Definition Documentation

◆ ATOMIC_ADD_UINT32

#define ATOMIC_ADD_UINT32 ( ptr,
delta )
Value:
atomic_fetch_add((atomic_uint32_t *)(ptr), (delta))

Atomically add to an unsigned 32-bit value.

Parameters
ptrPointer to atomic_uint32_t variable
deltaValue to add
Returns
Previous value of the atomic variable

Usage:

uint32_t previous = ATOMIC_ADD_UINT32(&g_frame_counter, 1);
log_debug("Frame count before: %u", previous);
unsigned int uint32_t
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565
#define ATOMIC_ADD_UINT32(ptr, delta)

Definition at line 216 of file util/atomic.h.

216#define ATOMIC_ADD_UINT32(ptr, delta) \
217 atomic_fetch_add((atomic_uint32_t *)(ptr), (delta))

◆ ATOMIC_CAS_BOOL

#define ATOMIC_CAS_BOOL ( ptr,
expected,
new_value )
Value:
atomic_compare_exchange_strong((atomic_t *)(ptr), &(expected), (new_value))
Atomic value wrapper for integral/boolean types.
Definition atomic.h:80

Compare and swap operation for boolean atomics. Atomically compares the value at ptr with expected, and if equal, stores new_value and returns true. Otherwise returns false.

Parameters
ptrPointer to atomic_t variable
expectedExpected value
new_valueNew value to store if comparison succeeds
Returns
true if swap succeeded (value was equal to expected), false otherwise

Usage:

// Try to set client as active (was inactive)
if (ATOMIC_CAS_BOOL(&client->active, false, true)) {
log_debug("Client activated");
} else {
log_debug("Client already active");
}
#define ATOMIC_CAS_BOOL(ptr, expected, new_value)

Definition at line 180 of file util/atomic.h.

180#define ATOMIC_CAS_BOOL(ptr, expected, new_value) \
181 atomic_compare_exchange_strong((atomic_t *)(ptr), &(expected), (new_value))

◆ ATOMIC_CAS_UINT32

#define ATOMIC_CAS_UINT32 ( ptr,
expected,
new_value )
Value:
atomic_compare_exchange_strong((atomic_uint32_t *)(ptr), (expected), \
(new_value))

Compare and swap operation for unsigned 32-bit atomics.

Parameters
ptrPointer to atomic_uint32_t variable
expectedExpected value (passed by reference)
new_valueNew value to store if comparison succeeds
Returns
true if swap succeeded, false otherwise

Usage:

uint32_t expected = current_id;
if (ATOMIC_CAS_UINT32(&g_next_client_id, &expected, current_id + 1)) {
log_debug("Claimed client ID %u", expected);
}
#define ATOMIC_CAS_UINT32(ptr, expected, new_value)

Definition at line 199 of file util/atomic.h.

199#define ATOMIC_CAS_UINT32(ptr, expected, new_value) \
200 atomic_compare_exchange_strong((atomic_uint32_t *)(ptr), (expected), \
201 (new_value))

◆ ATOMIC_LOAD_BOOL

#define ATOMIC_LOAD_BOOL ( ptr)
Value:
atomic_load((atomic_t *)(ptr))

Safely load a boolean atomic value.

Parameters
ptrPointer to atomic_t variable
Returns
Current value of the atomic variable

Usage:

bool is_active = ATOMIC_LOAD_BOOL(&client->active);
#define ATOMIC_LOAD_BOOL(ptr)
Definition util/atomic.h:91

Definition at line 91 of file util/atomic.h.

◆ ATOMIC_LOAD_UINT32

#define ATOMIC_LOAD_UINT32 ( ptr)
Value:
atomic_load((atomic_uint32_t *)(ptr))

Safely load an unsigned 32-bit atomic value.

Parameters
ptrPointer to atomic_uint32_t variable
Returns
Current value of the atomic variable

Usage:

uint32_t client_id = ATOMIC_LOAD_UINT32(&client->client_id);
#define ATOMIC_LOAD_UINT32(ptr)

Definition at line 104 of file util/atomic.h.

◆ ATOMIC_LOAD_UINT64

#define ATOMIC_LOAD_UINT64 ( ptr)
Value:
atomic_load((atomic_uint64_t *)(ptr))

Safely load an unsigned 64-bit atomic value.

Parameters
ptrPointer to atomic_uint64_t variable
Returns
Current value of the atomic variable

Usage:

uint64_t timestamp = ATOMIC_LOAD_UINT64(&client->last_activity);
unsigned long long uint64_t
#define ATOMIC_LOAD_UINT64(ptr)

Definition at line 117 of file util/atomic.h.

◆ ATOMIC_STORE_BOOL

#define ATOMIC_STORE_BOOL ( ptr,
value )
Value:
atomic_store((atomic_t *)(ptr), (value))

Safely store a boolean atomic value.

Parameters
ptrPointer to atomic_t variable
valueValue to store

Usage:

ATOMIC_STORE_BOOL(&client->active, true);
#define ATOMIC_STORE_BOOL(ptr, value)

Definition at line 130 of file util/atomic.h.

◆ ATOMIC_STORE_UINT32

#define ATOMIC_STORE_UINT32 ( ptr,
value )
Value:
atomic_store((atomic_uint32_t *)(ptr), (value))

Safely store an unsigned 32-bit atomic value.

Parameters
ptrPointer to atomic_uint32_t variable
valueValue to store

Usage:

ATOMIC_STORE_UINT32(&client->client_id, 42);
#define ATOMIC_STORE_UINT32(ptr, value)

Definition at line 143 of file util/atomic.h.

143#define ATOMIC_STORE_UINT32(ptr, value) \
144 atomic_store((atomic_uint32_t *)(ptr), (value))

◆ ATOMIC_STORE_UINT64

#define ATOMIC_STORE_UINT64 ( ptr,
value )
Value:
atomic_store((atomic_uint64_t *)(ptr), (value))

Safely store an unsigned 64-bit atomic value.

Parameters
ptrPointer to atomic_uint64_t variable
valueValue to store

Usage:

ATOMIC_STORE_UINT64(&client->last_activity, get_time_ms());
#define ATOMIC_STORE_UINT64(ptr, value)

Definition at line 157 of file util/atomic.h.

157#define ATOMIC_STORE_UINT64(ptr, value) \
158 atomic_store((atomic_uint64_t *)(ptr), (value))

◆ ATOMIC_SUB_UINT32

#define ATOMIC_SUB_UINT32 ( ptr,
delta )
Value:
atomic_fetch_sub((atomic_uint32_t *)(ptr), (delta))

Atomically subtract from an unsigned 32-bit value.

Parameters
ptrPointer to atomic_uint32_t variable
deltaValue to subtract
Returns
Previous value of the atomic variable

Usage:

uint32_t previous = ATOMIC_SUB_UINT32(&g_active_clients, 1);
log_debug("Active clients before: %u", previous);
#define ATOMIC_SUB_UINT32(ptr, delta)

Definition at line 232 of file util/atomic.h.

232#define ATOMIC_SUB_UINT32(ptr, delta) \
233 atomic_fetch_sub((atomic_uint32_t *)(ptr), (delta))

◆ CLIENT_SHOULD_EXIT

#define CLIENT_SHOULD_EXIT ( )
Value:
(atomic_load(&g_should_exit))
atomic_t g_should_exit
Global application exit flag (shared across all modes).
Definition globals.c:32

Check if the global client should exit. Requires g_should_exit to be declared as atomic_t in the compilation unit.

Returns
true if shutdown is requested, false otherwise

Usage:

while (!CLIENT_SHOULD_EXIT()) {
process_client_event();
}
#define CLIENT_SHOULD_EXIT()
Definition util/atomic.h:78

Definition at line 78 of file util/atomic.h.

◆ SHOULD_EXIT

#define SHOULD_EXIT ( )
Value:
(atomic_load(&g_should_exit))

Check if the global server should exit. Requires g_should_exit to be declared as atomic_t in the compilation unit.

Returns
true if shutdown is requested, false otherwise

Usage:

while (!SHOULD_EXIT()) {
process_event();
}
#define SHOULD_EXIT()
Definition util/atomic.h:61
Note
This assumes g_should_exit is declared in the same file or extern declared.

Definition at line 61 of file util/atomic.h.