ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
atomic.h File Reference

⚛️ Atomic operations abstraction layer with debug tracking More...

Go to the source code of this file.

Data Structures

struct  atomic_t
 Atomic value wrapper for integral/boolean types. More...
 
struct  atomic_ptr_t
 Atomic pointer wrapper. More...
 

Macros

#define ATOMIC_REGISTER_AUTO(name, parent_ptr)   NAMED_REGISTER_ATOMIC(&(name), #name, (uintptr_t)(const void *)(parent_ptr))
 Register an atomic variable for debug tracking with automatic name generation.
 
#define ATOMIC_PTR_REGISTER_AUTO(name)   NAMED_REGISTER_ATOMIC_PTR(&(name), #name)
 Register an atomic pointer variable for debug tracking with automatic name generation.
 
#define ATOMIC_INIT_AUTO(...)   = _atomic_init_auto(__VA_ARGS__)
 Initialize an atomic_t with automatic named registration.
 
#define ATOMIC_PTR_INIT_AUTO(...)   = _atomic_ptr_init_auto(__VA_ARGS__)
 Initialize an atomic_ptr_t with automatic named registration.
 
#define _atomic_init_auto(name)
 
#define _atomic_ptr_init_auto(name)
 

Functions

bool atomic_load_bool (atomic_t *a)
 Atomically load a boolean value.
 
bool atomic_load_bool_impl (const atomic_t *a)
 
void atomic_store_bool (atomic_t *a, bool value)
 Atomically store a boolean value.
 
void atomic_store_bool_impl (atomic_t *a, uint64_t value)
 
bool atomic_cas_bool (atomic_t *a, bool *expected, bool new_value)
 Atomically compare-and-swap a boolean.
 
bool atomic_cas_bool_impl (atomic_t *a, bool *expected, bool new_value)
 
bool atomic_exchange_bool (atomic_t *a, bool new_value)
 Atomically exchange a boolean and return the old value.
 
bool atomic_exchange_bool_impl (atomic_t *a, bool new_value)
 
int atomic_load_int (atomic_t *a)
 Atomically load an int value.
 
int atomic_load_int_impl (const atomic_t *a)
 
void atomic_store_int (atomic_t *a, int value)
 Atomically store an int value.
 
void atomic_store_int_impl (atomic_t *a, uint64_t value)
 
int atomic_fetch_add_int (atomic_t *a, int delta)
 Atomically add to an int and return the previous value.
 
int atomic_fetch_add_int_impl (atomic_t *a, int64_t delta)
 
int atomic_fetch_sub_int (atomic_t *a, int delta)
 Atomically subtract from an int and return the previous value.
 
int atomic_fetch_sub_int_impl (atomic_t *a, int64_t delta)
 
bool atomic_cas_int (atomic_t *a, int *expected, int new_value)
 Atomically compare-and-swap an int.
 
bool atomic_cas_int_impl (atomic_t *a, int *expected, int new_value)
 
uint64_t atomic_load_u64 (atomic_t *a)
 Atomically load a uint64_t value.
 
uint64_t atomic_load_u64_impl (const atomic_t *a)
 
void atomic_store_u64 (atomic_t *a, uint64_t value)
 Atomically store a uint64_t value.
 
void atomic_store_u64_impl (atomic_t *a, uint64_t value)
 
uint64_t atomic_fetch_add_u64 (atomic_t *a, uint64_t delta)
 Atomically add to a uint64_t and return the previous value.
 
uint64_t atomic_fetch_add_u64_impl (atomic_t *a, uint64_t delta)
 
uint64_t atomic_fetch_sub_u64 (atomic_t *a, uint64_t delta)
 Atomically subtract from a uint64_t and return the previous value.
 
uint64_t atomic_fetch_sub_u64_impl (atomic_t *a, uint64_t delta)
 
bool atomic_cas_u64 (atomic_t *a, uint64_t *expected, uint64_t new_value)
 Atomically compare-and-swap a uint64_t.
 
bool atomic_cas_u64_impl (atomic_t *a, uint64_t *expected, uint64_t new_value)
 
uint64_t atomic_exchange_u64 (atomic_t *a, uint64_t new_value)
 Atomically exchange a uint64_t and return the old value.
 
uint64_t atomic_exchange_u64_impl (atomic_t *a, uint64_t new_value)
 
void * atomic_ptr_load (atomic_ptr_t *a)
 Atomically load a pointer.
 
void * atomic_ptr_load_impl (const atomic_ptr_t *a)
 
void atomic_ptr_store (atomic_ptr_t *a, void *value)
 Atomically store a pointer.
 
void atomic_ptr_store_impl (atomic_ptr_t *a, void *value)
 
bool atomic_ptr_cas (atomic_ptr_t *a, void **expected, void *new_value)
 Atomically compare-and-swap a pointer.
 
bool atomic_ptr_cas_impl (atomic_ptr_t *a, void **expected, void *new_value)
 
void * atomic_ptr_exchange (atomic_ptr_t *a, void *new_value)
 Atomically exchange a pointer and return the old value.
 
void * atomic_ptr_exchange_impl (atomic_ptr_t *a, void *new_value)
 
int atomic_exchange_int (atomic_t *a, int new_value)
 Atomically exchange an int and return the old value.
 
int atomic_exchange_int_impl (atomic_t *a, int new_value)
 
void atomic_on_load (atomic_t *a)
 
void atomic_on_store (atomic_t *a)
 
void atomic_on_cas (atomic_t *a, bool success)
 
void atomic_on_fetch (atomic_t *a)
 
void atomic_ptr_on_load (atomic_ptr_t *a)
 
void atomic_ptr_on_store (atomic_ptr_t *a)
 
void atomic_ptr_on_cas (atomic_ptr_t *a, bool success)
 
void atomic_ptr_on_exchange (atomic_ptr_t *a)
 
void debug_atomic_init (void)
 
void debug_atomic_shutdown (void)
 
bool debug_atomic_is_initialized (void)
 
void debug_atomic_print_state (void)
 

Detailed Description

⚛️ Atomic operations abstraction layer with debug tracking

Date
March 2026

Provides a platform-independent wrapper for atomic operations similar to mutex_t, rwlock_t, and cond_t abstractions. Supports named registration, debug tracking, and integration with –sync-state output.

Design principles:

  • No recursion (atomics never depend on other atomics)
  • No mutexes in implementation (uses raw C11 atomics internally)
  • Named registration via named.c (NAMED_REGISTER_ATOMIC macro)
  • Debug-only timing and count tracking
  • Exchange operations for atomic min/max patterns (avoids TOCTOU races)

Supported types via typed macros:

  • bool: atomic_load_bool, atomic_store_bool, atomic_cas_bool, atomic_exchange_bool
  • int: atomic_load_int, atomic_store_int, atomic_fetch_add_int, atomic_fetch_sub_int
  • uint64_t: atomic_load_u64, atomic_store_u64, atomic_fetch_add_u64, atomic_fetch_sub_u64, atomic_exchange_u64
  • Pointers: atomic_ptr_load, atomic_ptr_store, atomic_ptr_cas, atomic_ptr_exchange

Usage:

// Declare a named atomic
atomic_t g_should_exit = ATOMIC_INIT(false, "g_should_exit");
// Load/store operations
// In release builds: zero overhead, calls _impl functions directly
// In debug builds: includes registration and timing hooks
void atomic_store_bool(atomic_t *a, bool value)
Atomically store a boolean value.
Definition atomic.c:177
bool atomic_load_bool(atomic_t *a)
Atomically load a boolean value.
Definition atomic.c:169
atomic_t g_should_exit
Global application exit flag (shared across all modes)
Definition globals.c:34
Atomic value wrapper for integral/boolean types.
Definition atomic.h:76

Definition in file atomic.h.

Macro Definition Documentation

◆ _atomic_init_auto

#define _atomic_init_auto (   name)
Value:
{0}; \
static void __attribute__((constructor, used)) _atomic_reg_##name(void) { \
NAMED_REGISTER_ATOMIC(&name, #name, NULL); \
} typedef int _atomic_assert_##name
Network quality metrics for a single participant.
Definition metrics.h:33

Definition at line 528 of file atomic.h.

528 {0}; \
529 static void __attribute__((constructor, used)) _atomic_reg_##name(void) { \
530 NAMED_REGISTER_ATOMIC(&name, #name, NULL); \
531 } typedef int _atomic_assert_##name

◆ _atomic_ptr_init_auto

#define _atomic_ptr_init_auto (   name)
Value:
{0}; \
static void __attribute__((constructor, used)) _atomic_ptr_reg_##name(void) { \
NAMED_REGISTER_ATOMIC_PTR(&name, #name, NULL); \
} typedef int _atomic_ptr_assert_##name

Definition at line 533 of file atomic.h.

533 {0}; \
534 static void __attribute__((constructor, used)) _atomic_ptr_reg_##name(void) { \
535 NAMED_REGISTER_ATOMIC_PTR(&name, #name, NULL); \
536 } typedef int _atomic_ptr_assert_##name

◆ ATOMIC_INIT_AUTO

#define ATOMIC_INIT_AUTO (   ...)    = _atomic_init_auto(__VA_ARGS__)

Initialize an atomic_t with automatic named registration.

In debug builds, automatically registers the atomic with named.c using a constructor function. In release builds, just initializes to {0}.

Usage:

// Declare with automatic registration:
static atomic_t g_counter ATOMIC_INIT_AUTO("g_counter");
// Or simpler - use the variable name directly:
static atomic_t g_counter ATOMIC_INIT_AUTO();
#define ATOMIC_INIT_AUTO(...)
Initialize an atomic_t with automatic named registration.
Definition atomic.h:510

Note: Currently requires GCC/Clang constructor attribute. For other compilers, use ATOMIC_REGISTER_AUTO(variable_name) separately.

Definition at line 510 of file atomic.h.

◆ ATOMIC_PTR_INIT_AUTO

#define ATOMIC_PTR_INIT_AUTO (   ...)    = _atomic_ptr_init_auto(__VA_ARGS__)

Initialize an atomic_ptr_t with automatic named registration.

Same as ATOMIC_INIT_AUTO but for atomic_ptr_t.

Definition at line 521 of file atomic.h.

◆ ATOMIC_PTR_REGISTER_AUTO

#define ATOMIC_PTR_REGISTER_AUTO (   name)    NAMED_REGISTER_ATOMIC_PTR(&(name), #name)

Register an atomic pointer variable for debug tracking with automatic name generation.

Same as ATOMIC_REGISTER_AUTO but for _Atomic(void *) variables.

Usage:

_Atomic(void *) g_buffer = {0};
#define ATOMIC_PTR_REGISTER_AUTO(name)
Register an atomic pointer variable for debug tracking with automatic name generation.
Definition atomic.h:485

Definition at line 485 of file atomic.h.

◆ ATOMIC_REGISTER_AUTO

#define ATOMIC_REGISTER_AUTO (   name,
  parent_ptr 
)    NAMED_REGISTER_ATOMIC(&(name), #name, (uintptr_t)(const void *)(parent_ptr))

Register an atomic variable for debug tracking with automatic name generation.

Uses stringification to avoid repeating the variable name. Simplifies the declaration + registration pattern.

Usage:

// In header:
extern atomic_t g_counter;
// In source:
atomic_t g_counter = {0};
// In main/init:
ATOMIC_REGISTER_AUTO(g_counter, NULL);
#define ATOMIC_REGISTER_AUTO(name, parent_ptr)
Register an atomic variable for debug tracking with automatic name generation.
Definition atomic.h:472

Definition at line 472 of file atomic.h.

Function Documentation

◆ atomic_cas_bool()

bool atomic_cas_bool ( atomic_t *  a,
bool *  expected,
bool  new_value 
)

Atomically compare-and-swap a boolean.

Parameters
aPointer to atomic_t
expectedExpected current value (passed by reference)
new_valueValue to store if comparison succeeds
Returns
true if swap succeeded, false otherwise

Definition at line 184 of file atomic.c.

184 {
185 if (!a || !expected)
186 return false;
187 uint64_t exp = (uint64_t)*expected;
188 bool success = atomic_compare_exchange_strong((_Atomic(uint64_t) *)&a->impl, &exp, (uint64_t)new_value);
189 *expected = (bool)exp;
190 atomic_on_cas(a, success);
191 return success;
192}
void atomic_on_cas(atomic_t *a, bool success)
Definition atomic.c:348
unsigned long long uint64_t
Definition common.h:59
#define bool
Definition stdbool.h:61

References atomic_on_cas(), and bool.

Referenced by handle_image_frame_packet(), keyboard_help_check_signal_cancel(), log_lock_terminal(), log_search_check_signal_cancel(), splash_intro_start(), and symbol_cache_resolve_batch().

◆ atomic_cas_bool_impl()

bool atomic_cas_bool_impl ( atomic_t *  a,
bool *  expected,
bool  new_value 
)

Definition at line 36 of file atomic.c.

36 {
37 if (!a || !expected)
38 return false;
39 uint64_t exp = (uint64_t)*expected;
40 bool success = atomic_compare_exchange_strong((_Atomic(uint64_t) *)&a->impl, &exp, (uint64_t)new_value);
41 *expected = (bool)exp;
42 return success;
43}

References bool.

◆ atomic_cas_int()

bool atomic_cas_int ( atomic_t *  a,
int *  expected,
int  new_value 
)

Atomically compare-and-swap an int.

Parameters
aPointer to atomic_t
expectedExpected current value (passed by reference)
new_valueValue to store if comparison succeeds
Returns
true if swap succeeded, false otherwise

Definition at line 225 of file atomic.c.

225 {
226 if (!a || !expected)
227 return false;
228 bool success = atomic_cas_int_impl(a, expected, new_value);
229 atomic_on_cas(a, success);
230 return success;
231}
bool atomic_cas_int_impl(atomic_t *a, int *expected, int new_value)
Definition atomic.c:107

References atomic_cas_int_impl(), and atomic_on_cas().

Referenced by lifecycle_init(), and lifecycle_init_once().

◆ atomic_cas_int_impl()

bool atomic_cas_int_impl ( atomic_t *  a,
int *  expected,
int  new_value 
)

Definition at line 107 of file atomic.c.

107 {
108 if (!a || !expected)
109 return false;
110 uint64_t expected_u64 = (uint64_t)(int64_t)*expected;
111 bool result =
112 atomic_compare_exchange_strong((_Atomic(uint64_t) *)&a->impl, &expected_u64, (uint64_t)(int64_t)new_value);
113 *expected = (int)(int32_t)expected_u64;
114 return result;
115}

Referenced by atomic_cas_int().

◆ atomic_cas_u64()

bool atomic_cas_u64 ( atomic_t *  a,
uint64_t *  expected,
uint64_t  new_value 
)

Atomically compare-and-swap a uint64_t.

Parameters
aPointer to atomic_t
expectedExpected current value (passed by reference)
new_valueValue to store if comparison succeeds
Returns
true if swap succeeded, false otherwise

Definition at line 264 of file atomic.c.

264 {
265 if (!a || !expected)
266 return false;
267 bool success = atomic_compare_exchange_strong((_Atomic(uint64_t) *)&a->impl, expected, new_value);
268 atomic_on_cas(a, success);
269 return success;
270}

References atomic_on_cas().

Referenced by buffer_pool_alloc(), create_mixed_ascii_frame_for_client(), lifecycle_destroy_once(), lifecycle_init(), lifecycle_init_once(), lifecycle_reset(), lifecycle_shutdown(), lifecycle_shutdown_forever(), and websocket_callback_timing_record().

◆ atomic_cas_u64_impl()

bool atomic_cas_u64_impl ( atomic_t *  a,
uint64_t *  expected,
uint64_t  new_value 
)

Definition at line 101 of file atomic.c.

101 {
102 if (!a || !expected)
103 return false;
104 return atomic_compare_exchange_strong((_Atomic(uint64_t) *)&a->impl, expected, new_value);
105}

◆ atomic_exchange_bool()

bool atomic_exchange_bool ( atomic_t *  a,
bool  new_value 
)

Atomically exchange a boolean and return the old value.

Parameters
aPointer to atomic_t
new_valueBoolean value to store
Returns
Previous boolean value

Atomically swaps the new value for the old value and returns the old value. Avoids TOCTOU race conditions present in load+check+cas patterns.

Definition at line 303 of file atomic.c.

303 {
304 if (!a)
305 return false;
306 bool result = (bool)atomic_exchange((_Atomic(uint64_t) *)&a->impl, (uint64_t)new_value);
308 return result;
309}
void atomic_on_store(atomic_t *a)
Definition atomic.c:341

References atomic_on_store(), and bool.

Referenced by audio_ring_buffer_read(), disconnect_client_for_bad_data(), display_disable_logging_for_first_frame(), handle_image_frame_h265_packet(), and session_display_write_ascii().

◆ atomic_exchange_bool_impl()

bool atomic_exchange_bool_impl ( atomic_t *  a,
bool  new_value 
)

Definition at line 145 of file atomic.c.

145 {
146 if (!a)
147 return false;
148 return (bool)atomic_exchange((_Atomic(uint64_t) *)&a->impl, (uint64_t)new_value);
149}

◆ atomic_exchange_int()

int atomic_exchange_int ( atomic_t *  a,
int  new_value 
)

Atomically exchange an int and return the old value.

Parameters
aPointer to atomic_t
new_valueInt value to store
Returns
Previous int value

Definition at line 311 of file atomic.c.

311 {
312 if (!a)
313 return 0;
314 int result = (int)(int32_t)atomic_exchange((_Atomic(uint64_t) *)&a->impl, (uint64_t)(int32_t)new_value);
316 return result;
317}

References atomic_on_store().

◆ atomic_exchange_int_impl()

int atomic_exchange_int_impl ( atomic_t *  a,
int  new_value 
)

Definition at line 151 of file atomic.c.

151 {
152 if (!a)
153 return 0;
154 return (int)(int32_t)atomic_exchange((_Atomic(uint64_t) *)&a->impl, (uint64_t)(int32_t)new_value);
155}

◆ atomic_exchange_u64()

uint64_t atomic_exchange_u64 ( atomic_t *  a,
uint64_t  new_value 
)

Atomically exchange a uint64_t and return the old value.

Parameters
aPointer to atomic_t
new_valueValue to store
Returns
Previous uint64_t value

Used for atomic min/max updates and other exchange patterns where you need the old value atomically. Avoids TOCTOU race conditions present in load+check+cas.

Example (atomic minimum):

uint64_t old = atomic_exchange_u64(&stats->min_val, 0);
// old is the previous value, atomically
asciichat_error_stats_t stats
uint64_t atomic_exchange_u64(atomic_t *a, uint64_t new_value)
Atomically exchange a uint64_t and return the old value.
Definition atomic.c:319

Definition at line 319 of file atomic.c.

319 {
320 if (!a)
321 return 0;
322 uint64_t result = atomic_exchange((_Atomic(uint64_t) *)&a->impl, new_value);
324 return result;
325}

References atomic_on_store().

◆ atomic_exchange_u64_impl()

uint64_t atomic_exchange_u64_impl ( atomic_t *  a,
uint64_t  new_value 
)

Definition at line 157 of file atomic.c.

157 {
158 if (!a)
159 return 0;
160 return atomic_exchange((_Atomic(uint64_t) *)&a->impl, new_value);
161}

◆ atomic_fetch_add_int()

int atomic_fetch_add_int ( atomic_t *  a,
int  delta 
)

Atomically add to an int and return the previous value.

Parameters
aPointer to atomic_t
deltaValue to add
Returns
Previous value before the add

Definition at line 209 of file atomic.c.

209 {
210 if (!a)
211 return 0;
212 int result = (int)(int32_t)atomic_fetch_add((_Atomic(uint64_t) *)&a->impl, (uint64_t)(int64_t)delta);
214 return result;
215}
void atomic_on_fetch(atomic_t *a)
Definition atomic.c:358

References atomic_on_fetch().

◆ atomic_fetch_add_int_impl()

int atomic_fetch_add_int_impl ( atomic_t *  a,
int64_t  delta 
)

Definition at line 61 of file atomic.c.

61 {
62 if (!a)
63 return 0;
64 return (int)(int32_t)atomic_fetch_add((_Atomic(uint64_t) *)&a->impl, (uint64_t)delta);
65}

◆ atomic_fetch_add_u64()

uint64_t atomic_fetch_add_u64 ( atomic_t *  a,
uint64_t  delta 
)

Atomically add to a uint64_t and return the previous value.

Parameters
aPointer to atomic_t
deltaValue to add
Returns
Previous value before the add

Definition at line 248 of file atomic.c.

248 {
249 if (!a)
250 return 0;
251 uint64_t result = atomic_fetch_add((_Atomic(uint64_t) *)&a->impl, delta);
253 return result;
254}

References atomic_on_fetch().

Referenced by acds_client_handler(), audio_ring_buffer_read(), buffer_pool_alloc(), buffer_pool_free(), buffer_pool_shrink(), client_send_thread_func(), cond_on_wait(), get_utf8_palette_cache(), log_mmap_rotate(), log_mmap_write(), node_pool_get(), packet_queue_enqueue(), packet_queue_enqueue_packet(), packet_queue_try_dequeue(), ringbuffer_write(), rwlock_on_rdlock(), session_display_write_ascii(), session_log_buffer_append(), symbol_cache_lookup(), video_frame_commit(), and websocket_callback_timing_record().

◆ atomic_fetch_add_u64_impl()

uint64_t atomic_fetch_add_u64_impl ( atomic_t *  a,
uint64_t  delta 
)

Definition at line 89 of file atomic.c.

89 {
90 if (!a)
91 return 0;
92 return atomic_fetch_add((_Atomic(uint64_t) *)&a->impl, delta);
93}

◆ atomic_fetch_sub_int()

int atomic_fetch_sub_int ( atomic_t *  a,
int  delta 
)

Atomically subtract from an int and return the previous value.

Parameters
aPointer to atomic_t
deltaValue to subtract
Returns
Previous value before the subtract

Definition at line 217 of file atomic.c.

217 {
218 if (!a)
219 return 0;
220 int result = (int)(int32_t)atomic_fetch_sub((_Atomic(uint64_t) *)&a->impl, (uint64_t)(int64_t)delta);
222 return result;
223}

References atomic_on_fetch().

◆ atomic_fetch_sub_int_impl()

int atomic_fetch_sub_int_impl ( atomic_t *  a,
int64_t  delta 
)

Definition at line 67 of file atomic.c.

67 {
68 if (!a)
69 return 0;
70 return (int)(int32_t)atomic_fetch_sub((_Atomic(uint64_t) *)&a->impl, (uint64_t)delta);
71}

◆ atomic_fetch_sub_u64()

uint64_t atomic_fetch_sub_u64 ( atomic_t *  a,
uint64_t  delta 
)

Atomically subtract from a uint64_t and return the previous value.

Parameters
aPointer to atomic_t
deltaValue to subtract
Returns
Previous value before the subtract

Definition at line 256 of file atomic.c.

256 {
257 if (!a)
258 return 0;
259 uint64_t result = atomic_fetch_sub((_Atomic(uint64_t) *)&a->impl, delta);
261 return result;
262}

References atomic_on_fetch().

Referenced by buffer_pool_alloc(), buffer_pool_free(), buffer_pool_shrink(), cond_on_signal(), log_mmap_write(), node_pool_put(), packet_queue_enqueue(), packet_queue_enqueue_packet(), packet_queue_try_dequeue(), ringbuffer_read(), and rwlock_on_unlock().

◆ atomic_fetch_sub_u64_impl()

uint64_t atomic_fetch_sub_u64_impl ( atomic_t *  a,
uint64_t  delta 
)

Definition at line 95 of file atomic.c.

95 {
96 if (!a)
97 return 0;
98 return atomic_fetch_sub((_Atomic(uint64_t) *)&a->impl, delta);
99}

◆ atomic_load_bool()

bool atomic_load_bool ( atomic_t *  a)

Atomically load a boolean value.

Parameters
aPointer to atomic_t
Returns
Current boolean value

Definition at line 169 of file atomic.c.

169 {
170 if (!a)
171 return false;
172 bool result = (bool)atomic_load((const _Atomic(uint64_t) *)&a->impl);
174 return result;
175}
void atomic_on_load(atomic_t *a)
Definition atomic.c:334

References atomic_on_load(), and bool.

Referenced by acds_client_handler(), acds_websocket_client_handler(), add_client(), add_webrtc_client(), any_clients_sending_video(), audio_sender_finalize(), audio_start_thread(), audio_stop_thread(), audio_thread_exited(), audio_write_samples(), broadcast_server_state_to_all_clients(), capture_stop_thread(), capture_thread_exited(), client_audio_render_thread(), client_dispatch_thread(), client_receive_thread(), client_send_thread_func(), client_video_render_thread(), debug_sync_is_cleanup_in_progress(), find_client_by_socket(), handle_audio_batch_packet(), handle_audio_opus_packet(), handle_image_frame_h265_packet(), handle_image_frame_packet(), handle_ping_packet(), keepalive_stop_thread(), keepalive_thread_exited(), keyboard_help_is_active(), keyboard_help_is_active_global(), keyboard_help_toggle(), log_lock_terminal(), log_msg(), log_search_needs_rerender(), mutex_stack_cleanup(), mutex_stack_cleanup_current_thread(), packet_queue_enqueue(), packet_queue_enqueue_packet(), packet_queue_try_dequeue(), protocol_connection_lost(), protocol_stop_connection(), remove_client(), send_server_state_to_client(), server_connection_is_active(), server_connection_is_lost(), session_display_has_first_frame(), session_display_write_ascii(), session_pipeline_run_main(), should_exit(), simple_frame_swap_update(), splash_is_running(), splash_wait_for_animation(), stats_logger_thread(), stop_client_render_threads(), tcp_client_is_active(), tcp_client_is_lost(), tcp_client_send_packet(), tcp_client_signal_lost(), tcp_server_run(), threaded_send_audio_batch_packet(), threaded_send_audio_opus(), threaded_send_audio_opus_batch(), threaded_send_packet(), update_server_stats(), video_frame_commit(), video_frame_get_latest(), webrtc_datachannel_is_open(), webrtc_datachannel_send(), websocket_client_is_active(), websocket_client_is_lost(), websocket_client_send_packet(), and websocket_server_run().

◆ atomic_load_bool_impl()

bool atomic_load_bool_impl ( const atomic_t *  a)

Definition at line 24 of file atomic.c.

24 {
25 if (!a)
26 return false;
27 return (bool)atomic_load((const _Atomic(uint64_t) *)&a->impl);
28}

◆ atomic_load_int()

int atomic_load_int ( atomic_t *  a)

Atomically load an int value.

Parameters
aPointer to atomic_t
Returns
Current int value

Definition at line 194 of file atomic.c.

194 {
195 if (!a)
196 return 0;
197 int result = (int)(int32_t)atomic_load((const _Atomic(uint64_t) *)&a->impl);
199 return result;
200}

References atomic_on_load().

Referenced by log_console_impl(), log_destroy(), log_disable_file_output(), log_init(), log_msg(), log_plain_msg(), log_search_get_global_highlight(), log_search_get_match_info(), log_search_get_pattern_singleton(), log_search_is_active(), log_search_is_entering(), log_search_is_entering_atomic(), log_truncate_if_large(), mutex_stack_cleanup(), mutex_stack_cleanup_current_thread(), and mutex_stack_get_all_threads().

◆ atomic_load_int_impl()

int atomic_load_int_impl ( const atomic_t *  a)

Definition at line 49 of file atomic.c.

49 {
50 if (!a)
51 return 0;
52 return (int)(int32_t)atomic_load((const _Atomic(uint64_t) *)&a->impl);
53}

◆ atomic_load_u64()

uint64_t atomic_load_u64 ( atomic_t *  a)

Atomically load a uint64_t value.

Parameters
aPointer to atomic_t
Returns
Current uint64_t value

Definition at line 233 of file atomic.c.

233 {
234 if (!a)
235 return 0;
236 uint64_t result = atomic_load((const _Atomic(uint64_t) *)&a->impl);
238 return result;
239}

References atomic_on_load().

Referenced by asciichat_pcre2_cleanup_all(), asciichat_pcre2_singleton_free(), asciichat_pcre2_singleton_get_code(), asciichat_pcre2_singleton_is_initialized(), audio_ring_buffer_available_read(), audio_ring_buffer_discard_pending(), audio_ring_buffer_peek(), audio_ring_buffer_read(), audio_ring_buffer_write(), buffer_pool_alloc(), buffer_pool_free(), buffer_pool_get_stats(), buffer_pool_log_stats(), buffer_pool_shrink(), client_send_thread_func(), cond_format_state(), cond_on_signal(), create_mixed_ascii_frame_for_client(), get_utf8_palette_cache(), lifecycle_is_dead(), lifecycle_is_initialized(), lifecycle_shutdown_forever(), log_file_msg(), log_get_force_stderr(), log_get_level(), log_get_terminal_output(), log_init(), log_mmap_destroy(), log_mmap_get_stats(), log_mmap_get_usage(), log_mmap_rotate(), log_msg(), log_plain_msg(), log_shutdown_begin(), log_terminal_msg(), node_pool_get(), packet_queue_enqueue(), packet_queue_enqueue_packet(), packet_queue_get_stats(), packet_queue_is_full(), packet_queue_size(), packet_queue_try_dequeue(), protocol_stop_connection(), ringbuffer_peek(), ringbuffer_read(), ringbuffer_size(), ringbuffer_write(), rwlock_format_state(), rwlock_on_unlock(), session_display_write_audio(), session_log_buffer_append(), session_log_buffer_get_recent(), session_server_like_run(), session_server_like_shutdown_requested(), simple_frame_swap_get(), symbol_cache_destroy(), symbol_cache_get_stats(), symbol_cache_print_stats(), threaded_send_client_join_packet(), video_frame_get_stats(), webrtc_get_gathering_state(), webrtc_get_state(), webrtc_is_gathering_timed_out(), websocket_callback_timing_log_stats(), and websocket_callback_timing_record().

◆ atomic_load_u64_impl()

uint64_t atomic_load_u64_impl ( const atomic_t *  a)

Definition at line 77 of file atomic.c.

77 {
78 if (!a)
79 return 0;
80 return atomic_load((const _Atomic(uint64_t) *)&a->impl);
81}

◆ atomic_on_cas()

void atomic_on_cas ( atomic_t *  a,
bool  success 
)

Definition at line 348 of file atomic.c.

348 {
349 if (!a || !debug_atomic_is_initialized())
350 return;
351 a->cas_count++;
352 if (success) {
355 }
356}
bool debug_atomic_is_initialized(void)
uint64_t time_get_ns(void)
Get current monotonic time in nanoseconds.
Definition util/time.c:108
uint64_t cas_count
Definition atomic.h:83
uint64_t last_store_time_ns
Definition atomic.h:79
uint64_t cas_success_count
Definition atomic.h:84

References atomic_t::cas_count, atomic_t::cas_success_count, debug_atomic_is_initialized(), atomic_t::last_store_time_ns, and time_get_ns().

Referenced by atomic_cas_bool(), atomic_cas_int(), and atomic_cas_u64().

◆ atomic_on_fetch()

void atomic_on_fetch ( atomic_t *  a)

◆ atomic_on_load()

void atomic_on_load ( atomic_t *  a)

Definition at line 334 of file atomic.c.

334 {
335 if (!a || !debug_atomic_is_initialized())
336 return;
338 a->load_count++;
339}
uint64_t last_load_time_ns
Definition atomic.h:80
uint64_t load_count
Definition atomic.h:82

References debug_atomic_is_initialized(), atomic_t::last_load_time_ns, atomic_t::load_count, and time_get_ns().

Referenced by atomic_load_bool(), atomic_load_int(), and atomic_load_u64().

◆ atomic_on_store()

◆ atomic_ptr_cas()

bool atomic_ptr_cas ( atomic_ptr_t *  a,
void **  expected,
void *  new_value 
)

Atomically compare-and-swap a pointer.

Parameters
aPointer to atomic_ptr_t
expectedExpected current pointer (passed by reference)
new_valuePointer value to store if comparison succeeds
Returns
true if swap succeeded, false otherwise

Definition at line 287 of file atomic.c.

287 {
288 if (!a || !expected)
289 return false;
290 bool success = atomic_compare_exchange_strong((_Atomic(void *) *)&a->impl, expected, new_value);
291 atomic_ptr_on_cas(a, success);
292 return success;
293}
void atomic_ptr_on_cas(atomic_ptr_t *a, bool success)
Definition atomic.c:379

References atomic_ptr_on_cas().

Referenced by buffer_pool_alloc(), buffer_pool_free(), buffer_pool_shrink(), node_pool_get(), node_pool_put(), packet_queue_enqueue(), packet_queue_enqueue_packet(), and packet_queue_try_dequeue().

◆ atomic_ptr_cas_impl()

bool atomic_ptr_cas_impl ( atomic_ptr_t *  a,
void **  expected,
void *  new_value 
)

Definition at line 133 of file atomic.c.

133 {
134 if (!a || !expected)
135 return false;
136 return atomic_compare_exchange_strong((_Atomic(void *) *)&a->impl, expected, new_value);
137}

◆ atomic_ptr_exchange()

void * atomic_ptr_exchange ( atomic_ptr_t *  a,
void *  new_value 
)

Atomically exchange a pointer and return the old value.

Parameters
aPointer to atomic_ptr_t
new_valuePointer value to store
Returns
Previous pointer value

Definition at line 295 of file atomic.c.

295 {
296 if (!a)
297 return NULL;
298 void *result = atomic_exchange((_Atomic(void *) *)&a->impl, new_value);
300 return result;
301}
void atomic_ptr_on_exchange(atomic_ptr_t *a)
Definition atomic.c:389

References atomic_ptr_on_exchange().

Referenced by buffer_pool_shrink().

◆ atomic_ptr_exchange_impl()

void * atomic_ptr_exchange_impl ( atomic_ptr_t *  a,
void *  new_value 
)

Definition at line 139 of file atomic.c.

139 {
140 if (!a)
141 return NULL;
142 return atomic_exchange((_Atomic(void *) *)&a->impl, new_value);
143}

◆ atomic_ptr_load()

void * atomic_ptr_load ( atomic_ptr_t *  a)

Atomically load a pointer.

Parameters
aPointer to atomic_ptr_t
Returns
Current pointer value

Definition at line 272 of file atomic.c.

272 {
273 if (!a)
274 return NULL;
275 void *result = atomic_load((const _Atomic(void *) *)&a->impl);
277 return result;
278}
void atomic_ptr_on_load(atomic_ptr_t *a)
Definition atomic.c:365

References atomic_ptr_on_load().

Referenced by buffer_pool_alloc(), buffer_pool_destroy(), buffer_pool_free(), buffer_pool_shrink(), log_get_session_log_buffer(), node_pool_get(), node_pool_put(), options_get(), options_state_destroy(), options_state_init(), options_state_set(), packet_queue_enqueue(), packet_queue_enqueue_packet(), packet_queue_try_dequeue(), and shutdown_is_requested().

◆ atomic_ptr_load_impl()

void * atomic_ptr_load_impl ( const atomic_ptr_t *  a)

Definition at line 121 of file atomic.c.

121 {
122 if (!a)
123 return NULL;
124 return atomic_load((const _Atomic(void *) *)&a->impl);
125}

◆ atomic_ptr_on_cas()

void atomic_ptr_on_cas ( atomic_ptr_t *  a,
bool  success 
)

Definition at line 379 of file atomic.c.

379 {
380 if (!a || !debug_atomic_is_initialized())
381 return;
382 a->cas_count++;
383 if (success) {
386 }
387}
uint64_t cas_success_count
Definition atomic.h:114
uint64_t cas_count
Definition atomic.h:113
uint64_t last_store_time_ns
Definition atomic.h:109

References atomic_ptr_t::cas_count, atomic_ptr_t::cas_success_count, debug_atomic_is_initialized(), atomic_ptr_t::last_store_time_ns, and time_get_ns().

Referenced by atomic_ptr_cas().

◆ atomic_ptr_on_exchange()

void atomic_ptr_on_exchange ( atomic_ptr_t *  a)

Definition at line 389 of file atomic.c.

389 {
390 if (!a || !debug_atomic_is_initialized())
391 return;
392 a->exchange_count++;
394}
uint64_t exchange_count
Definition atomic.h:115

References debug_atomic_is_initialized(), atomic_ptr_t::exchange_count, atomic_ptr_t::last_store_time_ns, and time_get_ns().

Referenced by atomic_ptr_exchange().

◆ atomic_ptr_on_load()

void atomic_ptr_on_load ( atomic_ptr_t *  a)

Definition at line 365 of file atomic.c.

365 {
366 if (!a || !debug_atomic_is_initialized())
367 return;
369 a->load_count++;
370}
uint64_t last_load_time_ns
Definition atomic.h:110
uint64_t load_count
Definition atomic.h:112

References debug_atomic_is_initialized(), atomic_ptr_t::last_load_time_ns, atomic_ptr_t::load_count, and time_get_ns().

Referenced by atomic_ptr_load().

◆ atomic_ptr_on_store()

void atomic_ptr_on_store ( atomic_ptr_t *  a)

Definition at line 372 of file atomic.c.

372 {
373 if (!a || !debug_atomic_is_initialized())
374 return;
376 a->store_count++;
377}
uint64_t store_count
Definition atomic.h:111

References debug_atomic_is_initialized(), atomic_ptr_t::last_store_time_ns, atomic_ptr_t::store_count, and time_get_ns().

Referenced by atomic_ptr_store().

◆ atomic_ptr_store()

void atomic_ptr_store ( atomic_ptr_t *  a,
void *  value 
)

◆ atomic_ptr_store_impl()

void atomic_ptr_store_impl ( atomic_ptr_t *  a,
void *  value 
)

Definition at line 127 of file atomic.c.

127 {
128 if (!a)
129 return;
130 atomic_store((_Atomic(void *) *)&a->impl, value);
131}

◆ atomic_store_bool()

void atomic_store_bool ( atomic_t *  a,
bool  value 
)

Atomically store a boolean value.

Parameters
aPointer to atomic_t
valueValue to store

Definition at line 177 of file atomic.c.

177 {
178 if (!a)
179 return;
180 atomic_store((_Atomic(uint64_t) *)&a->impl, (uint64_t)value);
182}

References atomic_on_store().

Referenced by acds_server_init(), acds_server_shutdown(), acip_websocket_client_transport_create(), add_client(), add_webrtc_client(), app_client_create(), audio_destroy(), audio_init(), audio_ring_buffer_clear(), audio_ring_buffer_discard_pending(), audio_sender_cleanup(), audio_sender_init(), audio_start_duplex(), audio_start_thread(), audio_stop_duplex(), audio_stop_thread(), capture_start_thread(), client_receive_thread(), client_send_thread_func(), create_client_render_threads(), debug_sync_cleanup_thread(), debug_sync_print_backtrace_delayed(), debug_sync_print_state_delayed(), debug_sync_start_thread(), debug_sync_trigger_print(), disconnect_client_for_bad_data(), display_reset_for_new_connection(), handle_client_leave_packet(), handle_stream_start_packet(), handle_stream_stop_packet(), keepalive_start_thread(), keyboard_help_signal_cancel(), keyboard_help_toggle(), log_destroy(), log_init(), log_search_enter_mode(), log_search_exit_mode(), log_search_handle_key(), log_search_init(), log_search_needs_rerender(), log_search_signal_cancel(), log_set_level(), log_shutdown_begin(), mutex_stack_cleanup(), packet_queue_create_with_pools(), protocol_start_connection(), remove_client(), server_connection_close(), server_connection_establish(), server_connection_init(), server_connection_lost(), server_connection_set_transport(), server_connection_shutdown(), session_display_create(), session_display_reset_first_frame(), session_pipeline_create(), session_pipeline_destroy(), session_pipeline_run_main(), session_server_like_run(), signal_exit(), simple_frame_swap_create(), simple_frame_swap_update(), splash_intro_start(), splash_wait_for_animation(), stop_client_render_threads(), stop_client_threads(), tcp_client_close(), tcp_client_connect(), tcp_client_create(), tcp_client_shutdown(), tcp_client_signal_lost(), tcp_server_destroy(), tcp_server_init(), video_frame_buffer_create(), video_frame_commit(), video_frame_get_latest(), webrtc_close_datachannel(), webrtc_create_datachannel(), webrtc_datachannel_set_open_state(), websocket_client_close(), websocket_client_connect(), websocket_client_create(), websocket_client_shutdown(), websocket_client_signal_lost(), websocket_server_destroy(), and websocket_server_init().

◆ atomic_store_bool_impl()

void atomic_store_bool_impl ( atomic_t *  a,
uint64_t  value 
)

Definition at line 30 of file atomic.c.

30 {
31 if (!a)
32 return;
33 atomic_store((_Atomic(uint64_t) *)&a->impl, value);
34}

◆ atomic_store_int()

void atomic_store_int ( atomic_t *  a,
int  value 
)

Atomically store an int value.

Parameters
aPointer to atomic_t
valueValue to store

Definition at line 202 of file atomic.c.

202 {
203 if (!a)
204 return;
205 atomic_store((_Atomic(uint64_t) *)&a->impl, (uint64_t)(int32_t)value);
207}

References atomic_on_store().

Referenced by lifecycle_init_abort(), lifecycle_init_commit(), lifecycle_shutdown(), log_destroy(), log_disable_file_output(), log_init(), log_search_enter_mode(), log_search_exit_mode(), log_search_init(), log_set_json_output(), and mutex_stack_cleanup().

◆ atomic_store_int_impl()

void atomic_store_int_impl ( atomic_t *  a,
uint64_t  value 
)

Definition at line 55 of file atomic.c.

55 {
56 if (!a)
57 return;
58 atomic_store((_Atomic(uint64_t) *)&a->impl, value);
59}

◆ atomic_store_u64()

void atomic_store_u64 ( atomic_t *  a,
uint64_t  value 
)

Atomically store a uint64_t value.

Parameters
aPointer to atomic_t
valueValue to store

Definition at line 241 of file atomic.c.

241 {
242 if (!a)
243 return;
244 atomic_store((_Atomic(uint64_t) *)&a->impl, value);
246}

References atomic_on_store().

Referenced by acds_server_init(), add_client(), add_webrtc_client(), asciichat_pcre2_cleanup_all(), asciichat_pcre2_singleton_compile(), asciichat_pcre2_singleton_get_code(), audio_ring_buffer_clear(), audio_ring_buffer_discard_pending(), audio_ring_buffer_read(), audio_ring_buffer_write(), buffer_pool_alloc(), buffer_pool_create(), buffer_pool_free(), client_send_thread_func(), client_video_render_thread(), cond_on_broadcast(), get_utf8_palette_cache(), lifecycle_destroy_commit(), lifecycle_init_abort(), lifecycle_init_commit(), log_init(), log_lock_terminal(), log_mmap_init(), log_mmap_rotate(), log_set_flush_delay(), log_set_force_stderr(), log_set_format(), log_set_level(), log_set_terminal_output(), log_shutdown_end(), log_truncate_if_large(), log_unlock_terminal(), node_pool_create(), packet_queue_create_with_pools(), packet_queue_stop(), ringbuffer_clear(), ringbuffer_read(), ringbuffer_write(), session_display_write_audio(), session_log_buffer_append(), session_log_buffer_clear(), session_log_buffer_create(), session_pipeline_create(), session_server_like_run(), simple_frame_swap_create(), simple_frame_swap_update(), splash_intro_done(), symbol_cache_init(), video_frame_buffer_create(), webrtc_create_peer_connection(), and websocket_callback_timing_reset().

◆ atomic_store_u64_impl()

void atomic_store_u64_impl ( atomic_t *  a,
uint64_t  value 
)

Definition at line 83 of file atomic.c.

83 {
84 if (!a)
85 return;
86 atomic_store((_Atomic(uint64_t) *)&a->impl, value);
87}

◆ debug_atomic_init()

void debug_atomic_init ( void  )

Definition at line 30 of file debug/atomic.c.

30 {
31 g_atomic_debug_initialized = true;
32}

◆ debug_atomic_is_initialized()

bool debug_atomic_is_initialized ( void  )

Definition at line 38 of file debug/atomic.c.

38 {
39 return g_atomic_debug_initialized;
40}

◆ debug_atomic_print_state()

void debug_atomic_print_state ( void  )

Definition at line 138 of file debug/atomic.c.

138 {
139 if (!g_atomic_debug_initialized)
140 return;
141
142 // Iterate named registry for atomic entries
143 // Note: this is now kept for backward compatibility
144 // The preferred method is to use sync.c's atomic_iter_callback
145 // which integrates atomics into the synchronized output buffer
146}

◆ debug_atomic_shutdown()

void debug_atomic_shutdown ( void  )

Definition at line 34 of file debug/atomic.c.

34 {
35 g_atomic_debug_initialized = false;
36}