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

Debug tracking for atomic operations. More...

Go to the source code of this file.

Functions

void debug_atomic_register (atomic_t *a, const char *name)
 Register an atomic_t in the debug named registry.
 
void debug_atomic_unregister (atomic_t *a)
 Unregister an atomic_t from the debug named registry.
 
void debug_atomic_ptr_register (_Atomic(void *) *a, const char *name)
 Register an _Atomic(void *) in the debug named registry.
 
void debug_atomic_ptr_unregister (_Atomic(void *) *a)
 Unregister an _Atomic(void *) from the debug named registry.
 
int debug_atomic_format_timing (const atomic_t *atomic, char *buffer, size_t size)
 Format timing info for an atomic_t to a buffer.
 
int debug_atomic_ptr_format_timing (const atomic_ptr_t *atomic, char *buffer, size_t size)
 Format timing info for an atomic_ptr_t to a buffer.
 
void debug_atomic_init (void)
 Lifecycle initialization for debug atomic tracking.
 
void debug_atomic_shutdown (void)
 Lifecycle shutdown for debug atomic tracking.
 
bool debug_atomic_is_initialized (void)
 Check if debug atomic tracking is initialized.
 
void debug_atomic_print_state (void)
 Print all named atomic operations state (integration with –sync-state)
 

Detailed Description

Debug tracking for atomic operations.

Date
March 2026

Provides debug-only tracking for named atomics via the named.c registry. Integrates with –sync-state output to display atomic timing and counts.

Definition in file debug/atomic.h.

Function Documentation

◆ debug_atomic_format_timing()

int debug_atomic_format_timing ( const atomic_t *  atomic,
char *  buffer,
size_t  size 
)

Format timing info for an atomic_t to a buffer.

Parameters
atomicPointer to atomic_t
bufferOutput buffer for formatted timing info
sizeBuffer size
Returns
Number of bytes written (0 if never accessed)

Used by –sync-state to display atomic operation timing and counts. Only formats output if the atomic has been accessed.

Format timing info for an atomic_t to a buffer.

Parameters
atomicPointer to atomic_t
bufferOutput buffer
sizeBuffer size
Returns
Number of bytes written to buffer

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

53 {
54 if (!atomic || !buffer || size == 0)
55 return 0;
56
57 // If never accessed, return empty (so caller can skip output)
58 if (atomic->last_load_time_ns == 0 && atomic->last_store_time_ns == 0) {
59 return 0;
60 }
61
62 int offset = 0;
63 uint64_t now_ns = time_get_ns();
64
65 char load_str[64] = "";
66 char store_str[64] = "";
67 char count_str[128] = "";
68
69 if (atomic->last_load_time_ns > 0 && atomic->last_load_time_ns <= now_ns) {
70 char elapsed_str[64];
71 time_pretty(now_ns - atomic->last_load_time_ns, -1, elapsed_str, sizeof(elapsed_str));
72 snprintf(load_str, sizeof(load_str), "load=%s", elapsed_str);
73 }
74
75 if (atomic->last_store_time_ns > 0 && atomic->last_store_time_ns <= now_ns) {
76 char elapsed_str[64];
77 time_pretty(now_ns - atomic->last_store_time_ns, -1, elapsed_str, sizeof(elapsed_str));
78 snprintf(store_str, sizeof(store_str), "store=%s", elapsed_str);
79 }
80
81 if (atomic->load_count > 0 || atomic->store_count > 0 || atomic->cas_count > 0 || atomic->fetch_count > 0) {
82 snprintf(count_str, sizeof(count_str), "[ops: load=%llu store=%llu cas=%llu/%llu fetch=%llu]",
83 (unsigned long long)atomic->load_count, (unsigned long long)atomic->store_count,
84 (unsigned long long)atomic->cas_success_count, (unsigned long long)atomic->cas_count,
85 (unsigned long long)atomic->fetch_count);
86 }
87
88 offset += snprintf(buffer + offset, size - offset, "%s %s %s", load_str, store_str, count_str);
89 return offset;
90}
unsigned long long uint64_t
Definition common.h:59
uint64_t time_get_ns(void)
Get current monotonic time in nanoseconds.
Definition util/time.c:108
int time_pretty(uint64_t nanoseconds, int decimals, char *buffer, size_t buffer_size)
Format nanoseconds as pretty duration with spaces and configurable precision.
Definition util/time.c:424
uint64_t last_load_time_ns
Definition atomic.h:80
uint64_t fetch_count
Definition atomic.h:85
uint64_t cas_count
Definition atomic.h:83
uint64_t store_count
Definition atomic.h:81
uint64_t load_count
Definition atomic.h:82
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, atomic_t::fetch_count, atomic_t::last_load_time_ns, atomic_t::last_store_time_ns, atomic_t::load_count, atomic_t::store_count, time_get_ns(), and time_pretty().

◆ debug_atomic_init()

void debug_atomic_init ( void  )

Lifecycle initialization for debug atomic tracking.

In release builds (NDEBUG), this is a no-op macro. In debug builds, this initializes atomic operation tracking.

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

30 {
31 g_atomic_debug_initialized = true;
32}

Referenced by main().

◆ debug_atomic_is_initialized()

bool debug_atomic_is_initialized ( void  )

Check if debug atomic tracking is initialized.

In release builds (NDEBUG), returns true. In debug builds, returns actual initialization state.

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  )

Print all named atomic operations state (integration with –sync-state)

Called by debug_sync_print_state() to include atomic operations in sync state output. In release builds (NDEBUG), this is a no-op macro. In debug builds, this prints detailed atomic operation statistics.

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_ptr_format_timing()

int debug_atomic_ptr_format_timing ( const atomic_ptr_t *  atomic,
char *  buffer,
size_t  size 
)

Format timing info for an atomic_ptr_t to a buffer.

Parameters
atomicPointer to atomic_ptr_t
bufferOutput buffer for formatted timing info
sizeBuffer size
Returns
Number of bytes written (0 if never accessed)

Used by –sync-state to display atomic operation timing and counts. Only formats output if the atomic has been accessed.

Format timing info for an atomic_ptr_t to a buffer.

Parameters
atomicPointer to atomic_ptr_t
bufferOutput buffer
sizeBuffer size
Returns
Number of bytes written to buffer

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

99 {
100 if (!atomic || !buffer || size == 0)
101 return 0;
102
103 // If never accessed, return empty (so caller can skip output)
104 if (atomic->last_load_time_ns == 0 && atomic->last_store_time_ns == 0) {
105 return 0;
106 }
107
108 int offset = 0;
109 uint64_t now_ns = time_get_ns();
110
111 char load_str[64] = "";
112 char store_str[64] = "";
113 char count_str[128] = "";
114
115 if (atomic->last_load_time_ns > 0 && atomic->last_load_time_ns <= now_ns) {
116 char elapsed_str[64];
117 time_pretty(now_ns - atomic->last_load_time_ns, -1, elapsed_str, sizeof(elapsed_str));
118 snprintf(load_str, sizeof(load_str), "load=%s", elapsed_str);
119 }
120
121 if (atomic->last_store_time_ns > 0 && atomic->last_store_time_ns <= now_ns) {
122 char elapsed_str[64];
123 time_pretty(now_ns - atomic->last_store_time_ns, -1, elapsed_str, sizeof(elapsed_str));
124 snprintf(store_str, sizeof(store_str), "store=%s", elapsed_str);
125 }
126
127 if (atomic->load_count > 0 || atomic->store_count > 0 || atomic->cas_count > 0 || atomic->exchange_count > 0) {
128 snprintf(count_str, sizeof(count_str), "[ops: load=%llu store=%llu cas=%llu/%llu exchange=%llu]",
129 (unsigned long long)atomic->load_count, (unsigned long long)atomic->store_count,
130 (unsigned long long)atomic->cas_success_count, (unsigned long long)atomic->cas_count,
131 (unsigned long long)atomic->exchange_count);
132 }
133
134 offset += snprintf(buffer + offset, size - offset, "%s %s %s", load_str, store_str, count_str);
135 return offset;
136}
uint64_t last_load_time_ns
Definition atomic.h:110
uint64_t store_count
Definition atomic.h:111
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
uint64_t exchange_count
Definition atomic.h:115
uint64_t load_count
Definition atomic.h:112

References atomic_ptr_t::cas_count, atomic_ptr_t::cas_success_count, atomic_ptr_t::exchange_count, atomic_ptr_t::last_load_time_ns, atomic_ptr_t::last_store_time_ns, atomic_ptr_t::load_count, atomic_ptr_t::store_count, time_get_ns(), and time_pretty().

◆ debug_atomic_ptr_register()

void debug_atomic_ptr_register ( _Atomic(void *) *  a,
const char *  name 
)

Register an _Atomic(void *) in the debug named registry.

Parameters
aPointer to _Atomic(void *) to register
nameHuman-readable name

◆ debug_atomic_ptr_unregister()

void debug_atomic_ptr_unregister ( _Atomic(void *) *  a)

Unregister an _Atomic(void *) from the debug named registry.

Parameters
aPointer to _Atomic(void *) to unregister

◆ debug_atomic_register()

void debug_atomic_register ( atomic_t *  a,
const char *  name 
)

Register an atomic_t in the debug named registry.

Parameters
aPointer to atomic_t to register
nameHuman-readable name

Called automatically by atomic_init_impl() in debug builds. Registers the atomic's address as a uintptr_t with the named registry.

◆ debug_atomic_shutdown()

void debug_atomic_shutdown ( void  )

Lifecycle shutdown for debug atomic tracking.

In release builds (NDEBUG), this is a no-op macro. In debug builds, this cleans up atomic operation tracking.

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

34 {
35 g_atomic_debug_initialized = false;
36}

Referenced by asciichat_shared_destroy().

◆ debug_atomic_unregister()

void debug_atomic_unregister ( atomic_t *  a)

Unregister an atomic_t from the debug named registry.

Parameters
aPointer to atomic_t to unregister

Called automatically by atomic_destroy() in debug builds.