ChibiOS/RT
6.1.4
|
Semaphores related APIs and services.
Semaphores are a flexible synchronization primitive, ChibiOS/RT implements semaphores in their "counting semaphores" variant as defined by Edsger Dijkstra plus several enhancements like:
The binary semaphores variant can be easily implemented using counting semaphores.
Operations defined for semaphores:
Semaphores can be used as guards for mutual exclusion zones (note that mutexes are recommended for this kind of use) but also have other uses, queues guards and counters for example.
Semaphores usually use a FIFO queuing strategy but it is possible to make them order threads by priority by enabling CH_CFG_USE_SEMAPHORES_PRIORITY
in chconf.h
.
CH_CFG_USE_SEMAPHORES
option must be enabled in chconf.h
. Macros | |
#define | _SEMAPHORE_DATA(name, n) {_CH_QUEUE_DATA(name.queue), n} |
Data part of a static semaphore initializer. More... | |
#define | SEMAPHORE_DECL(name, n) semaphore_t name = _SEMAPHORE_DATA(name, n) |
Static semaphore initializer. More... | |
Typedefs | |
typedef struct ch_semaphore | semaphore_t |
Semaphore structure. More... | |
Data Structures | |
struct | ch_semaphore |
Semaphore structure. More... | |
Functions | |
void | chSemObjectInit (semaphore_t *sp, cnt_t n) |
Initializes a semaphore with the specified counter value. More... | |
void | chSemResetWithMessage (semaphore_t *sp, cnt_t n, msg_t msg) |
Performs a reset operation on the semaphore. More... | |
void | chSemResetWithMessageI (semaphore_t *sp, cnt_t n, msg_t msg) |
Performs a reset operation on the semaphore. More... | |
msg_t | chSemWait (semaphore_t *sp) |
Performs a wait operation on a semaphore. More... | |
msg_t | chSemWaitS (semaphore_t *sp) |
Performs a wait operation on a semaphore. More... | |
msg_t | chSemWaitTimeout (semaphore_t *sp, sysinterval_t timeout) |
Performs a wait operation on a semaphore with timeout specification. More... | |
msg_t | chSemWaitTimeoutS (semaphore_t *sp, sysinterval_t timeout) |
Performs a wait operation on a semaphore with timeout specification. More... | |
void | chSemSignal (semaphore_t *sp) |
Performs a signal operation on a semaphore. More... | |
void | chSemSignalI (semaphore_t *sp) |
Performs a signal operation on a semaphore. More... | |
void | chSemAddCounterI (semaphore_t *sp, cnt_t n) |
Adds the specified value to the semaphore counter. More... | |
msg_t | chSemSignalWait (semaphore_t *sps, semaphore_t *spw) |
Performs atomic signal and wait operations on two semaphores. More... | |
static void | chSemReset (semaphore_t *sp, cnt_t n) |
Performs a reset operation on the semaphore. More... | |
static void | chSemResetI (semaphore_t *sp, cnt_t n) |
Performs a reset operation on the semaphore. More... | |
static void | chSemFastWaitI (semaphore_t *sp) |
Decreases the semaphore counter. More... | |
static void | chSemFastSignalI (semaphore_t *sp) |
Increases the semaphore counter. More... | |
static cnt_t | chSemGetCounterI (const semaphore_t *sp) |
Returns the semaphore counter current value. More... | |
#define _SEMAPHORE_DATA | ( | name, | |
n | |||
) | {_CH_QUEUE_DATA(name.queue), n} |
Data part of a static semaphore initializer.
This macro should be used when statically initializing a semaphore that is part of a bigger structure.
[in] | name | the name of the semaphore variable |
[in] | n | the counter initial value, this value must be non-negative |
#define SEMAPHORE_DECL | ( | name, | |
n | |||
) | semaphore_t name = _SEMAPHORE_DATA(name, n) |
typedef struct ch_semaphore semaphore_t |
Semaphore structure.
void chSemObjectInit | ( | semaphore_t * | sp, |
cnt_t | n | ||
) |
Initializes a semaphore with the specified counter value.
[out] | sp | pointer to a semaphore_t structure |
[in] | n | initial value of the semaphore counter. Must be non-negative. |
Definition at line 97 of file chsem.c.
References ch_queue_init(), chDbgCheck, ch_semaphore::cnt, and ch_semaphore::queue.
Referenced by _factory_init(), chBSemObjectInit(), chCacheObjectInit(), and chGuardedPoolObjectInitAligned().
void chSemResetWithMessage | ( | semaphore_t * | sp, |
cnt_t | n, | ||
msg_t | msg | ||
) |
Performs a reset operation on the semaphore.
[in] | sp | pointer to a semaphore_t structure |
[in] | n | the new value of the semaphore counter. The value must be non-negative. |
[in] | msg | message to be sent |
Definition at line 118 of file chsem.c.
References chSchRescheduleS(), chSemResetWithMessageI(), chSysLock(), and chSysUnlock().
Referenced by chSemReset().
void chSemResetWithMessageI | ( | semaphore_t * | sp, |
cnt_t | n, | ||
msg_t | msg | ||
) |
Performs a reset operation on the semaphore.
[in] | sp | pointer to a semaphore_t structure |
[in] | n | the new value of the semaphore counter. The value must be non-negative. |
[in] | msg | message to be sent |
Definition at line 143 of file chsem.c.
References ch_queue_isempty(), ch_queue_lifo_remove(), ch_queue_notempty(), chDbgAssert, chDbgCheck, chDbgCheckClassI(), chSchReadyI(), ch_semaphore::cnt, ch_semaphore::queue, ch_thread::rdymsg, and ch_thread::u.
Referenced by chSemResetI(), and chSemResetWithMessage().
msg_t chSemWait | ( | semaphore_t * | sp | ) |
Performs a wait operation on a semaphore.
[in] | sp | pointer to a semaphore_t structure |
MSG_OK | if the thread has not stopped on the semaphore or the semaphore has been signaled. |
MSG_RESET | if the semaphore has been reset using chSemReset() . |
Definition at line 169 of file chsem.c.
References chSemWaitS(), chSysLock(), and chSysUnlock().
Referenced by chBSemWait().
msg_t chSemWaitS | ( | semaphore_t * | sp | ) |
Performs a wait operation on a semaphore.
[in] | sp | pointer to a semaphore_t structure |
MSG_OK | if the thread has not stopped on the semaphore or the semaphore has been signaled. |
MSG_RESET | if the semaphore has been reset using chSemReset() . |
Definition at line 191 of file chsem.c.
References ch_queue_isempty(), ch_queue_notempty(), chDbgAssert, chDbgCheck, chDbgCheckClassS(), ch_semaphore::cnt, currp, and ch_semaphore::queue.
Referenced by chBSemWaitS(), chSemWait(), and lru_get_last_s().
msg_t chSemWaitTimeout | ( | semaphore_t * | sp, |
sysinterval_t | timeout | ||
) |
Performs a wait operation on a semaphore with timeout specification.
[in] | sp | pointer to a semaphore_t structure |
[in] | timeout | the number of ticks before the operation timeouts, the following special values are allowed:
|
MSG_OK | if the thread has not stopped on the semaphore or the semaphore has been signaled. |
MSG_RESET | if the semaphore has been reset using chSemReset() . |
MSG_TIMEOUT | if the semaphore has not been signaled or reset within the specified timeout. |
Definition at line 229 of file chsem.c.
References chSemWaitTimeoutS(), chSysLock(), and chSysUnlock().
Referenced by chBSemWaitTimeout().
msg_t chSemWaitTimeoutS | ( | semaphore_t * | sp, |
sysinterval_t | timeout | ||
) |
Performs a wait operation on a semaphore with timeout specification.
[in] | sp | pointer to a semaphore_t structure |
[in] | timeout | the number of ticks before the operation timeouts, the following special values are allowed:
|
MSG_OK | if the thread has not stopped on the semaphore or the semaphore has been signaled. |
MSG_RESET | if the semaphore has been reset using chSemReset() . |
MSG_TIMEOUT | if the semaphore has not been signaled or reset within the specified timeout. |
Definition at line 258 of file chsem.c.
References ch_queue_isempty(), ch_queue_notempty(), chDbgAssert, chDbgCheck, chDbgCheckClassS(), ch_semaphore::cnt, currp, MSG_TIMEOUT, ch_semaphore::queue, and TIME_IMMEDIATE.
Referenced by chBSemWaitTimeoutS(), chGuardedPoolAllocTimeoutS(), and chSemWaitTimeout().
void chSemSignal | ( | semaphore_t * | sp | ) |
Performs a signal operation on a semaphore.
[in] | sp | pointer to a semaphore_t structure |
Definition at line 288 of file chsem.c.
References ch_queue_fifo_remove(), ch_queue_isempty(), ch_queue_notempty(), chDbgAssert, chDbgCheck, chSchWakeupS(), chSysLock(), chSysUnlock(), ch_semaphore::cnt, MSG_OK, and ch_semaphore::queue.
void chSemSignalI | ( | semaphore_t * | sp | ) |
Performs a signal operation on a semaphore.
[in] | sp | pointer to a semaphore_t structure |
Definition at line 313 of file chsem.c.
References ch_queue_fifo_remove(), ch_queue_isempty(), ch_queue_notempty(), chDbgAssert, chDbgCheck, chDbgCheckClassI(), chSchReadyI(), ch_semaphore::cnt, MSG_OK, ch_semaphore::queue, ch_thread::rdymsg, and ch_thread::u.
Referenced by chBSemSignalI(), and chGuardedPoolFreeI().
void chSemAddCounterI | ( | semaphore_t * | sp, |
cnt_t | n | ||
) |
Adds the specified value to the semaphore counter.
[in] | sp | pointer to a semaphore_t structure |
[in] | n | value to be added to the semaphore counter. The value must be positive. |
Definition at line 343 of file chsem.c.
References ch_queue_fifo_remove(), ch_queue_isempty(), ch_queue_notempty(), chDbgAssert, chDbgCheck, chDbgCheckClassI(), chSchReadyI(), ch_semaphore::cnt, MSG_OK, ch_semaphore::queue, ch_thread::rdymsg, and ch_thread::u.
msg_t chSemSignalWait | ( | semaphore_t * | sps, |
semaphore_t * | spw | ||
) |
Performs atomic signal and wait operations on two semaphores.
[in] | sps | pointer to a semaphore_t structure to be signaled |
[in] | spw | pointer to a semaphore_t structure to wait on |
MSG_OK | if the thread has not stopped on the semaphore or the semaphore has been signaled. |
MSG_RESET | if the semaphore has been reset using chSemReset() . |
Definition at line 372 of file chsem.c.
References ch_queue_fifo_remove(), ch_queue_isempty(), ch_queue_notempty(), chDbgAssert, chDbgCheck, chSchReadyI(), chSysLock(), ch_semaphore::cnt, currp, MSG_OK, ch_semaphore::queue, ch_thread::rdymsg, and ch_thread::u.
|
inlinestatic |
Performs a reset operation on the semaphore.
MSG_RESET
as message.[in] | sp | pointer to a semaphore_t structure |
[in] | n | the new value of the semaphore counter. The value must be non-negative. |
Definition at line 123 of file chsem.h.
References chSemResetWithMessage(), and MSG_RESET.
Referenced by chBSemReset().
|
inlinestatic |
Performs a reset operation on the semaphore.
MSG_RESET
as message.[in] | sp | pointer to a semaphore_t structure |
[in] | n | the new value of the semaphore counter. The value must be non-negative. |
Definition at line 145 of file chsem.h.
References chSemResetWithMessageI(), and MSG_RESET.
Referenced by chBSemResetI().
|
inlinestatic |
Decreases the semaphore counter.
This macro can be used when the counter is known to be positive.
[in] | sp | pointer to a semaphore_t structure |
Definition at line 158 of file chsem.h.
References chDbgCheckClassI(), and ch_semaphore::cnt.
Referenced by chGuardedPoolAllocI().
|
inlinestatic |
Increases the semaphore counter.
This macro can be used when the counter is known to be not negative.
[in] | sp | pointer to a semaphore_t structure |
Definition at line 174 of file chsem.h.
References chDbgCheckClassI(), and ch_semaphore::cnt.
|
inlinestatic |
Returns the semaphore counter current value.
[in] | sp | pointer to a semaphore_t structure |
Definition at line 189 of file chsem.h.
References chDbgCheckClassI(), and ch_semaphore::cnt.
Referenced by chGuardedPoolAllocI(), and chGuardedPoolGetCounterI().