ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
rwlock.h
Go to the documentation of this file.
1#pragma once
2
30
31#include <stdbool.h>
32#include <stdint.h>
34#include <libplatform/atomic.h>
35
36#ifdef _WIN32
37#include "windows_compat.h"
43typedef struct {
44 SRWLOCK impl;
45 const char *name;
46#ifndef NDEBUG
47 _Atomic(uint64_t) last_rdlock_time_ns;
48 _Atomic(uint64_t) last_wrlock_time_ns;
49 _Atomic(uint64_t) last_unlock_time_ns;
50 _Atomic(uintptr_t) write_held_by_key;
51 atomic_t read_lock_count;
52 _Atomic(uint64_t) rdlock_count;
53 _Atomic(uint64_t) wrlock_count;
54 _Atomic(uint64_t) unlock_count;
55#endif
56} rwlock_t;
57#else
58#include <pthread.h>
63typedef struct {
64 pthread_rwlock_t impl;
65 const char *name;
66#ifndef NDEBUG
67 _Atomic(uint64_t) last_rdlock_time_ns;
68 _Atomic(uint64_t) last_wrlock_time_ns;
69 _Atomic(uint64_t) last_unlock_time_ns;
70 _Atomic(uintptr_t) write_held_by_key;
71 atomic_t read_lock_count;
72 _Atomic(uint64_t) rdlock_count;
73 _Atomic(uint64_t) wrlock_count;
74 _Atomic(uint64_t) unlock_count;
75#endif
76} rwlock_t;
77#endif
78
79// Forward declarations for lock debugging
80// Note: Full lock_debug.h cannot be included here due to circular dependencies
81// Files that use both rwlock and lock_debug must include lock_debug.h separately
82#ifndef NDEBUG
83#ifdef __cplusplus
84extern "C" {
85#endif
86
88int debug_sync_rwlock_rdlock(rwlock_t *rwlock, const char *file_name, int line_number, const char *function_name);
89int debug_sync_rwlock_wrlock(rwlock_t *rwlock, const char *file_name, int line_number, const char *function_name);
90int debug_sync_rwlock_rdunlock(rwlock_t *rwlock, const char *file_name, int line_number, const char *function_name);
91int debug_sync_rwlock_wrunlock(rwlock_t *rwlock, const char *file_name, int line_number, const char *function_name);
92
93#ifdef __cplusplus
94}
95#endif
96#endif
97
98#ifdef __cplusplus
99extern "C" {
100#endif
101
102// ============================================================================
103// Read-Write Lock Functions
104// ============================================================================
105
118int rwlock_init(rwlock_t *lock, const char *name);
119
131
141#ifndef NDEBUG
143#else
144#define rwlock_on_rdlock(rwlock) ((void)0)
145#endif
146
156#ifndef NDEBUG
158#else
159#define rwlock_on_wrlock(rwlock) ((void)0)
160#endif
161
171#ifndef NDEBUG
173#else
174#define rwlock_on_unlock(rwlock) ((void)0)
175#endif
176
189#ifndef NDEBUG
190void rwlock_log_state(const rwlock_t *rwlock, const char *file, int line, const char *func);
191#define LOG_RWLOCK_STATE(rwlock) rwlock_log_state(rwlock, __FILE__, __LINE__, __func__)
192#else
193#define LOG_RWLOCK_STATE(rwlock) ((void)0)
194#endif
195
206
217
232
235
250
264
278
283
296#ifdef NDEBUG
297#define rwlock_rdlock(lock) rwlock_rdlock_impl(lock)
298#else
299#define rwlock_rdlock(lock) \
300 (debug_sync_is_initialized() ? debug_sync_rwlock_rdlock(lock, __FILE__, __LINE__, __func__) \
301 : rwlock_rdlock_impl(lock))
302#endif
303
316#ifdef NDEBUG
317#define rwlock_wrlock(lock) rwlock_wrlock_impl(lock)
318#else
319#define rwlock_wrlock(lock) \
320 (debug_sync_is_initialized() ? debug_sync_rwlock_wrlock(lock, __FILE__, __LINE__, __func__) \
321 : rwlock_wrlock_impl(lock))
322#endif
323
335#ifdef NDEBUG
336#define rwlock_rdunlock(lock) rwlock_rdunlock_impl(lock)
337#else
338#define rwlock_rdunlock(lock) \
339 (debug_sync_is_initialized() ? debug_sync_rwlock_rdunlock(lock, __FILE__, __LINE__, __func__) \
340 : rwlock_rdunlock_impl(lock))
341#endif
342
354#ifdef NDEBUG
355#define rwlock_wrunlock(lock) rwlock_wrunlock_impl(lock)
356#else
357#define rwlock_wrunlock(lock) \
358 (debug_sync_is_initialized() ? debug_sync_rwlock_wrunlock(lock, __FILE__, __LINE__, __func__) \
359 : rwlock_wrunlock_impl(lock))
360#endif
361
362#ifdef __cplusplus
363}
364#endif
365
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
unsigned long long uint64_t
Definition common.h:63
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
int debug_sync_rwlock_rdunlock(rwlock_t *rwlock, const char *file_name, int line_number, const char *function_name)
asciichat_error_t rwlock_tryrdlock(rwlock_t *lock, bool *acquired)
const char * name
Human-readable name for named registry (all builds).
Definition rwlock.h:65
int rwlock_rdunlock_impl(rwlock_t *lock)
Release a read lock (implementation function).
int debug_sync_rwlock_wrunlock(rwlock_t *rwlock, const char *file_name, int line_number, const char *function_name)
int rwlock_init(rwlock_t *lock, const char *name)
Initialize a read-write lock with a name.
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)
Format and log the current state of a read-write lock.
_Atomic(uint64_t) last_rdlock_time_ns
Timestamp of last read lock acquisition (nanoseconds).
void rwlock_on_unlock(rwlock_t *rwlock)
Hook called when an rwlock is unlocked (read or write).
int debug_sync_rwlock_wrlock(rwlock_t *rwlock, const char *file_name, int line_number, const char *function_name)
int rwlock_rdlock_impl(rwlock_t *lock)
Acquire a read lock (implementation function).
pthread_rwlock_t impl
Underlying POSIX rwlock.
Definition rwlock.h:64
int rwlock_wrunlock_impl(rwlock_t *lock)
Release a write lock (implementation function).
int rwlock_wrlock_impl(rwlock_t *lock)
Acquire a write lock (implementation function).
void rwlock_on_rdlock(rwlock_t *rwlock)
Hook called when a read lock is successfully acquired.
int rwlock_destroy_impl(rwlock_t *lock)
Destroy a read-write lock (implementation function).
bool debug_sync_is_initialized(void)
int debug_sync_rwlock_rdlock(rwlock_t *rwlock, const char *file_name, int line_number, const char *function_name)
void rwlock_on_wrlock(rwlock_t *rwlock)
Hook called when a write lock is successfully acquired.
_Atomic(uintptr_t) write_held_by_key
Registry key of thread holding write lock (0 if not held).
int rwlock_init_impl(rwlock_t *lock)
Initialize a read-write lock (implementation function).
atomic_t read_lock_count
Number of threads holding read locks (thread-safe atomic).
Definition rwlock.h:71
Read-write lock type (POSIX: pthread_rwlock_t with debug tracking).
Definition rwlock.h:63
_Atomic(uint64_t) last_wrlock_time_ns
Timestamp of last write lock acquisition (nanoseconds).
rwlock_t rwlock
Read-write lock for thread-safe access (uthash requires external locking).
Definition time.c:39