Counting semaphore for task synchronization and resource management.
More...
#include <nbrtos.h>
Inherits OS_TASK_DLY_OBJ.
|
| | OS_SEM (int32_t cnt=0) |
| | Construct and initialize the semaphore with a starting count.
|
| |
| uint8_t | Init (int32_t cnt=0) |
| | Reset the semaphore to its initial state with the specified count.
|
| |
| uint8_t | Post () |
| | Increment the semaphore count by one, releasing any waiting tasks.
|
| |
| uint8_t | Pend (uint32_t timeoutTicks=WAIT_FOREVER) |
| | Wait for the semaphore count to become non-zero, then decrement it.
|
| |
| uint8_t | PendUntil (uint32_t timeout_time) |
| | Wait until an absolute TimeTick value for the semaphore count to become non-zero.
|
| |
| uint8_t | Pend (TickTimeout &t) |
| | Wait for the semaphore count to become non-zero, using a TickTimeout.
|
| |
| uint8_t | PendNoWait () |
| | Attempt to decrement the semaphore count without waiting.
|
| |
| uint32_t | Avail () |
| | Get the number of available (un-consumed) semaphore counts.
|
| |
Counting semaphore for task synchronization and resource management.
A semaphore maintains an integer count that is incremented by Post() and decremented by Pend(). When the count is zero, a Pend() call will block the calling task until another task (or ISR) calls Post(). Binary semaphores (initialized to 0 or 1) can be used for event signaling, while counting semaphores track the availability of multiple identical resources.
- See also
- OS_MBOX, OS_FLAGS
Expand for Example Usage
Examples
- Binary Semaphore for Event Signaling
void ISR_Handler()
{
gEventSem.Post();
}
void WaitingTask(void *pd)
{
while (1)
{
gEventSem.Pend();
}
}
Counting semaphore for task synchronization and resource management.
Definition nbrtos.h:550
◆ OS_SEM()
| OS_SEM::OS_SEM |
( |
int32_t | cnt = 0 | ) |
|
|
inline |
Construct and initialize the semaphore with a starting count.
- Parameters
-
| cnt | Initial count value (default: 0). |
- See also
- Init()
◆ Avail()
| uint32_t OS_SEM::Avail |
( |
| ) |
|
|
inline |
Get the number of available (un-consumed) semaphore counts.
Returns the difference between the total count and the used count.
- Returns
- The number of available semaphore counts.
- See also
- Post(), Pend()
◆ Init()
| uint8_t OS_SEM::Init |
( |
int32_t | cnt = 0 | ) |
|
Reset the semaphore to its initial state with the specified count.
- Parameters
-
| cnt | Initial count value (default: 0). |
- Return values
-
- See also
- NBRTOS Error Codes
◆ Pend() [1/2]
◆ Pend() [2/2]
| uint8_t OS_SEM::Pend |
( |
uint32_t | timeoutTicks = WAIT_FOREVER | ) |
|
|
inline |
Wait for the semaphore count to become non-zero, then decrement it.
- Parameters
-
| timeoutTicks | Maximum system ticks to wait. A value of 0 (default) waits forever. |
- Return values
-
| OS_NO_ERR | If the semaphore was acquired. |
| OS_TIMEOUT | If the timeout expired. |
- See also
- PendNoWait(), PendUntil(), NBRTOS Error Codes
◆ PendNoWait()
| uint8_t OS_SEM::PendNoWait |
( |
| ) |
|
Attempt to decrement the semaphore count without waiting.
- Return values
-
| OS_NO_ERR | If the count was non-zero and was decremented. |
| OS_TIMEOUT | If the count was zero (no resource available). |
- See also
- Pend(), PendUntil(), NBRTOS Error Codes
◆ PendUntil()
| uint8_t OS_SEM::PendUntil |
( |
uint32_t | timeout_time | ) |
|
|
inline |
Wait until an absolute TimeTick value for the semaphore count to become non-zero.
- Parameters
-
| timeout_time | Absolute system TimeTick value at which to stop waiting. |
- Return values
-
| OS_NO_ERR | If the semaphore was acquired. |
| OS_TIMEOUT | If the timeout expired. |
- See also
- Pend(), PendNoWait(), NBRTOS Error Codes
◆ Post()
Increment the semaphore count by one, releasing any waiting tasks.
If one or more higher-priority tasks are pending on this semaphore, the highest-priority task will be made ready to run.
- Return values
-
| OS_NO_ERR | If successful. |
| OS_SEM_OVF | If the count would overflow. |
- See also
- Pend(), PendNoWait(), NBRTOS Error Codes
The documentation for this struct was generated from the following file: