ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
errno.c
Go to the documentation of this file.
5#include <libplatform/atomic.h>
6#include <libplatform/cond.h>
7#include <libplatform/thread.h>
8#include <options/options.h>
9
10static atomic_t g_debug_errno_signal = {0};
11static atomic_t g_debug_errno_exiting = {0};
12static atomic_t g_debug_errno_cleaning = {0};
13static platform_thread_t g_debug_errno_thread;
14static mutex_t g_debug_errno_mutex;
15static cond_t g_debug_errno_condition;
16static bool g_debug_errno_initialized;
17static bool g_debug_errno_started;
18static uint64_t g_debug_errno_errno_deadline;
19
21 if (g_debug_errno_initialized)
22 return ASCIICHAT_OK;
23 if (mutex_init(&g_debug_errno_mutex, "debug_errno") != 0)
24 return SET_ERRNO(ERROR_THREAD, "Cannot initialize diagnostics mutex");
25 if (cond_init(&g_debug_errno_condition, "debug_errno") != 0) {
26 mutex_destroy(&g_debug_errno_mutex);
27 return SET_ERRNO(ERROR_THREAD, "Cannot initialize diagnostics condition");
28 }
29 g_debug_errno_initialized = true;
30 atomic_store_bool_impl(&g_debug_errno_cleaning, false);
31 return ASCIICHAT_OK;
32}
33static void debug_errno_print_errno(void) {
34 log_info("Errno stacks:");
38}
39void debug_errno_print(void) { debug_errno_print_errno(); }
41 if (!g_debug_errno_initialized)
42 return;
43 debug_report_schedule(&g_debug_errno_mutex, &g_debug_errno_condition,
44 &g_debug_errno_errno_deadline, delay);
45}
47 // Do not signal a condition variable or invoke diagnostic hooks in a signal
48 // handler. The worker checks this flag at most 100ms after its next wakeup.
49 atomic_store_bool_impl(&g_debug_errno_signal, true);
50}
52 return atomic_load_bool_impl(&g_debug_errno_cleaning);
53}
54void debug_errno_poll(void) {
55 if (!g_debug_errno_initialized)
56 return;
57 uint64_t now = time_get_ns();
58 bool all = atomic_exchange_bool_impl(&g_debug_errno_signal, false);
59 mutex_lock(&g_debug_errno_mutex);
60 bool errors =
61 g_debug_errno_errno_deadline && now >= g_debug_errno_errno_deadline;
62 if (errors)
63 g_debug_errno_errno_deadline = 0;
64 mutex_unlock(&g_debug_errno_mutex);
65 if (all || errors)
66 debug_errno_print_errno();
67}
68#ifndef EMSCRIPTEN_BUILD
69static void *debug_errno_worker(void *unused) {
70 (void)unused;
71 while (!atomic_load_bool_impl(&g_debug_errno_exiting)) {
73 mutex_lock(&g_debug_errno_mutex);
74 uint64_t now = time_get_ns();
75 uint64_t wait = 100 * NS_PER_MS_INT;
76 uint64_t deadlines[] = {g_debug_errno_errno_deadline};
77 for (size_t i = 0; i < sizeof(deadlines) / sizeof(deadlines[0]); ++i)
78 if (deadlines[i]) {
79 uint64_t remaining = deadlines[i] > now ? deadlines[i] - now : 1;
80 if (remaining < wait)
81 wait = remaining;
82 }
83 if (!atomic_load_bool_impl(&g_debug_errno_exiting))
84 cond_timedwait(&g_debug_errno_condition, &g_debug_errno_mutex, wait);
85 mutex_unlock(&g_debug_errno_mutex);
86 }
87 return NULL;
88}
89#endif
91 if (g_debug_errno_started)
92 return ASCIICHAT_OK;
94 if (result != ASCIICHAT_OK)
95 return result;
96 atomic_store_bool_impl(&g_debug_errno_exiting, false);
97 atomic_store_bool_impl(&g_debug_errno_cleaning, false);
98 const options_t *opts = options_get();
99 if (opts) {
100 if (opts->debug_errno_stacks_time > 0.0)
103 }
104#ifndef EMSCRIPTEN_BUILD
105 if (platform_thread_create(&g_debug_errno_thread, "debug_errno",
106 debug_errno_worker, NULL) != 0)
107 return SET_ERRNO(ERROR_THREAD, "Cannot start diagnostics worker");
108#endif
109 g_debug_errno_started = true;
110 return ASCIICHAT_OK;
111}
113 if (!g_debug_errno_started)
114 return;
115 atomic_store_bool_impl(&g_debug_errno_cleaning, true);
116 mutex_lock(&g_debug_errno_mutex);
117 atomic_store_bool_impl(&g_debug_errno_exiting, true);
118 cond_signal(&g_debug_errno_condition);
119 mutex_unlock(&g_debug_errno_mutex);
120#ifndef EMSCRIPTEN_BUILD
121 // Join fully: diagnostic data must not be destroyed while a reader is alive.
122 if (thread_join(&g_debug_errno_thread, NULL) != 0)
123 FATAL(ERROR_THREAD, "Cannot join diagnostics worker");
124#endif
125 g_debug_errno_started = false;
126}
129 if (g_debug_errno_initialized) {
130 cond_destroy(&g_debug_errno_condition);
131 mutex_destroy(&g_debug_errno_mutex);
132 g_debug_errno_initialized = false;
133 atomic_store_bool_impl(&g_debug_errno_signal, false);
134 g_debug_errno_errno_deadline = 0;
135 }
136}
void errno_print_history(void)
void errno_print_hash_stats(void)
void errno_print_stacks(void)
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
bool atomic_load_bool_impl(const atomic_t *a)
void atomic_store_bool_impl(atomic_t *a, uint64_t value)
bool atomic_exchange_bool_impl(atomic_t *a, bool new_value)
void debug_report_schedule(mutex_t *mutex, cond_t *condition, uint64_t *deadline, uint64_t delay_ns)
Shared deadline and scheduling helpers for diagnostic reports.
bool debug_errno_is_cleanup_in_progress(void)
Definition errno.c:51
asciichat_error_t debug_errno_init(void)
Definition errno.c:20
void debug_errno_print(void)
Definition errno.c:39
void debug_errno_trigger_print(void)
Definition errno.c:46
void debug_errno_poll(void)
Definition errno.c:54
void debug_errno_print_delayed(uint64_t delay)
Definition errno.c:40
void debug_errno_destroy(void)
Definition errno.c:127
asciichat_error_t debug_errno_start_thread(void)
Definition errno.c:90
void debug_errno_cleanup_thread(void)
Definition errno.c:112
Dedicated worker for all pending errno stacks and handled history. Signal handlers may only request a...
#define FATAL(code,...)
Exit with error code and custom message, with stack trace in debug builds.
unsigned long long uint64_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_THREAD
#define log_info(...)
Log an INFO message.
Definition log/log.h:578
#define NS_PER_SEC_INT
Definition time.h:173
#define NS_PER_MS_INT
Definition time.h:172
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.
const options_t * options_get(void)
Get pointer to current options struct (lock-free, thread-safe).
Definition rcu.c:513
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
Atomic value wrapper for integral/boolean types.
Definition atomic.h:80
double debug_errno_stacks_time
Delay before the error report.
Definition options.h:1237
⏱️ High-precision timing utilities using sokol_time.h and uthash