ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
cond.c
Go to the documentation of this file.
1
14#include <ascii-chat/atomic.h>
15#include <ascii-chat/log/log.h>
16#include <stdio.h>
17
18#ifndef NDEBUG
19
33void cond_on_wait(cond_t *cond, mutex_t *mutex, const char *file, int line, const char *func) {
34 if (!cond)
35 return;
37 cond->last_wait_mutex = mutex;
38 cond->last_wait_file = file;
39 cond->last_wait_line = line;
40 cond->last_wait_func = func;
42 cond->last_waiting_key = asciichat_thread_to_key(current_thread);
44 cond->wait_count++;
45}
46
57 if (!cond)
58 return;
60 cond->signal_count++;
61 if (atomic_load_u64(&cond->waiting_count) > 0) {
63 }
64}
65
76 if (!cond)
77 return;
79 cond->broadcast_count++;
81}
82
95int cond_format_state(const cond_t *cond, char *buffer, size_t size) {
96 if (!cond || !buffer || size == 0)
97 return 0;
98
99 int offset = 0;
100 uint64_t now_ns = time_get_ns();
101
102 char wait_str[64] = "";
103 char signal_str[64] = "";
104 char broadcast_str[64] = "";
105 char waiting_str[64] = "";
106 char count_str[256] = "";
107
108 if (cond->last_wait_time_ns > 0 && cond->last_wait_time_ns <= now_ns) {
109 char elapsed_str[64];
110 time_pretty(now_ns - cond->last_wait_time_ns, -1, elapsed_str, sizeof(elapsed_str));
111 snprintf(wait_str, sizeof(wait_str), "wait=%s", elapsed_str);
112 }
113
114 if (cond->last_signal_time_ns > 0 && cond->last_signal_time_ns <= now_ns) {
115 char elapsed_str[64];
116 time_pretty(now_ns - cond->last_signal_time_ns, -1, elapsed_str, sizeof(elapsed_str));
117 snprintf(signal_str, sizeof(signal_str), "signal=%s", elapsed_str);
118 }
119
120 if (cond->last_broadcast_time_ns > 0 && cond->last_broadcast_time_ns <= now_ns) {
121 char elapsed_str[64];
122 time_pretty(now_ns - cond->last_broadcast_time_ns, -1, elapsed_str, sizeof(elapsed_str));
123 snprintf(broadcast_str, sizeof(broadcast_str), "broadcast=%s", elapsed_str);
124 }
125
126 uint64_t waiting_count = atomic_load_u64(&cond->waiting_count);
127 if (waiting_count > 0) {
128 snprintf(waiting_str, sizeof(waiting_str), "[WAITING=%llu]", (unsigned long long)waiting_count);
129 }
130
131 if (cond->wait_count > 0 || cond->signal_count > 0 || cond->broadcast_count > 0) {
132 snprintf(count_str, sizeof(count_str), "[ops: wait=%llu signal=%llu broadcast=%llu]",
133 (unsigned long long)cond->wait_count, (unsigned long long)cond->signal_count,
134 (unsigned long long)cond->broadcast_count);
135 }
136
137 offset += snprintf(buffer + offset, size - offset, "%s %s %s %s %s", wait_str, signal_str, broadcast_str, waiting_str,
138 count_str);
139 return offset;
140}
141
153void cond_log_state(const cond_t *cond, const char *file, int line, const char *func) {
154 if (!cond)
155 return;
156 char buf[512];
157 cond_format_state(cond, buf, sizeof(buf));
158 log_msg(LOG_DEBUG, file, line, func, "cond/state %p: %s", (const void *)cond, buf);
159}
160
161#endif // !NDEBUG
mutex_t mutex
uint64_t atomic_fetch_add_u64(atomic_t *a, uint64_t delta)
Atomically add to a uint64_t and return the previous value.
Definition atomic.c:248
uint64_t atomic_fetch_sub_u64(atomic_t *a, uint64_t delta)
Atomically subtract from a uint64_t and return the previous value.
Definition atomic.c:256
void atomic_store_u64(atomic_t *a, uint64_t value)
Atomically store a uint64_t value.
Definition atomic.c:241
uint64_t atomic_load_u64(atomic_t *a)
Atomically load a uint64_t value.
Definition atomic.c:233
⚛️ Atomic operations abstraction layer with debug tracking
Cross-platform condition variable interface for ascii-chat.
unsigned long long uint64_t
Definition common.h:59
uintptr_t asciichat_thread_to_key(asciichat_thread_t thread)
Convert a thread handle to a registry key.
Definition threading.c:87
void log_msg(log_level_t level, const char *file, int line, const char *func, const char *fmt,...)
Log a message at a specific level.
Definition log/log.c:1019
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
void cond_on_wait(cond_t *cond, mutex_t *mutex, const char *file, int line, const char *func)
Hook called when a thread waits on a condition variable.
Definition cond.c:33
void cond_on_broadcast(cond_t *cond)
Hook called when a condition variable is broadcast.
Definition cond.c:75
int cond_format_state(const cond_t *cond, char *buffer, size_t size)
Format condition variable timing and state info into buffer.
Definition cond.c:95
void cond_on_signal(cond_t *cond)
Hook called when a condition variable is signaled.
Definition cond.c:56
void cond_log_state(const cond_t *cond, const char *file, int line, const char *func)
Log the state of a condition variable.
Definition cond.c:153
uint64_t asciichat_thread_current_id(void)
Get the current thread's unique numeric ID.
Definition threading.c:92
🧵 Cross-platform thread interface for ascii-chat
void * asciichat_thread_t
📝 Logging API with multiple log levels and terminal output control
Condition variable type (POSIX: pthread_cond_t with debug tracking)
Definition cond.h:63
uint64_t last_wait_time_ns
Timestamp of last wait (nanoseconds)
Definition cond.h:70
mutex_t * last_wait_mutex
Associated mutex at most recent wait (for deadlock detection)
Definition cond.h:72
uint64_t broadcast_count
Total broadcast calls.
Definition cond.h:78
uint64_t last_broadcast_time_ns
Timestamp of last broadcast (nanoseconds)
Definition cond.h:69
uint64_t last_signal_time_ns
Timestamp of last signal (nanoseconds)
Definition cond.h:68
uintptr_t last_waiting_key
Registry key of most recent waiter.
Definition cond.h:71
atomic_t waiting_count
Number of threads currently waiting (functional - needed for all builds)
Definition cond.h:66
const char * last_wait_file
Callsite file of most recent cond_wait (for deadlock detection)
Definition cond.h:73
uint64_t signal_count
Total signal calls.
Definition cond.h:77
uint64_t wait_count
Total wait calls.
Definition cond.h:76
int last_wait_line
Callsite line of most recent cond_wait (for deadlock detection)
Definition cond.h:74
const char * last_wait_func
Callsite function of most recent cond_wait (for deadlock detection)
Definition cond.h:75
Mutex type (POSIX: pthread_mutex_t with debug tracking)
⏱️ High-precision timing utilities using sokol_time.h and uthash
#define LOG_DEBUG
Definition types.h:39