ChibiOS/RT
5.1.0
|
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) {_THREADS_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 | chSemReset (semaphore_t *sp, cnt_t n) |
Performs a reset operation on the semaphore. More... | |
void | chSemResetI (semaphore_t *sp, cnt_t n) |
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 | 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 | |||
) | {_THREADS_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 chDbgCheck, ch_semaphore::cnt, ch_semaphore::queue, and queue_init().
Referenced by _factory_init(), _heap_init(), chBSemObjectInit(), chFactoryCreateSemaphore(), chGuardedPoolObjectInitAligned(), and chHeapObjectInit().
void chSemReset | ( | semaphore_t * | sp, |
cnt_t | n | ||
) |
Performs a reset operation on the semaphore.
chSemWait()
will return MSG_RESET
instead of MSG_OK
.[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 120 of file chsem.c.
References chSchRescheduleS(), chSemResetI(), chSysLock(), and chSysUnlock().
Referenced by chBSemReset().
void chSemResetI | ( | semaphore_t * | sp, |
cnt_t | n | ||
) |
Performs a reset operation on the semaphore.
chSemWait()
will return MSG_RESET
instead of MSG_OK
.[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 147 of file chsem.c.
References chDbgAssert, chDbgCheck, chDbgCheckClassI(), chSchReadyI(), ch_semaphore::cnt, MSG_RESET, ch_semaphore::queue, queue_isempty(), queue_lifo_remove(), queue_notempty(), ch_thread::rdymsg, and ch_thread::u.
Referenced by chBSemResetI(), and chSemReset().
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 175 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 197 of file chsem.c.
References CH_STATE_WTSEM, chDbgAssert, chDbgCheck, chDbgCheckClassS(), chSchGoSleepS(), ch_semaphore::cnt, currp, MSG_OK, ch_semaphore::queue, queue_isempty(), and queue_notempty().
Referenced by chBSemWaitS(), and chSemWait().
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 235 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 264 of file chsem.c.
References CH_STATE_WTSEM, chDbgAssert, chDbgCheck, chDbgCheckClassS(), chSchGoSleepTimeoutS(), ch_semaphore::cnt, currp, MSG_OK, MSG_TIMEOUT, ch_semaphore::queue, queue_isempty(), queue_notempty(), 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 294 of file chsem.c.
References chDbgAssert, chDbgCheck, chSchWakeupS(), chSysLock(), chSysUnlock(), ch_semaphore::cnt, MSG_OK, ch_semaphore::queue, queue_fifo_remove(), queue_isempty(), and queue_notempty().
void chSemSignalI | ( | semaphore_t * | sp | ) |
Performs a signal operation on a semaphore.
[in] | sp | pointer to a semaphore_t structure |
Definition at line 319 of file chsem.c.
References chDbgAssert, chDbgCheck, chDbgCheckClassI(), chSchReadyI(), ch_semaphore::cnt, MSG_OK, ch_semaphore::queue, queue_fifo_remove(), queue_isempty(), queue_notempty(), 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 349 of file chsem.c.
References chDbgAssert, chDbgCheck, chDbgCheckClassI(), chSchReadyI(), ch_semaphore::cnt, MSG_OK, ch_semaphore::queue, queue_fifo_remove(), queue_isempty(), queue_notempty(), 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 378 of file chsem.c.
References CH_STATE_WTSEM, chDbgAssert, chDbgCheck, chSchGoSleepS(), chSchReadyI(), chSchRescheduleS(), chSysLock(), chSysUnlock(), ch_semaphore::cnt, currp, MSG_OK, ch_semaphore::queue, queue_fifo_remove(), queue_isempty(), queue_notempty(), ch_thread::rdymsg, ch_thread::u, and ch_thread::wtsemp.
|
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 118 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 134 of file chsem.h.
References chDbgCheckClassI(), and ch_semaphore::cnt.
Referenced by chSchGoSleepS().
|
inlinestatic |
Returns the semaphore counter current value.
[in] | sp | pointer to a semaphore_t structure |
Definition at line 149 of file chsem.h.
References chDbgCheckClassI(), and ch_semaphore::cnt.
Referenced by chGuardedPoolAllocI().