ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
mutex.h
Go to the documentation of this file.
1#pragma once
2
11
12/* Storage capacities in elements; text buffers include the terminating NUL. */
13#define DEBUG_MUTEX_SPAN_NAME_BUFFER_SIZE 224
14#define DEBUG_MUTEX_ERROR_MESSAGE_BUFFER_SIZE 1024
15
16/* Existing buffer budgets in bytes, including NUL for text storage. */
17#define DEBUG_MUTEX_TRACE_ID_SIZE 16
18#define DEBUG_MUTEX_SPAN_ID_SIZE 8
19
20#include <stdint.h>
21#include <stddef.h>
22#include <stdbool.h>
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
32typedef enum {
33 MUTEX_STACK_STATE_PENDING, // Attempting to acquire lock
34 MUTEX_STACK_STATE_LOCKED, // Lock successfully acquired
36
40typedef struct {
41 uintptr_t mutex_key; // Pointer to mutex_t (used as unique ID)
42 const char *mutex_name; // Human-readable mutex name
43 mutex_stack_state_t state; // PENDING or LOCKED
44 uint64_t timestamp_ns; // When this lock was attempted/acquired
46
52void mutex_stack_push_pending(uintptr_t mutex_key, const char *mutex_name);
53
55void mutex_stack_push_locked(uintptr_t mutex_key, const char *mutex_name);
56
62void mutex_stack_mark_locked(uintptr_t mutex_key);
63
68void mutex_stack_pop(uintptr_t mutex_key);
69
76int mutex_stack_get_current(mutex_stack_entry_t *out_entries, int max_entries);
77
88int mutex_stack_get_all_threads(mutex_stack_entry_t ***out_stacks, int **out_stack_counts, int *out_thread_count);
89
93void mutex_stack_free_all_threads(mutex_stack_entry_t **stacks, int *stack_counts, int thread_count);
94
106
124bool mutex_stack_try_snapshot(mutex_wait_snapshot_t *waits, size_t capacity, size_t *count, bool *limited);
125
130int mutex_stack_init(void);
131
135void mutex_stack_cleanup(void);
136
143
144#ifdef __cplusplus
145}
146#endif
147
unsigned long long uint64_t
Definition common.h:63
unsigned char uint8_t
Definition common.h:60
mutex_stack_state_t
mutex stack entry is intentionally a small, concrete record. Fields such as mutex_key,...
Definition mutex.h:32
@ MUTEX_STACK_STATE_LOCKED
Definition mutex.h:34
@ MUTEX_STACK_STATE_PENDING
Definition mutex.h:33
#define DEBUG_MUTEX_SPAN_ID_SIZE
Definition mutex.h:18
void mutex_stack_cleanup_current_thread(void)
Cleanup TLS stack for current thread Explicitly frees the thread-local mutex stack....
Definition mutex.c:947
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:339
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:389
void mutex_stack_cleanup(void)
Cleanup mutex stack system.
Definition mutex.c:980
int mutex_stack_get_current(mutex_stack_entry_t *out_entries, int max_entries)
Get the current thread's lock stack.
Definition mutex.c:321
void mutex_stack_pop(uintptr_t mutex_key)
Pop the top mutex from the current thread's lock stack (unlock).
Definition mutex.c:293
void mutex_stack_push_locked(uintptr_t mutex_key, const char *mutex_name)
Definition mutex.c:267
bool mutex_stack_try_snapshot(mutex_wait_snapshot_t *waits, size_t capacity, size_t *count, bool *limited)
Definition mutex.c:507
#define DEBUG_MUTEX_ERROR_MESSAGE_BUFFER_SIZE
Definition mutex.h:14
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:263
#define DEBUG_MUTEX_SPAN_NAME_BUFFER_SIZE
Definition mutex.h:13
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:271
#define DEBUG_MUTEX_TRACE_ID_SIZE
Definition mutex.h:17
int mutex_stack_init(void)
Initialize mutex stack system.
Definition mutex.c:939
void mutex_stack_detect_deadlocks(void)
Detect and log circular wait deadlocks.
Definition mutex.c:630
Definition mutex.h:40
const char * mutex_name
Definition mutex.h:42
mutex_stack_state_t state
Definition mutex.h:43
uint64_t timestamp_ns
Definition mutex.h:44
uintptr_t mutex_key
Definition mutex.h:41
For mutex wait snapshot, this is the live record that ties mutex_key, thread_key, since_ns,...
Definition mutex.h:111
uint64_t since_ns
Definition mutex.h:114
uint8_t trace_id[16]
Definition mutex.h:119
uintptr_t thread_key
Definition mutex.h:113
uint8_t span_id[8]
Definition mutex.h:119
uint64_t span_start_unix_ns
Definition mutex.h:121
char span_name[224]
Definition mutex.h:116
uintptr_t mutex_key
Definition mutex.h:112
uint8_t parent_span_id[8]
Definition mutex.h:120
char error_message[1024]
Definition mutex.h:118