Semaphores

Lightweight semaphore implementation. More...

Detailed Description

Files

file  sema.h
 Semaphore definitions.
 

Data Structures

struct  sema_t
 A Semaphore. More...
 

Macros

#define SEMA_CREATE(value)   { (value), SEMA_OK, MUTEX_INIT }
 Creates semaphore statically. More...
 
#define SEMA_CREATE_LOCKED()   { (0), SEMA_OK, MUTEX_INIT_LOCKED }
 Creates semaphore statically initialized to 0. More...
 

Enumerations

enum  sema_state_t { SEMA_OK = 0, SEMA_DESTROY }
 A Semaphore states.
 

Functions

void sema_create (sema_t *sema, unsigned int value)
 Creates semaphore dynamically. More...
 
void sema_destroy (sema_t *sema)
 Destroys a semaphore. More...
 
int _sema_wait (sema_t *sema, int block, uint64_t timeout)
 Wait for a semaphore, blocking or non-blocking. More...
 
static int sema_wait_timed (sema_t *sema, uint64_t timeout)
 Wait for a semaphore being posted. More...
 
static int sema_wait (sema_t *sema)
 Wait for a semaphore being posted (without timeout). More...
 
static int sema_try_wait (sema_t *sema)
 Test if the semaphore is posted. More...
 
int sema_post (sema_t *sema)
 Signal semaphore. More...
 

Macro Definition Documentation

#define SEMA_CREATE (   value)    { (value), SEMA_OK, MUTEX_INIT }
Parameters
[in]valueInitial 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.

#define SEMA_CREATE_LOCKED ( )    { (0), SEMA_OK, MUTEX_INIT_LOCKED }
Returns
Statically initialized semaphore.

Definition at line 50 of file sema.h.

Function Documentation

int _sema_wait ( sema_t sema,
int  block,
uint64_t  timeout 
)

For commit purposes you should probably use sema_wait(), sema_wait_timed() and sema_try_wait() instead.

Precondition
(sema != NULL)
Parameters
[in]semaA semaphore.
[in]blockif true, block until semaphore is available.
[in]timeoutif 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)
void sema_create ( sema_t sema,
unsigned int  value 
)
Precondition
(sema != NULL)
See also
The Open Group Base Specifications Issue 7, sem_init() (without pshared parameter)
Parameters
[out]semaThe created semaphore.
[in]valueInitial value for the semaphore.
void sema_destroy ( sema_t sema)
Precondition
(sema != NULL)
See also
The Open Group Base Specifications Issue 7, sem_destroy()

Destroying a semaphore upon which other threads are currently blocked will wake the other threads causing the sema_wait (or sema_wait_timed) to return error (-ECANCELED).

Parameters
[in]semaThe semaphore to destroy.
int sema_post ( sema_t sema)
Precondition
(sema != NULL)
Parameters
[in]semaA semaphore.
Returns
0, on success
-EOVERFLOW, if the semaphore's value would overflow.
static int sema_try_wait ( sema_t sema)
inlinestatic
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.

static int sema_wait ( sema_t sema)
inlinestatic
Precondition
(sema != NULL)
Parameters
[in]semaA semaphore.
Returns
0 on success
-ECANCELED, if the semaphore was destroyed.

Definition at line 149 of file sema.h.

static int sema_wait_timed ( sema_t sema,
uint64_t  timeout 
)
inlinestatic
Precondition
(sema != NULL)
Parameters
[in]semaA semaphore.
[in]timeoutTime 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.