ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
atomic.h File Reference

Go to the source code of this file.

Macros

#define SHOULD_EXIT()   (atomic_load(&g_server_should_exit))
 
#define CLIENT_SHOULD_EXIT()   (atomic_load(&g_should_exit))
 
#define ATOMIC_LOAD_BOOL(ptr)   atomic_load((atomic_bool *)(ptr))
 
#define ATOMIC_LOAD_UINT32(ptr)   atomic_load((atomic_uint32_t *)(ptr))
 
#define ATOMIC_LOAD_UINT64(ptr)   atomic_load((atomic_uint64_t *)(ptr))
 
#define ATOMIC_STORE_BOOL(ptr, value)   atomic_store((atomic_bool *)(ptr), (value))
 
#define ATOMIC_STORE_UINT32(ptr, value)   atomic_store((atomic_uint32_t *)(ptr), (value))
 
#define ATOMIC_STORE_UINT64(ptr, value)   atomic_store((atomic_uint64_t *)(ptr), (value))
 
#define ATOMIC_CAS_BOOL(ptr, expected, new_value)    atomic_compare_exchange_strong((atomic_bool *)(ptr), &(expected), (new_value))
 
#define ATOMIC_CAS_UINT32(ptr, expected, new_value)    atomic_compare_exchange_strong((atomic_uint32_t *)(ptr), (expected), (new_value))
 
#define ATOMIC_ADD_UINT32(ptr, delta)   atomic_fetch_add((atomic_uint32_t *)(ptr), (delta))
 
#define ATOMIC_SUB_UINT32(ptr, delta)   atomic_fetch_sub((atomic_uint32_t *)(ptr), (delta))
 

Macro Definition Documentation

◆ ATOMIC_ADD_UINT32

#define ATOMIC_ADD_UINT32 (   ptr,
  delta 
)    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);
#define ATOMIC_ADD_UINT32(ptr, delta)
Definition atomic.h:216
unsigned int uint32_t
Definition common.h:58
#define log_debug(...)
Log a DEBUG message.

Definition at line 216 of file atomic.h.

◆ ATOMIC_CAS_BOOL

#define ATOMIC_CAS_BOOL (   ptr,
  expected,
  new_value 
)     atomic_compare_exchange_strong((atomic_bool *)(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_bool 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 atomic.h:181

Definition at line 181 of file atomic.h.

◆ ATOMIC_CAS_UINT32

#define ATOMIC_CAS_UINT32 (   ptr,
  expected,
  new_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 atomic.h:200

Definition at line 200 of file atomic.h.

◆ ATOMIC_LOAD_BOOL

#define ATOMIC_LOAD_BOOL (   ptr)    atomic_load((atomic_bool *)(ptr))

Safely load a boolean atomic value.

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

Usage:

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

Definition at line 94 of file atomic.h.

◆ ATOMIC_LOAD_UINT32

#define ATOMIC_LOAD_UINT32 (   ptr)    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 atomic.h:107

Definition at line 107 of file atomic.h.

◆ ATOMIC_LOAD_UINT64

#define ATOMIC_LOAD_UINT64 (   ptr)    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);
#define ATOMIC_LOAD_UINT64(ptr)
Definition atomic.h:120
unsigned long long uint64_t
Definition common.h:59

Definition at line 120 of file atomic.h.

◆ ATOMIC_STORE_BOOL

#define ATOMIC_STORE_BOOL (   ptr,
  value 
)    atomic_store((atomic_bool *)(ptr), (value))

Safely store a boolean atomic value.

Parameters
ptrPointer to atomic_bool variable
valueValue to store

Usage:

ATOMIC_STORE_BOOL(&client->active, true);
#define ATOMIC_STORE_BOOL(ptr, value)
Definition atomic.h:133

Definition at line 133 of file atomic.h.

◆ ATOMIC_STORE_UINT32

#define ATOMIC_STORE_UINT32 (   ptr,
  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 atomic.h:146

Definition at line 146 of file atomic.h.

◆ ATOMIC_STORE_UINT64

#define ATOMIC_STORE_UINT64 (   ptr,
  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 atomic.h:159

Definition at line 159 of file atomic.h.

◆ ATOMIC_SUB_UINT32

#define ATOMIC_SUB_UINT32 (   ptr,
  delta 
)    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 atomic.h:231

Definition at line 231 of file atomic.h.

◆ CLIENT_SHOULD_EXIT

#define CLIENT_SHOULD_EXIT ( )    (atomic_load(&g_should_exit))

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

Returns
true if shutdown is requested, false otherwise

Usage:

extern atomic_bool g_should_exit;
while (!CLIENT_SHOULD_EXIT()) {
process_client_event();
}
#define CLIENT_SHOULD_EXIT()
Definition atomic.h:81
atomic_bool g_should_exit
Definition globals.c:34

Definition at line 81 of file atomic.h.

◆ SHOULD_EXIT

#define SHOULD_EXIT ( )    (atomic_load(&g_server_should_exit))

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

Returns
true if shutdown is requested, false otherwise

Usage:

extern atomic_bool g_server_should_exit;
while (!SHOULD_EXIT()) {
process_event();
}
#define SHOULD_EXIT()
Definition atomic.h:64
atomic_bool g_server_should_exit
Global shutdown flag from main.c.
Note
This assumes g_server_should_exit is declared in the same file or extern declared.

Definition at line 64 of file atomic.h.