FreeRTOS-Cpp
|
Class that encapsulates the functionality of a FreeRTOS binary semaphore. More...
#include <FreeRTOS/Semaphore.hpp>
Public Member Functions | |
StaticBinarySemaphore () | |
Construct a new StaticBinarySemaphore object by calling SemaphoreHandle_t xSemaphoreCreateBinaryStatic( StaticSemaphore_t *pxSemaphoreBuffer ) More... | |
StaticBinarySemaphore (const StaticBinarySemaphore &)=delete | |
StaticBinarySemaphore & | operator= (const StaticBinarySemaphore &)=delete |
StaticBinarySemaphore (StaticBinarySemaphore &&) noexcept=default | |
StaticBinarySemaphore & | operator= (StaticBinarySemaphore &&) noexcept=default |
![]() | |
SemaphoreBase (const SemaphoreBase &)=delete | |
SemaphoreBase & | operator= (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... | |
Private Attributes | |
StaticSemaphore_t | staticBinarySemaphore |
Additional Inherited Members | |
![]() | |
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 |
Class that encapsulates the functionality of a FreeRTOS binary semaphore.
Each binary semaphore require a small amount of RAM that is used to hold the semaphore's state. If a binary semaphore is created using FreeRTOS::BinarySemaphore then the required RAM is automatically allocated from the FreeRTOS heap. If a binary semaphore is created using FreeRTOS::StaticBinarySemaphore 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.
The semaphore is created in the 'empty' state, meaning the semaphore must first be given using the give() API function before it can subsequently be taken (obtained) using the take() function.
Binary semaphores and mutexes are very similar but have some subtle differences: Mutexes include a priority inheritance mechanism, binary semaphores do not. This makes binary semaphores the better choice for implementing synchronisation (between tasks or between tasks and an interrupt), and mutexes the better choice for implementing simple mutual exclusion.
A binary semaphore need not be given back once obtained, so task synchronisation can be implemented by one task/interrupt continuously 'giving' the semaphore while another continuously 'takes' the semaphore. This is demonstrated by the sample code on the giveFromISR() documentation page. Note the same functionality can often be achieved in a more efficient way using a direct to task notification.
The priority of a task that 'takes' a mutex can potentially be raised if another task of higher priority attempts to obtain the same mutex. The task that owns the mutex 'inherits' the priority of the task attempting to 'take' the same mutex. This means the mutex must always be 'given' back - otherwise the higher priority task will never be able to obtain the mutex, and the lower priority task will never 'disinherit' the priority. An example of a mutex being used to implement mutual exclusion is provided on the take() documentation page.
|
inline |
Construct a new StaticBinarySemaphore object by calling SemaphoreHandle_t xSemaphoreCreateBinaryStatic( StaticSemaphore_t *pxSemaphoreBuffer )
Example Usage