FreeRTOS-Cpp
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Private Member Functions | Friends | List of all members
FreeRTOS::RecursiveMutexBase Class Reference

Base class that provides the recursive mutex interface to FreeRTOS::RecursiveMutex and FreeRTOS::StaticRecursiveMutex. This class exists to override the lock() and unlock() functions which require different underlying functions from what is used in FreeRTOS::MutexBase. More...

#include <FreeRTOS/Mutex.hpp>

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

Public Member Functions

 RecursiveMutexBase (const RecursiveMutexBase &)=delete
 
RecursiveMutexBaseoperator= (const RecursiveMutexBase &)=delete
 
bool lock (const TickType_t ticksToWait=portMAX_DELAY) const
 Function that calls xSemaphoreTakeRecursive( SemaphoreHandle_t xMutex, TickType_t xTicksToWait )
 
bool unlock () const
 Function that calls xSemaphoreGiveRecursive( SemaphoreHandle_t xSemaphore )
 
- Public Member Functions inherited from FreeRTOS::MutexBase
 MutexBase (const MutexBase &)=delete
 
MutexBaseoperator= (const MutexBase &)=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.
 
bool lock (const TickType_t ticksToWait=portMAX_DELAY) const
 Function that calls xSemaphoreTake( SemaphoreHandle_t xSemaphore, TickType_t xTicksToWait )
 
bool lockFromISR (bool &higherPriorityTaskWoken) const
 Function that calls xSemaphoreTakeFromISR ( SemaphoreHandle_t xSemaphore, signed BaseType_t *pxHigherPriorityTaskWoken )
 
bool lockFromISR () const
 Function that calls xSemaphoreTakeFromISR ( SemaphoreHandle_t xSemaphore, signed BaseType_t *pxHigherPriorityTaskWoken )
 
bool unlock () const
 Function that calls xSemaphoreGive( SemaphoreHandle_t xSemaphore )
 

Static Public Member Functions

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

Private Member Functions

 RecursiveMutexBase (RecursiveMutexBase &&) noexcept=default
 
RecursiveMutexBaseoperator= (RecursiveMutexBase &&) noexcept=default
 

Friends

class RecursiveMutex
 
class StaticRecursiveMutex
 

Detailed Description

Base class that provides the recursive mutex interface to FreeRTOS::RecursiveMutex and FreeRTOS::StaticRecursiveMutex. This class exists to override the lock() and unlock() functions which require different underlying functions from what is used in FreeRTOS::MutexBase.

Note
This class is not intended to be instantiated by the user. Use FreeRTOS::RecursiveMutex or FreeRTOS::StaticRecursiveMutex.

Member Function Documentation

◆ lock()

bool FreeRTOS::RecursiveMutexBase::lock ( const TickType_t  ticksToWait = portMAX_DELAY) const
inline

Function that calls xSemaphoreTakeRecursive( SemaphoreHandle_t xMutex, TickType_t xTicksToWait )

Mutex.hpp

See also
https://www.freertos.org/xSemaphoreTakeRecursive.html
Parameters
ticksToWaitThe time in ticks to wait for the mutex to become available. The macro portTICK_PERIOD_MS can be used to convert this to a real time. A block time of zero can be used to poll the mutex. If the task already owns the mutex then take() will return immediately no matter what the value of ticksToWait.
Return values
trueIf the mutex was locked.
falseIf ticksToWait expired without the mutex becoming available.

Example Usage

#include <FreeRTOS/Mutex.hpp>
#include <FreeRTOS/Task.hpp>
class MyTask : public FreeRTOS::Task {
public:
void taskFunction() final;
};
class MyOtherTask : public FreeRTOS::Task {
public:
void taskFunction() final;
};
FreeRTOS::StaticMutex mutex;
// A task that uses the mutex.
void MyTask::taskFunction() {
// Do something
}
// Another task that uses the mutex.
void MyOtherTask::taskFunction() {
if (mutex.isValid()) {
// See if we can lock the mutex. If the mutex is not available wait 10
// ticks to see if it becomes free.
if (mutex.lock()) {
// We were able to obtain the mutex and can now access the shared
// resource.
// ...
// For some reason due to the nature of the code further calls to take()
// are made on the same mutex. In real code these would not be just
// sequential calls as this would make no sense. Instead the calls are
// likely to be buried inside a more complex call structure.
mutex.lock(10);
mutex.lock(10);
// The mutex has now been locked three times, so will not be available to
// another task until it has also been given back three times. Again it
// is unlikely that real code would have these calls sequentially, but
// instead buried in a more complex call structure. This is just for
// illustrative purposes.
mutex.unlock();
mutex.unlock();
mutex.unlock();
// Now the mutex can be taken by other tasks.
} else {
// We could not lock the mutex and can therefore not access the shared
// resource safely.
}
}
}
bool lock(const TickType_t ticksToWait=portMAX_DELAY) const
Function that calls xSemaphoreTake( SemaphoreHandle_t xSemaphore, TickType_t xTicksToWait )
Definition Mutex.hpp:99
Class that encapsulates the functionality of a FreeRTOS mutex.
Definition Mutex.hpp:417
Class that encapsulates the functionality of a FreeRTOS task.
Definition Task.hpp:1427

◆ unlock()

bool FreeRTOS::RecursiveMutexBase::unlock ( ) const
inline

Function that calls xSemaphoreGiveRecursive( SemaphoreHandle_t xSemaphore )

Mutex.hpp

See also
https://www.freertos.org/xSemaphoreGiveRecursive.html

A mutex used recursively can be locked repeatedly by the owner. The mutex doesn't become available again until the owner has called unlock() for each successful lock request. For example, if a task successfully locks the same mutex 5 times then the mutex will not be available to any other task until it has also unlocked the mutex back exactly five times.

Returns
true If the mutex was unlocked.
false Otherwise.

Example Usage

#include <FreeRTOS/Mutex.hpp>
#include <FreeRTOS/Task.hpp>
class MyTask : public FreeRTOS::Task {
public:
void taskFunction() final;
};
class MyOtherTask : public FreeRTOS::Task {
public:
void taskFunction() final;
};
FreeRTOS::StaticMutex mutex;
// A task that uses the mutex.
void MyTask::taskFunction() {
// Do something
}
// Another task that uses the mutex.
void MyOtherTask::taskFunction() {
if (mutex.isValid()) {
// See if we can lock the mutex. If the mutex is not available wait 10
// ticks to see if it becomes free.
if (mutex.lock()) {
// We were able to obtain the mutex and can now access the shared
// resource.
// ...
// For some reason due to the nature of the code further calls to take()
// are made on the same mutex. In real code these would not be just
// sequential calls as this would make no sense. Instead the calls are
// likely to be buried inside a more complex call structure.
mutex.lock(10);
mutex.lock(10);
// The mutex has now been locked three times, so will not be available to
// another task until it has also been given back three times. Again it
// is unlikely that real code would have these calls sequentially, but
// instead buried in a more complex call structure. This is just for
// illustrative purposes.
mutex.unlock();
mutex.unlock();
mutex.unlock();
// Now the mutex can be taken by other tasks.
} else {
// We could not lock the mutex and can therefore not access the shared
// resource safely.
}
}
}

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