ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
platform/mutex.c
Go to the documentation of this file.
1
14#include <ascii-chat/log/log.h>
15#include <stdint.h>
16#include <stdio.h>
17
18#ifndef NDEBUG
19
37
54
65void mutex_on_trylock(mutex_t *mutex, bool success) {
66 if (!mutex)
67 return;
69 if (success) {
74 }
75}
76
89int mutex_format_state(const mutex_t *mutex, char *buffer, size_t size) {
90 if (!mutex || !buffer || size == 0)
91 return 0;
92
93 int offset = 0;
94 uint64_t now_ns = time_get_ns();
95
96 // Format timing information
97 char lock_str[64] = "";
98 char unlock_str[64] = "";
99 char held_str[64] = "";
100 char count_str[256] = "";
101
102 if (mutex->last_lock_time_ns > 0 && mutex->last_lock_time_ns <= now_ns) {
103 char elapsed_str[64];
104 time_pretty(now_ns - mutex->last_lock_time_ns, -1, elapsed_str, sizeof(elapsed_str));
105 snprintf(lock_str, sizeof(lock_str), "lock=%s", elapsed_str);
106 }
107
108 if (mutex->last_unlock_time_ns > 0 && mutex->last_unlock_time_ns <= now_ns) {
109 char elapsed_str[64];
110 time_pretty(now_ns - mutex->last_unlock_time_ns, -1, elapsed_str, sizeof(elapsed_str));
111 snprintf(unlock_str, sizeof(unlock_str), "unlock=%s", elapsed_str);
112 }
113
114 if (mutex->currently_held_by_key != 0) {
115 snprintf(held_str, sizeof(held_str), "[LOCKED_BY=thread.%lu]", (unsigned long)mutex->currently_held_by_key);
116 }
117
118 if (mutex->lock_count > 0 || mutex->unlock_count > 0 || mutex->trylock_count > 0) {
119 snprintf(count_str, sizeof(count_str), "[ops: lock=%llu unlock=%llu trylock=%llu/%llu]",
120 (unsigned long long)mutex->lock_count, (unsigned long long)mutex->unlock_count,
121 (unsigned long long)mutex->trylock_success_count, (unsigned long long)mutex->trylock_count);
122 }
123
124 offset += snprintf(buffer + offset, size - offset, "%s %s %s %s", lock_str, unlock_str, held_str, count_str);
125 return offset;
126}
127
139void mutex_log_state(const mutex_t *mutex, const char *file, int line, const char *func) {
140 if (!mutex)
141 return;
142 char buf[512];
143 mutex_format_state(mutex, buf, sizeof(buf));
144 log_msg(LOG_DEBUG, file, line, func, "mutex/state %p: %s", (const void *)mutex, buf);
145}
146
147#endif // !NDEBUG
mutex_t mutex
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 mutex_on_unlock(mutex_t *mutex)
Hook called when a mutex is unlocked.
int mutex_format_state(const mutex_t *mutex, char *buffer, size_t size)
Format mutex timing and state info into buffer.
void mutex_on_trylock(mutex_t *mutex, bool success)
Hook called when a mutex trylock is attempted.
void mutex_log_state(const mutex_t *mutex, const char *file, int line, const char *func)
Log the state of a mutex.
void mutex_on_lock(mutex_t *mutex)
Hook called when a mutex is successfully locked.
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
Cross-platform mutex interface for ascii-chat.
Mutex type (POSIX: pthread_mutex_t with debug tracking)
uintptr_t currently_held_by_key
Registry key of thread holding the lock (0 if free)
uint64_t last_lock_time_ns
Timestamp of last lock acquisition (nanoseconds)
uint64_t unlock_count
Total unlocks.
uint64_t last_unlock_time_ns
Timestamp of last unlock (nanoseconds)
uint64_t trylock_success_count
Successful trylocks.
uint64_t trylock_count
Total trylock attempts.
uint64_t lock_count
Total lock acquisitions.
⏱️ High-precision timing utilities using sokol_time.h and uthash
#define LOG_DEBUG
Definition types.h:39