ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
rwlock.h
Go to the documentation of this file.
1#pragma once
2
31#include <stdbool.h>
32#include <stdint.h>
33#include <ascii-chat/atomic.h>
34
35#ifdef _WIN32
36#include "windows_compat.h"
41typedef struct {
42 SRWLOCK impl;
43 const char *name;
44#ifndef NDEBUG
45 uint64_t last_rdlock_time_ns;
46 uint64_t last_wrlock_time_ns;
47 uint64_t last_unlock_time_ns;
48 uintptr_t write_held_by_key;
49 atomic_t read_lock_count;
50 uint64_t rdlock_count;
51 uint64_t wrlock_count;
52 uint64_t unlock_count;
53#endif
54} rwlock_t;
55#else
56#include <pthread.h>
75#endif
76
77// Forward declarations for lock debugging
78// Note: Full lock_debug.h cannot be included here due to circular dependencies
79// Files that use both rwlock and lock_debug must include lock_debug.h separately
80#ifndef NDEBUG
81#ifdef __cplusplus
82extern "C" {
83#endif
84
86int debug_sync_rwlock_rdlock(rwlock_t *rwlock, const char *file_name, int line_number, const char *function_name);
87int debug_sync_rwlock_wrlock(rwlock_t *rwlock, const char *file_name, int line_number, const char *function_name);
88int debug_sync_rwlock_rdunlock(rwlock_t *rwlock, const char *file_name, int line_number, const char *function_name);
89int debug_sync_rwlock_wrunlock(rwlock_t *rwlock, const char *file_name, int line_number, const char *function_name);
90
91#ifdef __cplusplus
92}
93#endif
94#endif
95
96#ifdef __cplusplus
97extern "C" {
98#endif
99
100// ============================================================================
101// Read-Write Lock Functions
102// ============================================================================
103
116int rwlock_init(rwlock_t *lock, const char *name);
117
129
139#ifndef NDEBUG
141#else
142#define rwlock_on_rdlock(rwlock) ((void)0)
143#endif
144
154#ifndef NDEBUG
156#else
157#define rwlock_on_wrlock(rwlock) ((void)0)
158#endif
159
169#ifndef NDEBUG
171#else
172#define rwlock_on_unlock(rwlock) ((void)0)
173#endif
174
187#ifndef NDEBUG
188void rwlock_log_state(const rwlock_t *rwlock, const char *file, int line, const char *func);
189#define LOG_RWLOCK_STATE(rwlock) rwlock_log_state(rwlock, __FILE__, __LINE__, __func__)
190#else
191#define LOG_RWLOCK_STATE(rwlock) ((void)0)
192#endif
193
204
215
230
245
259
273
291#ifdef NDEBUG
292#define rwlock_rdlock(lock) rwlock_rdlock_impl(lock)
293#else
294#define rwlock_rdlock(lock) \
295 (debug_sync_is_initialized() ? debug_sync_rwlock_rdlock(lock, __FILE__, __LINE__, __func__) : rwlock_rdlock_impl(lock))
296#endif
297
310#ifdef NDEBUG
311#define rwlock_wrlock(lock) rwlock_wrlock_impl(lock)
312#else
313#define rwlock_wrlock(lock) \
314 (debug_sync_is_initialized() ? debug_sync_rwlock_wrlock(lock, __FILE__, __LINE__, __func__) : rwlock_wrlock_impl(lock))
315#endif
316
328#ifdef NDEBUG
329#define rwlock_rdunlock(lock) rwlock_rdunlock_impl(lock)
330#else
331#define rwlock_rdunlock(lock) \
332 (debug_sync_is_initialized() ? debug_sync_rwlock_rdunlock(lock, __FILE__, __LINE__, __func__) : rwlock_rdunlock_impl(lock))
333#endif
334
346#ifdef NDEBUG
347#define rwlock_wrunlock(lock) rwlock_wrunlock_impl(lock)
348#else
349#define rwlock_wrunlock(lock) \
350 (debug_sync_is_initialized() ? debug_sync_rwlock_wrunlock(lock, __FILE__, __LINE__, __func__) : rwlock_wrunlock_impl(lock))
351#endif
352
353#ifdef __cplusplus
354}
355#endif
356
⚛️ Atomic operations abstraction layer with debug tracking
unsigned long long uint64_t
Definition common.h:59
int debug_sync_rwlock_rdunlock(rwlock_t *rwlock, const char *file_name, int line_number, const char *function_name)
Definition sync.c:765
uint64_t wrlock_count
Total write lock acquisitions.
Definition rwlock.h:71
const char * name
Human-readable name for named registry (all builds)
Definition rwlock.h:63
int rwlock_rdunlock_impl(rwlock_t *lock)
Release a read lock (implementation function)
Definition threading.c:78
int debug_sync_rwlock_wrunlock(rwlock_t *rwlock, const char *file_name, int line_number, const char *function_name)
Definition sync.c:779
int rwlock_init(rwlock_t *lock, const char *name)
Initialize a read-write lock with a name.
Definition threading.c:65
int rwlock_destroy(rwlock_t *lock)
Destroy a read-write lock.
void rwlock_log_state(const rwlock_t *rwlock, const char *file, int line, const char *func)
Log the state of a read-write lock.
Definition rwlock.c:150
void rwlock_on_unlock(rwlock_t *rwlock)
Hook called when an rwlock is unlocked (read or write)
Definition rwlock.c:64
int debug_sync_rwlock_wrlock(rwlock_t *rwlock, const char *file_name, int line_number, const char *function_name)
Definition sync.c:772
uintptr_t write_held_by_key
Registry key of thread holding write lock (0 if not held)
Definition rwlock.h:68
int rwlock_rdlock_impl(rwlock_t *lock)
Acquire a read lock (implementation function)
Definition threading.c:70
pthread_rwlock_t impl
Underlying POSIX rwlock.
Definition rwlock.h:62
uint64_t last_unlock_time_ns
Timestamp of last unlock (nanoseconds)
Definition rwlock.h:67
int rwlock_wrunlock_impl(rwlock_t *lock)
Release a write lock (implementation function)
Definition threading.c:82
int rwlock_wrlock_impl(rwlock_t *lock)
Acquire a write lock (implementation function)
Definition threading.c:74
void rwlock_on_rdlock(rwlock_t *rwlock)
Hook called when a read lock is successfully acquired.
Definition rwlock.c:29
int rwlock_destroy_impl(rwlock_t *lock)
Destroy a read-write lock (implementation function)
bool debug_sync_is_initialized(void)
Definition sync.c:820
int debug_sync_rwlock_rdlock(rwlock_t *rwlock, const char *file_name, int line_number, const char *function_name)
Definition sync.c:758
uint64_t unlock_count
Total unlocks.
Definition rwlock.h:72
uint64_t last_wrlock_time_ns
Timestamp of last write lock acquisition (nanoseconds)
Definition rwlock.h:66
void rwlock_on_wrlock(rwlock_t *rwlock)
Hook called when a write lock is successfully acquired.
Definition rwlock.c:46
uint64_t last_rdlock_time_ns
Timestamp of last read lock acquisition (nanoseconds)
Definition rwlock.h:65
int rwlock_init_impl(rwlock_t *lock)
Initialize a read-write lock (implementation function)
uint64_t rdlock_count
Total read lock acquisitions.
Definition rwlock.h:70
atomic_t read_lock_count
Number of threads holding read locks (thread-safe atomic)
Definition rwlock.h:69
ClangTool/LibTooling compatibility shim for stdbool.h.
Atomic value wrapper for integral/boolean types.
Definition atomic.h:76
Read-write lock type (POSIX: pthread_rwlock_t with debug tracking)
Definition rwlock.h:61
rwlock_t rwlock
Read-write lock for thread-safe access (uthash requires external locking)
Definition util/time.c:34
Wrapper for windows.h with C23 alignment compatibility.