ChibiOS/RT 7.0.5
|
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
.Data Structures | |
struct | ch_semaphore |
Semaphore structure. More... |
Macros | |
#define | sem_insert(qp, tp) |
#define | __SEMAPHORE_DATA(name, n) |
Data part of a static semaphore initializer. | |
#define | SEMAPHORE_DECL(name, n) |
Static semaphore initializer. |
Typedefs | |
typedef struct ch_semaphore | semaphore_t |
Semaphore structure. |
Functions | |
void | chSemObjectInit (semaphore_t *sp, cnt_t n) |
Initializes a semaphore with the specified counter value. | |
void | chSemResetWithMessage (semaphore_t *sp, cnt_t n, msg_t msg) |
Performs a reset operation on the semaphore. | |
void | chSemResetWithMessageI (semaphore_t *sp, cnt_t n, msg_t msg) |
Performs a reset operation on the semaphore. | |
msg_t | chSemWait (semaphore_t *sp) |
Performs a wait operation on a semaphore. | |
msg_t | chSemWaitS (semaphore_t *sp) |
Performs a wait operation on a semaphore. | |
msg_t | chSemWaitTimeout (semaphore_t *sp, sysinterval_t timeout) |
Performs a wait operation on a semaphore with timeout specification. | |
msg_t | chSemWaitTimeoutS (semaphore_t *sp, sysinterval_t timeout) |
Performs a wait operation on a semaphore with timeout specification. | |
void | chSemSignal (semaphore_t *sp) |
Performs a signal operation on a semaphore. | |
void | chSemSignalI (semaphore_t *sp) |
Performs a signal operation on a semaphore. | |
void | chSemAddCounterI (semaphore_t *sp, cnt_t n) |
Adds the specified value to the semaphore counter. | |
msg_t | chSemSignalWait (semaphore_t *sps, semaphore_t *spw) |
Performs atomic signal and wait operations on two semaphores. | |
static void | chSemReset (semaphore_t *sp, cnt_t n) |
Performs a reset operation on the semaphore. | |
static void | chSemResetI (semaphore_t *sp, cnt_t n) |
Performs a reset operation on the semaphore. | |
static void | chSemFastWaitI (semaphore_t *sp) |
Decreases the semaphore counter. | |
static void | chSemFastSignalI (semaphore_t *sp) |
Increases the semaphore counter. | |
static cnt_t | chSemGetCounterI (const semaphore_t *sp) |
Returns the semaphore counter current value. |
#define sem_insert | ( | qp, | |
tp ) |
Definition at line 79 of file chsem.c.
Referenced by chSemSignalWait(), chSemWaitS(), and chSemWaitTimeoutS().
#define __SEMAPHORE_DATA | ( | name, | |
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 ) |
Static semaphore initializer.
Statically initialized semaphores require no explicit initialization using chSemInit()
.
[in] | name | the name of the semaphore variable |
[in] | n | the counter initial value, this value must be non-negative |
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(), __heap_init(), chBSemObjectInit(), chCacheObjectInit(), chFactoryCreateSemaphore(), chGuardedPoolObjectInitAligned(), and chHeapObjectInit().
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, threadref, 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(), CH_STATE_WTSEM, chDbgAssert, chDbgCheck, chDbgCheckClassS, chSchGoSleepS(), chThdGetSelfX(), ch_semaphore::cnt, MSG_OK, ch_semaphore::queue, ch_thread::rdymsg, sem_insert, ch_thread::u, and ch_thread::wtsemp.
Referenced by chBSemWaitS(), chCacheGetObject(), 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 230 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 259 of file chsem.c.
References ch_queue_isempty(), ch_queue_notempty(), CH_STATE_WTSEM, chDbgAssert, chDbgCheck, chDbgCheckClassS, chSchGoSleepTimeoutS(), chThdGetSelfX(), ch_semaphore::cnt, MSG_OK, MSG_TIMEOUT, ch_semaphore::queue, sem_insert, TIME_IMMEDIATE, ch_thread::u, unlikely, and ch_thread::wtsemp.
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 290 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, ch_semaphore::queue, and threadref.
void chSemSignalI | ( | semaphore_t * | sp | ) |
Performs a signal operation on a semaphore.
[in] | sp | pointer to a semaphore_t structure |
Definition at line 315 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, threadref, and ch_thread::u.
Referenced by chBSemSignalI(), chCacheReleaseObjectI(), 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 345 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, threadref, 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 374 of file chsem.c.
References ch_queue_fifo_remove(), ch_queue_isempty(), ch_queue_notempty(), CH_STATE_WTSEM, chDbgAssert, chDbgCheck, chSchGoSleepS(), chSchReadyI(), chSchRescheduleS(), chSysLock(), chSysUnlock(), chThdGetSelfX(), ch_semaphore::cnt, MSG_OK, ch_semaphore::queue, ch_thread::rdymsg, sem_insert, threadref, ch_thread::u, and ch_thread::wtsemp.
|
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 chCacheGetObject(), chGuardedPoolAllocI(), and lru_get_last_s().
|
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.
Referenced by __sch_wakeup(), and chCacheReleaseObjectI().
|
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 chCacheGetObject(), chCacheReleaseObjectI(), chGuardedPoolAllocI(), chGuardedPoolGetCounterI(), and lru_get_last_s().