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

Asterisk semaphore API. More...

#include <semaphore.h>
#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.
Returns
0 on success.
-1 on error, errno set to indicate error.

Referenced by bridge_sync_cleanup(), default_listener_pvt_destroy(), and test_cb_data_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.
Returns
0 on success.
-1 on 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.
Returns
0 on success.
-1 on error, errno set to indicate error.

Referenced by bridge_sync_init(), default_listener_pvt_alloc(), and test_cb_data_alloc().

◆ ast_sem_post()

int ast_sem_post ( struct ast_sem sem)

Increments the semaphore, unblocking a waiter if necessary.

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

Referenced by bridge_sync_signal(), default_task_pushed(), and test_cb().

◆ 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.
Returns
0 on success.
-1 on 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.
Returns
0 on success.
-1 on error, errno set to indicate error.

Referenced by default_tps_processing_function(), and presence_change_common().