|
ChibiOS/RT
2.5.1 |
|
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_USE_SEMAPHORES_PRIORITY in chconf.h.
CH_USE_SEMAPHORES option must be enabled in chconf.h. Data Structures | |
| struct | Semaphore |
| Semaphore structure. More... | |
Functions | |
| void | chSemInit (Semaphore *sp, cnt_t n) |
| Initializes a semaphore with the specified counter value. | |
| void | chSemReset (Semaphore *sp, cnt_t n) |
| Performs a reset operation on the semaphore. | |
| void | chSemResetI (Semaphore *sp, cnt_t n) |
| Performs a reset operation on the semaphore. | |
| msg_t | chSemWait (Semaphore *sp) |
| Performs a wait operation on a semaphore. | |
| msg_t | chSemWaitS (Semaphore *sp) |
| Performs a wait operation on a semaphore. | |
| msg_t | chSemWaitTimeout (Semaphore *sp, systime_t time) |
| Performs a wait operation on a semaphore with timeout specification. | |
| msg_t | chSemWaitTimeoutS (Semaphore *sp, systime_t time) |
| Performs a wait operation on a semaphore with timeout specification. | |
| void | chSemSignal (Semaphore *sp) |
| Performs a signal operation on a semaphore. | |
| void | chSemSignalI (Semaphore *sp) |
| Performs a signal operation on a semaphore. | |
| void | chSemAddCounterI (Semaphore *sp, cnt_t n) |
| Adds the specified value to the semaphore counter. | |
| msg_t | chSemSignalWait (Semaphore *sps, Semaphore *spw) |
| Performs atomic signal and wait operations on two semaphores. | |
Macro Functions | |
| #define | chSemFastWaitI(sp) ((sp)->s_cnt--) |
| Decreases the semaphore counter. | |
| #define | chSemFastSignalI(sp) ((sp)->s_cnt++) |
| Increases the semaphore counter. | |
| #define | chSemGetCounterI(sp) ((sp)->s_cnt) |
| Returns the semaphore counter current value. | |
Defines | |
| #define | _SEMAPHORE_DATA(name, n) {_THREADSQUEUE_DATA(name.s_queue), n} |
| Data part of a static semaphore initializer. | |
| #define | SEMAPHORE_DECL(name, n) Semaphore name = _SEMAPHORE_DATA(name, n) |
| Static semaphore initializer. | |
Typedefs | |
| typedef struct Semaphore | Semaphore |
| Semaphore structure. | |
Initializes a semaphore with the specified counter value.
| [out] | sp | pointer to a Semaphore structure |
| [in] | n | initial value of the semaphore counter. Must be non-negative. |
Definition at line 79 of file chsem.c.
References chDbgCheck, queue_init, Semaphore::s_cnt, and Semaphore::s_queue.
Referenced by _heap_init(), chHeapInit(), and chMBInit().
Performs a reset operation on the semaphore.
chSemWait() will return RDY_RESET instead of RDY_OK.| [in] | sp | pointer to a Semaphore structure |
| [in] | n | the new value of the semaphore counter. The value must be non-negative. |
Definition at line 102 of file chsem.c.
References chSchRescheduleS(), chSemResetI(), chSysLock, and chSysUnlock.

Performs a reset operation on the semaphore.
chSemWait() will return RDY_RESET instead of RDY_OK.| [in] | sp | pointer to a Semaphore structure |
| [in] | n | the new value of the semaphore counter. The value must be non-negative. |
Definition at line 129 of file chsem.c.
References chDbgAssert, chDbgCheck, chDbgCheckClassI(), chSchReadyI(), isempty, lifo_remove(), notempty, RDY_RESET, Thread::rdymsg, Semaphore::s_cnt, and Semaphore::s_queue.
Referenced by chMBReset(), and chSemReset().

Performs a wait operation on a semaphore.
| [in] | sp | pointer to a Semaphore structure |
| RDY_OK | if the thread has not stopped on the semaphore or the semaphore has been signaled. |
| RDY_RESET | if the semaphore has been reset using chSemReset(). |
Definition at line 157 of file chsem.c.
References chSemWaitS(), chSysLock, and chSysUnlock.

Performs a wait operation on a semaphore.
| [in] | sp | pointer to a Semaphore structure |
| RDY_OK | if the thread has not stopped on the semaphore or the semaphore has been signaled. |
| RDY_RESET | if the semaphore has been reset using chSemReset(). |
Definition at line 178 of file chsem.c.
References chDbgAssert, chDbgCheck, chDbgCheckClassS(), chSchGoSleepS(), currp, isempty, notempty, RDY_OK, Semaphore::s_cnt, Semaphore::s_queue, and THD_STATE_WTSEM.
Referenced by chSemWait().

Performs a wait operation on a semaphore with timeout specification.
| [in] | sp | pointer to a Semaphore structure |
| [in] | time | the number of ticks before the operation timeouts, the following special values are allowed:
|
| RDY_OK | if the thread has not stopped on the semaphore or the semaphore has been signaled. |
| RDY_RESET | if the semaphore has been reset using chSemReset(). |
| RDY_TIMEOUT | if the semaphore has not been signaled or reset within the specified timeout. |
Definition at line 215 of file chsem.c.
References chSemWaitTimeoutS(), chSysLock, and chSysUnlock.

Performs a wait operation on a semaphore with timeout specification.
| [in] | sp | pointer to a Semaphore structure |
| [in] | time | the number of ticks before the operation timeouts, the following special values are allowed:
|
| RDY_OK | if the thread has not stopped on the semaphore or the semaphore has been signaled. |
| RDY_RESET | if the semaphore has been reset using chSemReset(). |
| RDY_TIMEOUT | if the semaphore has not been signaled or reset within the specified timeout. |
Definition at line 243 of file chsem.c.
References chDbgAssert, chDbgCheck, chDbgCheckClassS(), chSchGoSleepTimeoutS(), currp, isempty, notempty, RDY_OK, RDY_TIMEOUT, Semaphore::s_cnt, Semaphore::s_queue, THD_STATE_WTSEM, and TIME_IMMEDIATE.
Referenced by chMBFetchS(), chMBPostAheadS(), chMBPostS(), and chSemWaitTimeout().

| void chSemSignal | ( | Semaphore * | sp | ) |
Performs a signal operation on a semaphore.
| [in] | sp | pointer to a Semaphore structure |
Definition at line 271 of file chsem.c.
References chDbgAssert, chDbgCheck, chSchWakeupS(), chSysLock, chSysUnlock, fifo_remove(), isempty, notempty, RDY_OK, Semaphore::s_cnt, and Semaphore::s_queue.

| void chSemSignalI | ( | Semaphore * | sp | ) |
Performs a signal operation on a semaphore.
| [in] | sp | pointer to a Semaphore structure |
Definition at line 296 of file chsem.c.
References chDbgAssert, chDbgCheck, chDbgCheckClassI(), chSchReadyI(), fifo_remove(), isempty, notempty, RDY_OK, Thread::rdymsg, Semaphore::s_cnt, and Semaphore::s_queue.
Referenced by chMBFetchI(), chMBFetchS(), chMBPostAheadI(), chMBPostAheadS(), chMBPostI(), and chMBPostS().

Adds the specified value to the semaphore counter.
| [in] | sp | pointer to a Semaphore structure |
| [in] | n | value to be added to the semaphore counter. The value must be positive. |
Definition at line 327 of file chsem.c.
References chDbgAssert, chDbgCheck, chDbgCheckClassI(), chSchReadyI(), fifo_remove(), isempty, notempty, RDY_OK, Thread::rdymsg, Semaphore::s_cnt, and Semaphore::s_queue.

Performs atomic signal and wait operations on two semaphores.
CH_USE_SEMSW must be enabled in order to use this function.| [in] | sps | pointer to a Semaphore structure to be signaled |
| [in] | spw | pointer to a Semaphore structure to be wait on |
| RDY_OK | if the thread has not stopped on the semaphore or the semaphore has been signaled. |
| RDY_RESET | if the semaphore has been reset using chSemReset(). |
Definition at line 359 of file chsem.c.
References chDbgAssert, chDbgCheck, chSchGoSleepS(), chSchReadyI(), chSchRescheduleS(), chSysLock, chSysUnlock, currp, fifo_remove(), isempty, notempty, RDY_OK, Thread::rdymsg, Semaphore::s_cnt, Semaphore::s_queue, THD_STATE_WTSEM, and Thread::wtobjp.

| #define _SEMAPHORE_DATA | ( | name, | |
| n | |||
| ) | {_THREADSQUEUE_DATA(name.s_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 name = _SEMAPHORE_DATA(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 |
| #define chSemFastWaitI | ( | sp | ) | ((sp)->s_cnt--) |
Decreases the semaphore counter.
This macro can be used when the counter is known to be positive.
Definition at line 95 of file chsem.h.
Referenced by chMBFetchI(), chMBPostAheadI(), and chMBPostI().
| #define chSemFastSignalI | ( | sp | ) | ((sp)->s_cnt++) |
| #define chSemGetCounterI | ( | sp | ) | ((sp)->s_cnt) |
Returns the semaphore counter current value.
Definition at line 111 of file chsem.h.
Referenced by chMBFetchI(), chMBPostAheadI(), and chMBPostI().