ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
errno.h File Reference

Dedicated worker for all pending errno stacks and handled history. Signal handlers may only request a report. More...

Go to the source code of this file.

Functions

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

Detailed Description

Dedicated worker for all pending errno stacks and handled history. Signal handlers may only request a report.

Definition in file debug/errno.h.

Function Documentation

◆ debug_errno_cleanup_thread()

void debug_errno_cleanup_thread ( void )

Definition at line 110 of file errno.c.

110 {
111 if (!g_debug_errno_started)
112 return;
113 atomic_store_bool_impl(&g_debug_errno_cleaning, true);
114 mutex_lock(&g_debug_errno_mutex);
115 atomic_store_bool_impl(&g_debug_errno_exiting, true);
116 cond_signal(&g_debug_errno_condition);
117 mutex_unlock(&g_debug_errno_mutex);
118#ifndef EMSCRIPTEN_BUILD
119 // Join fully: diagnostic data must not be destroyed while a reader is alive.
120 if (thread_join(&g_debug_errno_thread, NULL) != 0)
121 FATAL(ERROR_THREAD, "Cannot join diagnostics worker");
122#endif
123 g_debug_errno_started = false;
124}
#define FATAL(code,...)
Exit with error code and custom message, with stack trace in debug builds.
Definition common.h:160
@ ERROR_THREAD

References ERROR_THREAD, and FATAL.

Referenced by asciichat_shared_destroy(), and debug_errno_destroy().

◆ debug_errno_destroy()

void debug_errno_destroy ( void )

Definition at line 125 of file errno.c.

125 {
127 if (g_debug_errno_initialized) {
128 cond_destroy(&g_debug_errno_condition);
129 mutex_destroy(&g_debug_errno_mutex);
130 g_debug_errno_initialized = false;
131 atomic_store_bool_impl(&g_debug_errno_signal, false);
132 g_debug_errno_errno_deadline = 0;
133 }
134}
void debug_errno_cleanup_thread(void)
Definition errno.c:110

References 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:46

References ASCIICHAT_OK, 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 52 of file errno.c.

52 {
53 return atomic_load_bool_impl(&g_debug_errno_cleaning);
54}

◆ debug_errno_poll()

void debug_errno_poll ( void )

Definition at line 55 of file errno.c.

55 {
56 if (!g_debug_errno_initialized)
57 return;
58 uint64_t now = time_get_ns();
59 bool all = atomic_exchange_bool_impl(&g_debug_errno_signal, false);
60 mutex_lock(&g_debug_errno_mutex);
61 bool errors = 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}
unsigned long long uint64_t
Definition common.h:63

◆ debug_errno_print()

void debug_errno_print ( void )

Definition at line 39 of file errno.c.

39 {
40 debug_errno_print_errno();
41}

◆ debug_errno_print_delayed()

void debug_errno_print_delayed ( uint64_t delay_ns)

Definition at line 42 of file errno.c.

42 {
43 if (!g_debug_errno_initialized)
44 return;
45 debug_report_schedule(&g_debug_errno_mutex, &g_debug_errno_condition, &g_debug_errno_errno_deadline, delay);
46}
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 options_t *opts = options_get();
99 if (opts) {
100 if (IS_OPTION_EXPLICIT(debug_errno_stacks_time, opts))
102 }
103#ifndef EMSCRIPTEN_BUILD
104 if (platform_thread_create(&g_debug_errno_thread, "debug_errno", debug_errno_worker, NULL) != 0)
105 return SET_ERRNO(ERROR_THREAD, "Cannot start diagnostics worker");
106#endif
107 g_debug_errno_started = true;
108 return ASCIICHAT_OK;
109}
asciichat_error_t debug_errno_init(void)
Definition errno.c:20
void debug_errno_print_delayed(uint64_t delay)
Definition errno.c:42
#define IS_OPTION_EXPLICIT(name, opts)
Definition explicit.h:57
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
#define NS_PER_SEC_INT
Definition time.h:169
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:511
double debug_errno_stacks_time
Delay before the error report.
Definition options.h:1246

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

Referenced by main().

◆ debug_errno_trigger_print()

void debug_errno_trigger_print ( void )

Definition at line 47 of file errno.c.

47 {
48 // Do not signal a condition variable or invoke diagnostic hooks in a signal
49 // handler. The worker checks this flag at most 100ms after its next wakeup.
50 atomic_store_bool_impl(&g_debug_errno_signal, true);
51}