NetBurner 3.5.7
PDF Version
OS_SEM Struct Reference

Counting semaphore for task synchronization and resource management. More...

#include <nbrtos.h>

Inherits OS_TASK_DLY_OBJ.

Public Member Functions

 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.
 

Detailed Description

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
OS_SEM gEventSem(0); // Start with count 0 (no event yet)
void ISR_Handler()
{
gEventSem.Post(); // Signal the event
}
void WaitingTask(void *pd)
{
while (1)
{
gEventSem.Pend(); // Wait forever for the event
// Handle the event...
}
}
Counting semaphore for task synchronization and resource management.
Definition nbrtos.h:550

Constructor & Destructor Documentation

◆ OS_SEM()

OS_SEM::OS_SEM ( int32_t cnt = 0)
inline

Construct and initialize the semaphore with a starting count.

Parameters
cntInitial count value (default: 0).
See also
Init()

Member Function Documentation

◆ 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
cntInitial count value (default: 0).
Return values
OS_NO_ERRIf successful.
See also
NBRTOS Error Codes

◆ Pend() [1/2]

uint8_t OS_SEM::Pend ( TickTimeout & t)

Wait for the semaphore count to become non-zero, using a TickTimeout.

Parameters
tTickTimeout controlling the maximum wait duration.
Return values
OS_NO_ERRIf the semaphore was acquired.
OS_TIMEOUTIf the timeout expired.
See also
Pend(), PendNoWait(), PendUntil(), NBRTOS Error Codes

◆ 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
timeoutTicksMaximum system ticks to wait. A value of 0 (default) waits forever.
Return values
OS_NO_ERRIf the semaphore was acquired.
OS_TIMEOUTIf 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_ERRIf the count was non-zero and was decremented.
OS_TIMEOUTIf 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_timeAbsolute system TimeTick value at which to stop waiting.
Return values
OS_NO_ERRIf the semaphore was acquired.
OS_TIMEOUTIf the timeout expired.
See also
Pend(), PendNoWait(), NBRTOS Error Codes

◆ Post()

uint8_t OS_SEM::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_ERRIf successful.
OS_SEM_OVFIf the count would overflow.
See also
Pend(), PendNoWait(), NBRTOS Error Codes

The documentation for this struct was generated from the following file: