Asterisk - The Open Source Telephony Project GIT-master-a358458
Data Structures | Macros | Functions
sem.h File Reference

Asterisk semaphore API. More...

#include "asterisk/lock.h"
#include <limits.h>
Include dependency graph for sem.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ast_sem
 

Macros

#define AST_SEM_VALUE_MAX   INT_MAX
 

Functions

int ast_sem_destroy (struct ast_sem *sem)
 Destroy a semaphore. More...
 
int ast_sem_getvalue (struct ast_sem *sem, int *sval)
 Gets the current value of the semaphore. More...
 
int ast_sem_init (struct ast_sem *sem, int pshared, unsigned int value)
 Initialize a semaphore. More...
 
int ast_sem_post (struct ast_sem *sem)
 Increments the semaphore, unblocking a waiter if necessary. More...
 
int ast_sem_timedwait (struct ast_sem *sem, const struct timespec *abs_timeout)
 Decrements the semaphore, waiting until abs_timeout. More...
 
int ast_sem_wait (struct ast_sem *sem)
 Decrements the semaphore. More...
 

Detailed Description

Asterisk semaphore API.

This API is a thin wrapper around the POSIX semaphore API (when available), so see the POSIX documentation for further details.

Definition in file sem.h.

Macro Definition Documentation

◆ AST_SEM_VALUE_MAX

#define AST_SEM_VALUE_MAX   INT_MAX

Definition at line 92 of file sem.h.

Function Documentation

◆ ast_sem_destroy()

int ast_sem_destroy ( struct ast_sem sem)

Destroy a semaphore.

Destroying a semaphore that other threads are currently blocked on produces undefined behavior.

Parameters
semSemaphore to destroy.
Return values
0on success.
-1on error, errno set to indicate error.

Referenced by bridge_sync_cleanup(), and default_listener_pvt_destroy().

◆ ast_sem_getvalue()

int ast_sem_getvalue ( struct ast_sem sem,
int *  sval 
)

Gets the current value of the semaphore.

If threads are blocked on this semaphore, POSIX allows the return value to be either 0 or a negative number whose absolute value is the number of threads blocked. Don't assume that it will give you one or the other; Asterisk has been ported to just about everything.

Parameters
semSemaphore to query.
[out]svalOutput value.
Return values
0on success.
-1on error, errno set to indicate error.

Referenced by default_tps_processing_function().

◆ ast_sem_init()

int ast_sem_init ( struct ast_sem sem,
int  pshared,
unsigned int  value 
)

Initialize a semaphore.

Parameters
semSemaphore to initialize.
psharedPass true (nonzero) to share this thread between processes. Not be supported on all platforms, so be wary! But leave the parameter, to be compatible with the POSIX ABI in case we need to add support in the future.
valueInitial value of the semaphore.
Return values
0on success.
-1on error, errno set to indicate error.

Referenced by bridge_sync_init(), and default_listener_pvt_alloc().

◆ ast_sem_post()

int ast_sem_post ( struct ast_sem sem)

Increments the semaphore, unblocking a waiter if necessary.

Parameters
semSemaphore to increment.
Return values
0on success.
-1on error, errno set to indicate error.

Referenced by bridge_sync_signal(), and default_task_pushed().

◆ ast_sem_timedwait()

int ast_sem_timedwait ( struct ast_sem sem,
const struct timespec *  abs_timeout 
)

Decrements the semaphore, waiting until abs_timeout.

If the semaphore's current value is zero, this function blocks until another thread posts (ast_sem_post()) to the semaphore (or is interrupted by a signal handler, which sets errno to EINTR).

Parameters
semSemaphore to decrement.
abs_timeout
Return values
0on success.
-1on error, errno set to indicate error.

Referenced by bridge_sync_wait().

◆ ast_sem_wait()

int ast_sem_wait ( struct ast_sem sem)

Decrements the semaphore.

If the semaphore's current value is zero, this function blocks until another thread posts (ast_sem_post()) to the semaphore (or is interrupted by a signal handler, which sets errno to EINTR).

Parameters
semSemaphore to decrement.
Return values
0on success.
-1on error, errno set to indicate error.

Referenced by default_tps_processing_function().