ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
mutex.h
Go to the documentation of this file.
1#pragma once
2
12
13/* Storage capacities in elements; text buffers include the terminating NUL. */
14#define DEBUG_MUTEX_SPAN_NAME_BUFFER_SIZE 224
15#define DEBUG_MUTEX_ERROR_MESSAGE_BUFFER_SIZE 1024
16
17/* Existing buffer budgets in bytes, including NUL for text storage. */
18#define DEBUG_MUTEX_TRACE_ID_SIZE 16
19#define DEBUG_MUTEX_SPAN_ID_SIZE 8
20
21#include <stdbool.h>
22#include <stddef.h>
23#include <stdint.h>
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
36typedef enum {
37 MUTEX_STACK_STATE_PENDING, // Attempting to acquire lock
38 MUTEX_STACK_STATE_LOCKED, // Lock successfully acquired
40
44typedef struct {
45 uintptr_t mutex_key; // Pointer to mutex_t (used as unique ID)
46 const char *mutex_name; // Human-readable mutex name
47 mutex_stack_state_t state; // PENDING or LOCKED
48 uint64_t timestamp_ns; // When this lock was attempted/acquired
50
56void mutex_stack_push_pending(uintptr_t mutex_key, const char *mutex_name);
57
60void mutex_stack_push_locked(uintptr_t mutex_key, const char *mutex_name);
61
67void mutex_stack_mark_locked(uintptr_t mutex_key);
68
73void mutex_stack_pop(uintptr_t mutex_key);
74
81int mutex_stack_get_current(mutex_stack_entry_t *out_entries, int max_entries);
82
95 int **out_stack_counts, int *out_thread_count);
96
101 int *stack_counts, int thread_count);
102
114
136bool mutex_stack_try_snapshot(mutex_wait_snapshot_t *waits, size_t capacity,
137 size_t *count, bool *limited);
138
143int mutex_stack_init(void);
144
148void mutex_stack_cleanup(void);
149
156
157#ifdef __cplusplus
158}
159#endif
160
unsigned long long uint64_t
unsigned char uint8_t
mutex_stack_state_t
mutex stack entry is intentionally a small, concrete record. Fields such as mutex_key,...
Definition mutex.h:36
@ MUTEX_STACK_STATE_LOCKED
Definition mutex.h:38
@ MUTEX_STACK_STATE_PENDING
Definition mutex.h:37
#define DEBUG_MUTEX_SPAN_ID_SIZE
Definition mutex.h:19
void mutex_stack_cleanup_current_thread(void)
Cleanup TLS stack for current thread Explicitly frees the thread-local mutex stack....
Definition mutex.c:1028
int mutex_stack_get_all_threads(mutex_stack_entry_t ***out_stacks, int **out_stack_counts, int *out_thread_count)
Get all threads' lock stacks for deadlock analysis.
Definition mutex.c:351
void mutex_stack_free_all_threads(mutex_stack_entry_t **stacks, int *stack_counts, int thread_count)
Free memory allocated by mutex_stack_get_all_threads().
Definition mutex.c:407
void mutex_stack_cleanup(void)
Cleanup mutex stack system.
Definition mutex.c:1064
int mutex_stack_get_current(mutex_stack_entry_t *out_entries, int max_entries)
Get the current thread's lock stack.
Definition mutex.c:333
void mutex_stack_pop(uintptr_t mutex_key)
Pop the top mutex from the current thread's lock stack (unlock).
Definition mutex.c:303
void mutex_stack_push_locked(uintptr_t mutex_key, const char *mutex_name)
Definition mutex.c:276
bool mutex_stack_try_snapshot(mutex_wait_snapshot_t *waits, size_t capacity, size_t *count, bool *limited)
Definition mutex.c:529
#define DEBUG_MUTEX_ERROR_MESSAGE_BUFFER_SIZE
Definition mutex.h:15
void mutex_stack_push_pending(uintptr_t mutex_key, const char *mutex_name)
Push a mutex onto the current thread's lock stack (PENDING state).
Definition mutex.c:272
#define DEBUG_MUTEX_SPAN_NAME_BUFFER_SIZE
Definition mutex.h:14
void mutex_stack_mark_locked(uintptr_t mutex_key)
Mark the top of the current thread's lock stack as LOCKED (transitions PENDING -> LOCKED).
Definition mutex.c:280
#define DEBUG_MUTEX_TRACE_ID_SIZE
Definition mutex.h:18
int mutex_stack_init(void)
Initialize mutex stack system.
Definition mutex.c:1020
void mutex_stack_detect_deadlocks(void)
Detect and log circular wait deadlocks.
Definition mutex.c:656
Definition mutex.h:44
const char * mutex_name
Definition mutex.h:46
mutex_stack_state_t state
Definition mutex.h:47
uint64_t timestamp_ns
Definition mutex.h:48
uintptr_t mutex_key
Definition mutex.h:45
For mutex wait snapshot, this is the live record that ties mutex_key, thread_key, since_ns,...
Definition mutex.h:122
uint64_t since_ns
Definition mutex.h:125
uint8_t trace_id[16]
Definition mutex.h:130
uintptr_t thread_key
Definition mutex.h:124
uint8_t span_id[8]
Definition mutex.h:131
uint64_t span_start_unix_ns
Definition mutex.h:133
char span_name[224]
Definition mutex.h:127
uintptr_t mutex_key
Definition mutex.h:123
uint8_t parent_span_id[8]
Definition mutex.h:132
char error_message[1024]
Definition mutex.h:129