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 File Reference

Go to the source code of this file.

Functions

asciichat_error_t debug_errno_init (void)
void debug_errno_print (void)
void debug_errno_print_delayed (uint64_t delay)
void debug_errno_trigger_print (void)
bool debug_errno_is_cleanup_in_progress (void)
void debug_errno_poll (void)
asciichat_error_t debug_errno_start_thread (void)
void debug_errno_cleanup_thread (void)
void debug_errno_destroy (void)

Function Documentation

◆ debug_errno_cleanup_thread()

void debug_errno_cleanup_thread ( void )

Definition at line 112 of file errno.c.

112 {
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}
void atomic_store_bool_impl(atomic_t *a, uint64_t value)
#define FATAL(code,...)
Exit with error code and custom message, with stack trace in debug builds.
@ ERROR_THREAD

References atomic_store_bool_impl(), ERROR_THREAD, and FATAL.

Referenced by asciichat_shared_destroy(), and debug_errno_destroy().

◆ debug_errno_destroy()

void debug_errno_destroy ( void )

Definition at line 127 of file errno.c.

127 {
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 debug_errno_cleanup_thread(void)
Definition errno.c:112

References atomic_store_bool_impl(), and debug_errno_cleanup_thread().

Referenced by asciichat_shared_destroy().

◆ debug_errno_init()

asciichat_error_t debug_errno_init ( void )

Definition at line 20 of file errno.c.

20 {
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}
#define SET_ERRNO(code,...)
@ ASCIICHAT_OK
Definition error_codes.h:50

References ASCIICHAT_OK, atomic_store_bool_impl(), ERROR_THREAD, and SET_ERRNO.

Referenced by debug_errno_start_thread().

◆ debug_errno_is_cleanup_in_progress()

bool debug_errno_is_cleanup_in_progress ( void )

Definition at line 51 of file errno.c.

51 {
52 return atomic_load_bool_impl(&g_debug_errno_cleaning);
53}
bool atomic_load_bool_impl(const atomic_t *a)

References atomic_load_bool_impl().

◆ debug_errno_poll()

void debug_errno_poll ( void )

Definition at line 54 of file errno.c.

54 {
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}
bool atomic_exchange_bool_impl(atomic_t *a, bool new_value)
unsigned long long uint64_t

References atomic_exchange_bool_impl().

◆ debug_errno_print()

void debug_errno_print ( void )

Definition at line 39 of file errno.c.

39{ debug_errno_print_errno(); }

◆ debug_errno_print_delayed()

void debug_errno_print_delayed ( uint64_t delay)

Definition at line 40 of file errno.c.

40 {
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}
void debug_report_schedule(mutex_t *mutex, cond_t *condition, uint64_t *deadline, uint64_t delay_ns)

References debug_report_schedule().

Referenced by debug_errno_start_thread().

◆ debug_errno_start_thread()

asciichat_error_t debug_errno_start_thread ( void )

Definition at line 90 of file errno.c.

90 {
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}
asciichat_error_t debug_errno_init(void)
Definition errno.c:20
void debug_errno_print_delayed(uint64_t delay)
Definition errno.c:40
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
#define NS_PER_SEC_INT
Definition time.h:173
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
double debug_errno_stacks_time
Delay before the error report.
Definition options.h:1237

References ASCIICHAT_OK, atomic_store_bool_impl(), debug_errno_init(), debug_errno_print_delayed(), options_state::debug_errno_stacks_time, ERROR_THREAD, NS_PER_SEC_INT, options_get(), and SET_ERRNO.

◆ debug_errno_trigger_print()

void debug_errno_trigger_print ( void )

Definition at line 46 of file errno.c.

46 {
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}

References atomic_store_bool_impl().