|
CX Framework
Cross-platform C utility framework
|
Data Structures | |
| struct | Mutex |
Macros | |
| #define | mutexInit(m, ...) _mutexInit(m, opt_flags(__VA_ARGS__)) |
| #define | withMutex(m) blkWrap (mutexAcquire(m), mutexRelease(m)) |
Typedefs | |
| typedef struct Mutex | Mutex |
Enumerations | |
| enum | MUTEX_Flags { MUTEX_NoSpin = 1 } |
| Mutex initialization flags. More... | |
Functions | |
| bool | mutexTryAcquireTimeout (Mutex *m, int64 timeout) |
| bool | mutexRelease (Mutex *m) |
| bool | mutexTryAcquire (Mutex *m) |
| void | mutexAcquire (Mutex *m) |
| void | mutexDestroy (Mutex *m) |
Fast, adaptive mutexes for exclusive access to shared resources.
The CX Mutex implementation uses futexes and adaptive spinning for efficiency:
Mutexes are NOT reentrant - attempting to acquire the same mutex twice from the same thread will deadlock.
Basic usage:
Scoped locking with automatic release:
| #define mutexInit | ( | m, | |
| ... | |||
| ) | _mutexInit(m, opt_flags(__VA_ARGS__)) |
void mutexInit(Mutex *m, [flags])
Initialize a mutex for use.
Must be called before using any other mutex operations.
| m | Pointer to uninitialized mutex structure |
| ... | (flags) Optional MUTEX_Flags (e.g., MUTEX_NoSpin) |
| #define withMutex | ( | m | ) | blkWrap (mutexAcquire(m), mutexRelease(m)) |
Execute a block with automatic mutex locking and unlocking
Acquires the mutex before executing the following block, and automatically releases it when the block exits (including early returns, breaks, or exceptions).
Example:
| m | Mutex to lock for the duration of the block |
Mutex synchronization primitive
Internal structure using futex-based locking with adaptive spinning. Futex values: 0=unlocked, 1=locked (no waiters), 2=locked with contention
| enum MUTEX_Flags |
|
inline |
Acquire a mutex, blocking until it becomes available
Blocks the calling thread until the mutex can be acquired. This is equivalent to mutexTryAcquireTimeout() with timeForever.
| m | Mutex to acquire |
Definition at line 120 of file mutex.h.
References mutexTryAcquireTimeout(), and timeForever.
| void mutexDestroy | ( | Mutex * | m | ) |
Destroy a mutex and release its resources
Cleans up the mutex after use. The mutex must not be held when destroyed. After destruction, the mutex must be reinitialized before it can be used again.
| m | Mutex to destroy |
| bool mutexRelease | ( | Mutex * | m | ) |
Release a previously acquired mutex
Releases the mutex so other threads can acquire it. Must be called from the same thread that acquired the mutex. Undefined behavior if called on a mutex not currently held.
| m | Mutex to release |
|
inline |
| bool mutexTryAcquireTimeout | ( | Mutex * | m, |
| int64 | timeout | ||
| ) |
Attempt to acquire a mutex with a timeout
Tries to acquire the mutex, waiting up to the specified timeout. Uses adaptive spinning before falling back to kernel waits for efficiency.
| m | Mutex to acquire |
| timeout | Maximum time to wait in nanoseconds (use timeForever for infinite) |
Referenced by mutexAcquire().