ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
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
Definition common.h:62
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:595
#define ATOMIC_ADD_UINT32(ptr, delta)

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

◆ ATOMIC_CAS_BOOL

#define ATOMIC_CAS_BOOL ( ptr,
expected,
new_value )
Value:
atomic_compare_exchange_strong((atomic_t *)(ptr), &(expected), (new_value))

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 173 of file util/atomic.h.

173#define ATOMIC_CAS_BOOL(ptr, expected, new_value) \
174 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 192 of file util/atomic.h.

192#define ATOMIC_CAS_UINT32(ptr, expected, new_value) \
193 atomic_compare_exchange_strong((atomic_uint32_t *)(ptr), (expected), (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:86

Definition at line 86 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 util/atomic.h:99

Definition at line 99 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
Definition common.h:63
#define ATOMIC_LOAD_UINT64(ptr)

Definition at line 112 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 125 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 138 of file util/atomic.h.

◆ 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 151 of file util/atomic.h.

◆ 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 223 of file util/atomic.h.

◆ 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:34

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:

extern atomic_t g_should_exit;
while (!CLIENT_SHOULD_EXIT()) {
process_client_event();
}
#define CLIENT_SHOULD_EXIT()
Definition util/atomic.h:73

Definition at line 73 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:

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

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