FreeRTOS C++ Wrappers  1.6.0
C++ interface to FreeRTOS
Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
cpp_freertos::Thread Class Referenceabstract

#include <thread.hpp>

Inheritance diagram for cpp_freertos::Thread:
Inheritance graph
[legend]
Collaboration diagram for cpp_freertos::Thread:
Collaboration graph
[legend]

Public Member Functions

 Thread (const std::string Name, uint16_t StackDepth, UBaseType_t Priority)
 
 Thread (uint16_t StackDepth, UBaseType_t Priority)
 
bool Start ()
 
virtual ~Thread ()
 
TaskHandle_t GetHandle ()
 
void Suspend ()
 
void Resume ()
 
void ResumeFromISR ()
 
UBaseType_t GetPriority ()
 
UBaseType_t GetPriorityFromISR ()
 
void SetPriority (UBaseType_t NewPriority)
 
std::string GetName ()
 

Static Public Member Functions

static void Yield ()
 
static void StartScheduler ()
 
static void EndScheduler ()
 

Protected Member Functions

virtual void Run ()=0
 
virtual void Cleanup ()
 
void Delay (const TickType_t Delay)
 
void DelayUntil (const TickType_t Period)
 
void ResetDelayUntil ()
 
bool Wait (ConditionVariable &Cv, Mutex &CvLock, TickType_t Timeout=portMAX_DELAY)
 

Private Member Functions

void Signal ()
 

Static Private Member Functions

static void TaskFunctionAdapter (void *pvParameters)
 

Private Attributes

TaskHandle_t handle
 
const std::string Name
 
const uint16_t StackDepth
 
UBaseType_t Priority
 
bool ThreadStarted
 
bool delayUntilInitialized
 
TickType_t delayUntilPreviousWakeTime
 
BinarySemaphore ThreadWaitSem
 

Static Private Attributes

static volatile bool SchedulerActive = false
 
static MutexStandard StartGuardLock
 

Friends

class ConditionVariable
 

Detailed Description

Wrapper class around FreeRTOS's implementation of a task.

This is an abstract base class. To use this, you need to subclass it. All of your threads should be derived from the Thread class. Then implement the virtual Run function. This is a similar design to Java threading.

By default, we leverage C++ strings for the Thread Name. If this is not desirable, define CPP_FREERTOS_NO_CPP_STRINGS and the class will fall back to C character arrays.

Definition at line 77 of file thread.hpp.

Constructor & Destructor Documentation

◆ Thread() [1/2]

Thread::Thread ( const std::string  Name,
uint16_t  StackDepth,
UBaseType_t  Priority 
)

Constructor to create a named thread.

Parameters
NameName of the thread. Only useful for debugging.
StackDepthNumber of "words" allocated for the Thread stack.
PriorityFreeRTOS priority of this Thread.

Definition at line 56 of file cthread.cpp.

◆ Thread() [2/2]

Thread::Thread ( uint16_t  StackDepth,
UBaseType_t  Priority 
)

Constructor to create an unnamed thread.

Parameters
StackDepthNumber of "words" allocated for the Thread stack.
PriorityFreeRTOS priority of this Thread.

Definition at line 70 of file cthread.cpp.

◆ ~Thread()

Thread::~Thread ( )
virtual

Our destructor. This must exist even if FreeRTOS is configured to disallow task deletion.

Definition at line 184 of file cthread.cpp.

Member Function Documentation

◆ Cleanup()

void Thread::Cleanup ( )
protectedvirtual

Called on exit from your Run() routine.

It is optional whether you implement this or not.

If you allow your Thread to exit its Run method, implementing a Cleanup method allows you to call your Thread's destructor. If you decide to call delete in your Cleanup function, be aware that additional derived classes shouldn't call delete.

Definition at line 179 of file cthread.cpp.

◆ Delay()

void cpp_freertos::Thread::Delay ( const TickType_t  Delay)
inlineprotected

Delay this thread for at least Delay ticks.

Parameters
DelayHow long to delay the thread.

Definition at line 314 of file thread.hpp.

◆ DelayUntil()

void Thread::DelayUntil ( const TickType_t  Period)
protected

Delay this thread for Period ticks, taking into account the execution time of the thread.

This FreeRTOS permutation of delay can be used by periodic tasks to ensure a constant execution frequency.

Parameters
PeriodHow long to delay the thread.

Definition at line 222 of file cthread.cpp.

◆ EndScheduler()

static void cpp_freertos::Thread::EndScheduler ( )
inlinestatic

End the scheduler.

Note
Please see the FreeRTOS documentation regarding constraints with the implementation of this.
You need to use this call. Do NOT directly call vTaskEndScheduler while using this library.

Definition at line 177 of file thread.hpp.

◆ GetHandle()

TaskHandle_t cpp_freertos::Thread::GetHandle ( )
inline

Accessor to get the thread's backing task handle. There is no setter, on purpose.

Returns
FreeRTOS task handle.

Definition at line 143 of file thread.hpp.

◆ GetName()

std::string cpp_freertos::Thread::GetName ( )
inline

Get the name of this thread.

Returns
a C++ string with the name of the task.

Definition at line 256 of file thread.hpp.

◆ GetPriority()

UBaseType_t cpp_freertos::Thread::GetPriority ( )
inline

Get the priority of this Thread.

Returns
Priority at the time this was called.

Definition at line 220 of file thread.hpp.

◆ GetPriorityFromISR()

UBaseType_t cpp_freertos::Thread::GetPriorityFromISR ( )
inline

Get the priority of this Thread from ISR context.

Returns
Priority at the time this was called.

Definition at line 230 of file thread.hpp.

◆ ResetDelayUntil()

void Thread::ResetDelayUntil ( )
protected

If you need to adjust or reset the period of the DelayUntil method.

Definition at line 233 of file cthread.cpp.

◆ Resume()

void cpp_freertos::Thread::Resume ( )
inline

Resume a specific thread.

Definition at line 198 of file thread.hpp.

◆ ResumeFromISR()

void cpp_freertos::Thread::ResumeFromISR ( )
inline

Resume a specific thread from ISR context.

Definition at line 208 of file thread.hpp.

◆ Run()

virtual void cpp_freertos::Thread::Run ( )
protectedpure virtual

Implementation of your actual thread code. You must override this function.

Note
If INCLUDE_vTaskDelete is defined, then you may return from your Run method. This will cause the task to be deleted from FreeRTOS, however you are still responsible to delete the task object. If this is not defined, then retuning from your Run() method will result in an assert.

Implemented in cpp_freertos::WorkQueue::CWorkerThread.

◆ SetPriority()

void cpp_freertos::Thread::SetPriority ( UBaseType_t  NewPriority)
inline

Set the priority of this thread.

Parameters
NewPriorityThe thread's new priority.

Definition at line 243 of file thread.hpp.

◆ Signal()

void cpp_freertos::Thread::Signal ( )
inlineprivate

Internal helper function to signal this thread.

Definition at line 442 of file thread.hpp.

◆ Start()

bool Thread::Start ( )

Starts a thread.

This is the API call that actually starts the thread running. It creates a backing FreeRTOS task. By separating object creation from starting the Thread, it solves the pure virtual fuction call failure case. If we attempt to automatically call xTaskCreate from the base class constructor, in certain conditions the task starts to run "before" the derived class is constructed! So we don't do that anymore.

This may be called from your ctor once you have completed your objects construction (so as the last step).

This should only be called once ever!

Definition at line 123 of file cthread.cpp.

◆ StartScheduler()

static void cpp_freertos::Thread::StartScheduler ( )
inlinestatic

Start the scheduler.

Note
You need to use this call. Do NOT directly call vTaskStartScheduler while using this library.

Definition at line 162 of file thread.hpp.

◆ Suspend()

void cpp_freertos::Thread::Suspend ( )
inline

Suspend this thread.

Note
While a Thread can Suspend() itself, it cannot Resume() itself, becauseit's suspended.

Definition at line 190 of file thread.hpp.

◆ TaskFunctionAdapter()

void Thread::TaskFunctionAdapter ( void *  pvParameters)
staticprivate

Adapter function that allows you to write a class specific Run() function that interfaces with FreeRTOS. Look at the implementation of the constructors and this code to see how the interface between C and C++ is performed.

Definition at line 201 of file cthread.cpp.

◆ Wait()

bool Thread::Wait ( ConditionVariable Cv,
Mutex CvLock,
TickType_t  Timeout = portMAX_DELAY 
)
protected

Have this thread wait on a condition variable.

Note
Threads wait, while ConditionVariables signal.
Parameters
CvThe condition variable associated with the Wait.
CvLockThe required condition variable lock. The Lock must be held before calling Wait.
TimeoutAllows you to specify a timeout on the Wait, if desired.
Returns
true if the condition variable was signaled, false if it timed out.

Definition at line 244 of file cthread.cpp.

◆ Yield()

static void cpp_freertos::Thread::Yield ( )
inlinestatic

Yield the scheduler.

Definition at line 151 of file thread.hpp.

Friends And Related Function Documentation

◆ ConditionVariable

friend class ConditionVariable
friend

The Thread class and the ConditionVariable class are interdependent. If we allow the ConditionVariable class to access the internals of the Thread class, we can reduce the public interface, which is a good thing.

Definition at line 453 of file thread.hpp.

Member Data Documentation

◆ delayUntilInitialized

bool cpp_freertos::Thread::delayUntilInitialized
private

Flag denoting if we've setup delay until yet.

Definition at line 422 of file thread.hpp.

◆ delayUntilPreviousWakeTime

TickType_t cpp_freertos::Thread::delayUntilPreviousWakeTime
private

Book keeping value for delay until.

Definition at line 427 of file thread.hpp.

◆ handle

TaskHandle_t cpp_freertos::Thread::handle
private

Reference to the underlying task handle for this thread. Can be obtained from GetHandle().

Definition at line 374 of file thread.hpp.

◆ Name

const std::string cpp_freertos::Thread::Name
private

The name of this thread.

Definition at line 385 of file thread.hpp.

◆ Priority

UBaseType_t cpp_freertos::Thread::Priority
private

A saved / cached copy of what the Thread's priority is.

Definition at line 398 of file thread.hpp.

◆ SchedulerActive

volatile bool Thread::SchedulerActive = false
staticprivate

We need to track whether the scheduler is active or not.

Definition at line 379 of file thread.hpp.

◆ StackDepth

const uint16_t cpp_freertos::Thread::StackDepth
private

Stack depth of this Thread, in words.

Definition at line 393 of file thread.hpp.

◆ StartGuardLock

MutexStandard Thread::StartGuardLock
staticprivate

Make sure no one calls Start more than once.

Definition at line 408 of file thread.hpp.

◆ ThreadStarted

bool cpp_freertos::Thread::ThreadStarted
private

Flag whether or not the Thread was started.

Definition at line 403 of file thread.hpp.

◆ ThreadWaitSem

BinarySemaphore cpp_freertos::Thread::ThreadWaitSem
private

How we wait and signal the thread when using condition variables. Because a semaphore maintains state, this solves the race condition between dropping the CvLock and waiting.

Definition at line 437 of file thread.hpp.


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