Lightweight semaphore implementation.
More...
Lightweight semaphore implementation.
|
file | sema.h |
| Semaphore definitions.
|
|
|
enum | sema_state_t { SEMA_OK = 0,
SEMA_DESTROY
} |
| A Semaphore states.
|
|
◆ SEMA_CREATE
#define SEMA_CREATE |
( |
|
value | ) |
{ (value), SEMA_OK, MUTEX_INIT } |
Creates semaphore statically.
- Parameters
-
[in] | value | Initial value for the semaphore (can't be 0). For a 0 initialized semaphore |
- See also
- SEMA_CREATE_LOCKED
- Returns
- Statically initialized semaphore.
Definition at line 44 of file sema.h.
◆ SEMA_CREATE_LOCKED
Creates semaphore statically initialized to 0.
- Returns
- Statically initialized semaphore.
Definition at line 50 of file sema.h.
◆ _sema_wait()
int _sema_wait |
( |
sema_t * |
sema, |
|
|
int |
block, |
|
|
uint64_t |
timeout |
|
) |
| |
Wait for a semaphore, blocking or non-blocking.
For commit purposes you should probably use sema_wait(), sema_wait_timed() and sema_try_wait() instead.
- Precondition
(sema != NULL)
- Parameters
-
[in] | sema | A semaphore. |
[in] | block | if true, block until semaphore is available. |
[in] | timeout | if blocking, time in microseconds until the semaphore times out. 0 waits forever. |
- Returns
- 0 on success
-
-ETIMEDOUT, if the semaphore times out.
-
-ECANCELED, if the semaphore was destroyed.
-
-EAGAIN, if the semaphore is not posted (only if block = 0)
◆ sema_create()
void sema_create |
( |
sema_t * |
sema, |
|
|
unsigned int |
value |
|
) |
| |
◆ sema_destroy()
void sema_destroy |
( |
sema_t * |
sema | ) |
|
◆ sema_post()
int sema_post |
( |
sema_t * |
sema | ) |
|
Signal semaphore.
- Precondition
(sema != NULL)
- Parameters
-
- Returns
- 0, on success
-
-EOVERFLOW, if the semaphore's value would overflow.
◆ sema_try_wait()
static int sema_try_wait |
( |
sema_t * |
sema | ) |
|
|
inlinestatic |
Test if the semaphore is posted.
- Precondition
(sema != NULL)
This is a non-blocking alternative to sema_wait.
- Returns
- 0 on success
-
-EAGAIN, if the semaphore is not posted.
-
-ECANCELED, if the semaphore was destroyed.
Definition at line 165 of file sema.h.
◆ sema_wait()
static int sema_wait |
( |
sema_t * |
sema | ) |
|
|
inlinestatic |
Wait for a semaphore being posted (without timeout).
- Precondition
(sema != NULL)
- Parameters
-
- Returns
- 0 on success
-
-ECANCELED, if the semaphore was destroyed.
Definition at line 149 of file sema.h.
◆ sema_wait_timed()
static int sema_wait_timed |
( |
sema_t * |
sema, |
|
|
uint64_t |
timeout |
|
) |
| |
|
inlinestatic |
Wait for a semaphore being posted.
- Precondition
(sema != NULL)
- Parameters
-
[in] | sema | A semaphore. |
[in] | timeout | Time in microseconds until the semaphore times out. 0 does not wait. |
- Returns
- 0 on success
-
-ETIMEDOUT, if the semaphore times out.
-
-ECANCELED, if the semaphore was destroyed.
-
-EAGAIN, if the semaphore is not posted (only if timeout = 0)
Definition at line 134 of file sema.h.