|
| #define | semaInit(sema, count, ...) _semaInit(sema, count, opt_flags(__VA_ARGS__)) |
| |
|
|
typedef struct Semaphore | Semaphore |
| | Counting semaphore synchronization primitive.
|
| |
A counting semaphore for controlling access to a resource with a limited number of concurrent users, or for producer/consumer hand-off of a count of available items.
A semaphore holds a non-negative count. semaDec() (and its timed/non-blocking variants) waits until the count is greater than zero, then decrements it. semaInc() adds to the count and wakes any waiters that can now proceed.
Basic usage:
#define semaInit(sema, count,...)
bool semaInc(Semaphore *sema, int32 count)
bool semaDec(Semaphore *sema)
bool semaDestroy(Semaphore *sema)
Counting semaphore synchronization primitive.
◆ semaInit
| #define semaInit |
( |
|
sema, |
|
|
|
count, |
|
|
|
... |
|
) |
| _semaInit(sema, count, opt_flags(__VA_ARGS__)) |
void semaInit(Semaphore *sema, int32 count, [flags])
Initialize a semaphore for use.
Must be called before using any other semaphore operations.
- Parameters
-
| sema | Pointer to uninitialized semaphore structure |
| count | Initial count. Must not be negative |
| ... | (flags) Optional SEMA_Flags (e.g., SEMA_NoSpin) |
Definition at line 61 of file sema.h.
◆ SEMA_Flags
Semaphore initialization flags.
| Enumerator |
|---|
| SEMA_NoSpin | Disable adaptive spinning, use kernel futex immediately.
|
Definition at line 41 of file sema.h.
◆ semaDec()
Decrement a semaphore, blocking until the count is available
Blocks the calling thread until the count is greater than zero, then decrements it. This is equivalent to semaTryDecTimeout() with timeForever.
- Parameters
-
- Returns
- true on success
Definition at line 109 of file sema.h.
References semaTryDecTimeout(), and timeForever.
◆ semaDestroy()
Destroy a semaphore and release its resources
Cleans up the semaphore after use. After destruction, the semaphore must be reinitialized before it can be used again.
- Parameters
-
- Returns
- true on success
◆ semaInc()
| bool semaInc |
( |
Semaphore * |
sema, |
|
|
int32 |
count |
|
) |
| |
|
inline |
Increment a semaphore's count
Adds count to the semaphore and wakes up to that many waiting threads.
- Parameters
-
| sema | Semaphore to increment |
| count | Amount to add to the count |
- Returns
- true on success
Definition at line 120 of file sema.h.
◆ semaTryDec()
Attempt to decrement a semaphore without blocking
Decrements the count immediately if it is greater than zero, returning false otherwise. Does not block or wait.
- Parameters
-
- Returns
- true if the count was decremented, false if it was already zero
Definition at line 87 of file sema.h.
◆ semaTryDecTimeout()
| bool semaTryDecTimeout |
( |
Semaphore * |
sema, |
|
|
int64 |
timeout |
|
) |
| |
Attempt to decrement a semaphore with a timeout
Waits up to the specified timeout for the count to become greater than zero, then decrements it. Uses adaptive spinning before falling back to kernel waits for efficiency.
- Parameters
-
| sema | Semaphore to decrement |
| timeout | Maximum time to wait in nanoseconds (use timeForever for infinite) |
- Returns
- true if the count was decremented, false if the timeout elapsed
Referenced by semaDec().