POSIX semaphores

Detailed Description

Files

file  semaphore.h
 Semaphores.
 

Macros

#define SEM_FAILED   ((sem_t *) 0)
 Value returned if `sem_open' failed.
 

Typedefs

typedef sema_t sem_t
 POSIX-specific semaphore type.
 

Functions

static int sem_init (sem_t *sem, int pshared, unsigned value)
 Initialize an unnamed semaphore. More...
 
static int sem_destroy (sem_t *sem)
 destroy an unnamed semaphore More...
 
static int sem_post (sem_t *sem)
 Unlock a semaphore. More...
 
static int sem_wait (sem_t *sem)
 Lock a semaphore. More...
 
static sem_tsem_open (const char *name, int oflag,...)
 Open a named semaphore name with open flags oflag. More...
 
static int sem_close (sem_t *sem)
 Close descriptor for named semaphore sem. More...
 
static int sem_unlink (const char *name)
 Remove named semaphore name. More...
 
int sem_timedwait (sem_t *sem, const struct timespec *abstime)
 Similar to `sem_wait' but wait only until abstime. More...
 
static int sem_trywait (sem_t *sem)
 Test whether sem is posted. More...
 
static int sem_getvalue (sem_t *sem, int *sval)
 Get current value of sem and store it in sval. More...
 

Function Documentation

◆ sem_close()

static int sem_close ( sem_t sem)
inlinestatic

Close descriptor for named semaphore sem.

See also
The Open Group Base Specifications Issue 7, sem_close()
Todo:
named semaphore are currently not supported
Parameters
[in]semSemaphore to close.
Returns
Always -1, since it is not implemented currently.

Definition at line 198 of file semaphore.h.

◆ sem_destroy()

static int sem_destroy ( sem_t sem)
inlinestatic

destroy an unnamed semaphore

See also
The Open Group Base Specifications Issue 7, sem_destroy()

The sem_destroy() function shall destroy the unnamed semaphore indicated by sem. Only a semaphore that was created using sem_init() may be destroyed using sem_destroy(); the effect of calling sem_destroy() with a named semaphore is undefined. The effect of subsequent use of the semaphore sem is undefined until sem is reinitialized by another call to sem_init().

It is safe to destroy an initialized semaphore upon which no threads are currently blocked. The effect of destroying a semaphore upon which other threads are currently blocked is undefined.

Parameters
semA semaphore.
Returns
0 on success.
-1, on error and errno set to indicate the error.

Definition at line 96 of file semaphore.h.

◆ sem_getvalue()

static int sem_getvalue ( sem_t sem,
int *  sval 
)
inlinestatic

Get current value of sem and store it in sval.

See also
The Open Group Base Specifications Issue 7, sem_getvalue()
Parameters
[in]semSemaphore to get the value from.
[out]svalPlace where value goes to.
Returns
0 on success.
-1, on error and errno set to indicate the error.

Definition at line 283 of file semaphore.h.

◆ sem_init()

static int sem_init ( sem_t sem,
int  pshared,
unsigned  value 
)
inlinestatic

Initialize an unnamed semaphore.

See also
The Open Group Base Specifications Issue 7, sem_init()

The sem_init() function shall initialize the unnamed semaphore referred to by sem. The value of the initialized semaphore shall be value. Following a successful call to sem_init(), the semaphore may be used in subsequent calls to sem_wait(), sem_timedwait(), sem_trywait(), sem_post(), and sem_destroy(). This semaphore shall remain usable until the semaphore is destroyed.

Parameters
[out]semSemaphore to initialize.
[in]pshared**(unused, since RIOT only has threads)** Semaphore is shared between processes not threads.
[in]valueValue to set.
Returns
0 on success.
-1, on error and errno set to indicate the error.

Definition at line 68 of file semaphore.h.

◆ sem_open()

static sem_t* sem_open ( const char *  name,
int  oflag,
  ... 
)
inlinestatic

Open a named semaphore name with open flags oflag.

See also
The Open Group Base Specifications Issue 7, sem_open()
Todo:
named semaphore are currently not supported
Parameters
[in]nameName to set.
[in]oflagFlags to set.
Returns
Always SEM_FAILED, since it is not implemented currently.

Definition at line 177 of file semaphore.h.

◆ sem_post()

static int sem_post ( sem_t sem)
inlinestatic

Unlock a semaphore.

See also
The Open Group Base Specifications Issue 7, sem_post()

The sem_post() function shall unlock the semaphore referenced by sem by performing a semaphore unlock operation on that semaphore.

If the semaphore value resulting from this operation is positive, then no threads were blocked waiting for the semaphore to become unlocked; the semaphore value is simply incremented.

If the value of the semaphore resulting from this operation is zero, then one of the threads blocked waiting for the semaphore shall be allowed to return successfully from its call to sem_wait().

Parameters
semA semaphore
Returns
0 on success.
-1, on error and errno set to indicate the error.

Definition at line 124 of file semaphore.h.

◆ sem_timedwait()

int sem_timedwait ( sem_t sem,
const struct timespec abstime 
)

Similar to `sem_wait' but wait only until abstime.

See also
The Open Group Base Specifications Issue 7, sem_timedwait()

The sem_timedwait() function shall lock the semaphore referenced by sem as in the sem_wait() function. However, if the semaphore cannot be locked without waiting for another process or thread to unlock the semaphore by performing a sem_post() function, this wait shall be terminated when the specified timeout expires.

Parameters
[in]semSemaphore to wait on.
[in]abstimeAbsolute time (that is when the clock on which temouts are based equals this value) the timeout for the wait shall expire. If the value specified has already passed the timeout expires immediately.
Returns
0 on success.
-1, on error and errno set to indicate the error.

◆ sem_trywait()

static int sem_trywait ( sem_t sem)
inlinestatic

Test whether sem is posted.

See also
The Open Group Base Specifications Issue 7, sem_trywait()
Parameters
[in]semSemaphore to try to wait on
Returns
0 on success.
-1, on error and errno set to indicate the error.

Definition at line 259 of file semaphore.h.

◆ sem_unlink()

static int sem_unlink ( const char *  name)
inlinestatic

Remove named semaphore name.

See also
The Open Group Base Specifications Issue 7, sem_unlink()
Todo:
named semaphore are currently not supported
Parameters
[in]nameName to unlink.
Returns
Always -1, since it is not implemented currently.

Definition at line 218 of file semaphore.h.

◆ sem_wait()

static int sem_wait ( sem_t sem)
inlinestatic

Lock a semaphore.

See also
The Open Group Base Specifications Issue 7, sem_wait()

The sem_wait() function shall lock the semaphore referenced by sem by performing a semaphore lock operation on that semaphore. If the semaphore value is currently zero, then the calling thread shall not return from the call to sem_wait() until it either locks the semaphore or the call is interrupted by a signal.

Parameters
semA semaphore.
Returns
0 on success.
-1, on error and errno set to indicate the error.

Definition at line 152 of file semaphore.h.