EtcPal  HEAD (unstable)
ETC Platform Abstraction Layer (EtcPal)
View other versions:
Semaphore Class Reference

Overview

A wrapper class for the EtcPal counting semaphore type.

Usage example:

void main()
{
// Install interrupt/signal handler and/or enable interrupts...
while(true)
{
if (sem.Wait())
{
// Service the interrupt.
// Because this is a counting semaphore, we will enter this block once for every time the
// interrupt is received.
}
}
}
// An interrupt or signal handler installed to handle some event
void InterruptHandler()
{
// We've received an event. Notify the service thread that a new event has occurred.
sem.PostFromIsr();
}
A wrapper class for the EtcPal counting semaphore type.
Definition: sem.h:69
bool PostFromIsr()
Post the semaphore from an interrupt context.
Definition: sem.h:140
bool Wait()
Wait for the semaphore.
Definition: sem.h:108

See sem (Counting Semaphores) for more information.

Public Member Functions

 Semaphore (unsigned int initial_count=0, unsigned int max_count=kDefaultMaxCount)
 Create a new semaphore.
 
 ~Semaphore ()
 Destroy the semaphore.
 
 Semaphore (const Semaphore &other)=delete
 
Semaphoreoperator= (const Semaphore &other)=delete
 
 Semaphore (Semaphore &&other)=delete
 
Semaphoreoperator= (Semaphore &&other)=delete
 
bool Wait ()
 Wait for the semaphore. More...
 
bool TryWait (int timeout_ms=0)
 Wait for the semaphore until either it is received or a timeout expires. More...
 
bool Post ()
 Post the semaphore. More...
 
bool PostFromIsr ()
 Post the semaphore from an interrupt context. More...
 
etcpal_sem_tget ()
 Get a reference to the underlying etcpal_sem_t type.
 

Static Public Attributes

static constexpr unsigned int kDefaultMaxCount = 50
 The default value used for the semaphore's max_count. More...
 

Member Function Documentation

◆ Post()

bool Post ( )
inline

Post the semaphore.

See etcpal_sem_post().

◆ PostFromIsr()

bool PostFromIsr ( )
inline

Post the semaphore from an interrupt context.

NOTE: Only meaningful on some platforms. See the table in sem (Counting Semaphores) and etcpal_sem_post_from_isr() for more information. On platforms on which it is not meaningful, executes the equivalent of Post().

◆ TryWait()

bool TryWait ( int  timeout_ms = 0)
inline

Wait for the semaphore until either it is received or a timeout expires.

NOTE: Timeouts other than 0 or ETCPAL_WAIT_FOREVER are typically only honored on real-time platforms. See the table in sem (Counting Semaphores) for more information. On platforms where timeouts are not honored, passing 0 for timeout_ms returns immediately whether the semaphore was available and taken, while any other value executes the equivalent of Wait().

Parameters
timeout_msHow long to wait for the semaphore, in milliseconds.
Returns
The result of etcpal_sem_timed_wait() on the underlying semaphore.

◆ Wait()

bool Wait ( )
inline

Wait for the semaphore.

Returns
The result of etcpal_sem_wait() on the underlying semaphore.

Field Documentation

◆ kDefaultMaxCount

constexpr unsigned int kDefaultMaxCount = 50
staticconstexpr

The default value used for the semaphore's max_count.

This is not honored on all platforms - see sem (Counting Semaphores) for more information.


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