FreeRTOS-Cpp
Public Member Functions | List of all members
FreeRTOS::CountingSemaphore Class Reference

Class that encapsulates the functionality of a FreeRTOS counting semaphore. More...

#include <FreeRTOS/Semaphore.hpp>

Inheritance diagram for FreeRTOS::CountingSemaphore:
Inheritance graph
[legend]
Collaboration diagram for FreeRTOS::CountingSemaphore:
Collaboration graph
[legend]

Public Member Functions

 CountingSemaphore (const UBaseType_t maxCount, const UBaseType_t initialCount=0)
 Construct a new CountingSemaphore by calling SemaphoreHandle_t xSemaphoreCreateCounting( UBaseType_t uxMaxCount, UBaseType_t uxInitialCount) More...
 
 CountingSemaphore (const CountingSemaphore &)=delete
 
CountingSemaphoreoperator= (const CountingSemaphore &)=delete
 
 CountingSemaphore (CountingSemaphore &&) noexcept=default
 
CountingSemaphoreoperator= (CountingSemaphore &&) noexcept=default
 
- Public Member Functions inherited from FreeRTOS::SemaphoreBase
 SemaphoreBase (const SemaphoreBase &)=delete
 
SemaphoreBaseoperator= (const SemaphoreBase &)=delete
 
bool isValid () const
 Function that checks if the underlying semaphore handle is not NULL. This should be used to ensure a semaphore has been created correctly. More...
 
UBaseType_t getCount () const
 Function that calls UBaseType_t uxSemaphoreGetCount( SemaphoreHandle_t xSemaphore ) More...
 
bool take (const TickType_t ticksToWait=portMAX_DELAY) const
 Function that calls xSemaphoreTake( SemaphoreHandle_t xSemaphore, TickType_t xTicksToWait ) More...
 
bool takeFromISR (bool &higherPriorityTaskWoken) const
 Function that calls xSemaphoreTakeFromISR( SemaphoreHandle_t xSemaphore, signed BaseType_t *pxHigherPriorityTaskWoken) More...
 
bool takeFromISR () const
 Function that calls xSemaphoreTakeFromISR( SemaphoreHandle_t xSemaphore, signed BaseType_t *pxHigherPriorityTaskWoken) More...
 
bool give () const
 Function that calls xSemaphoreGive( SemaphoreHandle_t xSemaphore ) More...
 
bool giveFromISR (bool &higherPriorityTaskWoken) const
 Function that calls xSemaphoreGiveFromISR( SemaphoreHandle_t xSemaphore, signed BaseType_t *pxHigherPriorityTaskWoken ) More...
 
bool giveFromISR () const
 Function that calls xSemaphoreGiveFromISR( SemaphoreHandle_t xSemaphore, signed BaseType_t *pxHigherPriorityTaskWoken ) More...
 

Additional Inherited Members

- Static Public Member Functions inherited from FreeRTOS::SemaphoreBase
static void * operator new (size_t, void *ptr)
 
static void * operator new[] (size_t, void *ptr)
 
static void * operator new (size_t)=delete
 
static void * operator new[] (size_t)=delete
 

Detailed Description

Class that encapsulates the functionality of a FreeRTOS counting semaphore.

Each counting semaphore require a small amount of RAM that is used to hold the semaphore's state. If a counting semaphore is created using FreeRTOS::CountingSemaphore then the required RAM is automatically allocated from the FreeRTOS heap. If a counting semaphore is created using FreeRTOS::StaticCountingSemaphore then the RAM is provided by the application writer as part of the class and allows the RAM to be statically allocated at compile time. See the Static Vs Dynamic allocation page for more information.

Counting semaphores are typically used for two things:

  1. Counting Events: In this usage scenario an event handler will 'give' a semaphore each time an event occurs (incrementing the semaphore count value), and a handler task will 'take' a semaphore each time it processes an event (decrementing the semaphore count value). The count value is therefore the difference between the number of events that have occurred and the number that have been processed. In this case it is desirable for the initial count value to be zero. Note the same functionality can often be achieved in a more efficient way using a direct to task notification.
  2. Resource Management: In this usage scenario the count value indicates the number of resources available. To obtain control of a resource a task must first obtain a semaphore - decrementing the semaphore count value. When the count value reaches zero there are no free resources. When a task finishes with the resource it 'gives' the semaphore back - incrementing the semaphore count value. In this case it is desirable for the initial count value to be equal to the maximum count value, indicating that all resources are free.

Constructor & Destructor Documentation

◆ CountingSemaphore()

FreeRTOS::CountingSemaphore::CountingSemaphore ( const UBaseType_t  maxCount,
const UBaseType_t  initialCount = 0 
)
inlineexplicit

Construct a new CountingSemaphore by calling SemaphoreHandle_t xSemaphoreCreateCounting( UBaseType_t uxMaxCount, UBaseType_t uxInitialCount)

Semaphore.hpp

Warning
The user should call isValid() on this object to verify that the binary semaphore was created successfully in case the memory required to create the queue could not be allocated.
Parameters
maxCountThe maximum count value that can be reached. When the semaphore reaches this value it can no longer be 'given'.
initialCountThe count value assigned to the semaphore when it is created.

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