|
CX Framework
Cross-platform C utility framework
|
Data Structures | |
| struct | RWLock |
Macros | |
| #define | RWLOCK_READER_MAX 4095 |
| Maximum number of concurrent readers. | |
| #define | RWLOCK_READWAIT_MAX 2047 |
| Maximum number of waiting readers. | |
| #define | RWLOCK_WRITER_MAX 511 |
| Maximum number of writers (active + pending) | |
| #define | rwlockInit(l, ...) _rwlockInit(l, opt_flags(__VA_ARGS__)) |
| #define | withReadLock(l) blkWrap (rwlockAcquireRead(l), rwlockReleaseRead(l)) |
| #define | withWriteLock(l) blkWrap (rwlockAcquireWrite(l), rwlockReleaseWrite(l)) |
Typedefs | |
| typedef struct RWLock | RWLock |
Enumerations | |
| enum | RWLOCK_Flags { RWLOCK_NoSpin = 1 } |
| Reader-writer lock initialization flags. More... | |
Functions | |
| bool | rwlockTryAcquireReadTimeout (RWLock *l, int64 timeout) |
| bool | rwlockTryAcquireWriteTimeout (RWLock *l, int64 timeout) |
| bool | rwlockTryAcquireRead (RWLock *l) |
| bool | rwlockTryAcquireWrite (RWLock *l) |
| void | rwlockAcquireRead (RWLock *l) |
| void | rwlockAcquireWrite (RWLock *l) |
| bool | rwlockReleaseRead (RWLock *l) |
| bool | rwlockReleaseWrite (RWLock *l) |
| bool | rwlockDowngradeWrite (RWLock *l) |
| void | rwlockDestroy (RWLock *l) |
Reader-writer locks for shared read access with exclusive write access.
RWLock allows multiple threads to hold read locks simultaneously, but only one thread can hold a write lock at a time (with no concurrent readers). This is ideal for data structures with frequent reads and infrequent writes.
Implementation details:
Basic usage:
Scoped locking:
| #define rwlockInit | ( | l, | |
| ... | |||
| ) | _rwlockInit(l, opt_flags(__VA_ARGS__)) |
void rwlockInit(RWLock *l, [flags])
Initialize a reader-writer lock for use.
Must be called before using any other rwlock operations.
| l | Pointer to uninitialized RWLock structure |
| ... | (flags) Optional RWLOCK_Flags (e.g., RWLOCK_NoSpin) |
| #define withReadLock | ( | l | ) | blkWrap (rwlockAcquireRead(l), rwlockReleaseRead(l)) |
| #define withWriteLock | ( | l | ) | blkWrap (rwlockAcquireWrite(l), rwlockReleaseWrite(l)) |
Reader-writer lock synchronization primitive
Allows multiple concurrent readers or a single exclusive writer. The state field packs reader count (12 bits), waiting reader count (11 bits), and writer count (9 bits) into a single 32-bit atomic.
| enum RWLOCK_Flags |
|
inline |
Acquire a read lock, blocking until available
Blocks the calling thread until shared read access can be acquired. This is equivalent to rwlockTryAcquireReadTimeout() with timeForever.
| l | RWLock to acquire for reading |
Definition at line 204 of file rwlock.h.
References relFatalError, rwlockTryAcquireReadTimeout(), and timeForever.
|
inline |
Acquire a write lock, blocking until available
Blocks the calling thread until exclusive write access can be acquired. This is equivalent to rwlockTryAcquireWriteTimeout() with timeForever.
| l | RWLock to acquire for writing |
Definition at line 249 of file rwlock.h.
References relFatalError, rwlockTryAcquireWriteTimeout(), and timeForever.
| void rwlockDestroy | ( | RWLock * | l | ) |
| bool rwlockDowngradeWrite | ( | RWLock * | l | ) |
Atomically downgrade a write lock to a read lock
Converts exclusive write access to shared read access without releasing the lock. This operation is guaranteed not to block. Useful when transitioning from a write operation to a read operation without allowing other writers to interfere.
| l | RWLock currently held for writing |
|
inline |
| bool rwlockReleaseWrite | ( | RWLock * | l | ) |
Release a previously acquired write lock
Releases exclusive write access, allowing waiting readers or writers to acquire the lock.
| l | RWLock to release |
|
inline |
Attempt to acquire a read lock without blocking
Tries to acquire shared read access immediately, returning false if not possible. Does not block or wait.
| l | RWLock to acquire for reading |
Definition at line 142 of file rwlock.h.
References RWLOCK_READER_MAX.
| bool rwlockTryAcquireReadTimeout | ( | RWLock * | l, |
| int64 | timeout | ||
| ) |
Attempt to acquire a read lock with a timeout
Tries to acquire shared read access, waiting up to the specified timeout. Multiple threads can hold read locks simultaneously. New read acquisitions are blocked if writers are pending (writer preference).
| l | RWLock to acquire for reading |
| timeout | Maximum time to wait in nanoseconds (use timeForever for infinite) |
Referenced by rwlockAcquireRead().
|
inline |
Attempt to acquire a write lock without blocking
Tries to acquire exclusive write access immediately, returning false if not possible. Does not block or wait.
| l | RWLock to acquire for writing |
Definition at line 175 of file rwlock.h.
References RWLOCK_WRITER_MAX.
| bool rwlockTryAcquireWriteTimeout | ( | RWLock * | l, |
| int64 | timeout | ||
| ) |
Attempt to acquire a write lock with a timeout
Tries to acquire exclusive write access, waiting up to the specified timeout. Only one writer can hold the lock, and no readers can be active when a write lock is held.
| l | RWLock to acquire for writing |
| timeout | Maximum time to wait in nanoseconds (use timeForever for infinite) |
Referenced by rwlockAcquireWrite().