FreeRTOS Addons
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Member Functions | Friends | List of all members
cpp_freertos::Thread Class Referenceabstract

#include <thread.hpp>

Inheritance diagram for cpp_freertos::Thread:
cpp_freertos::Task

Public Member Functions

 Thread (const char *Name, uint16_t StackDepth, UBaseType_t Priority)
 
 Thread (const std::string Name, uint16_t StackDepth, UBaseType_t Priority)
 
 Thread (uint16_t StackDepth, UBaseType_t Priority)
 
virtual ~Thread ()
 
TaskHandle_t GetHandle ()
 
std::string GetName ()
 
char * GetName ()
 
UBaseType_t GetPriority ()
 
UBaseType_t GetPriorityFromISR ()
 
void Resume ()
 
void ResumeFromISR ()
 
void SetPriority (UBaseType_t NewPriority)
 
bool Start ()
 
bool Start (int core)
 Starts the task on the indicated core. More...
 
void Suspend ()
 

Static Public Member Functions

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

Protected Member Functions

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

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.

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.

◆ 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.

◆ ~Thread()

Thread::~Thread ( )
virtual

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

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.

◆ Delay()

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

If we can't delete a task, it makes no sense to have a Cleanup routine. Delay this thread for at least Delay ticks.

Parameters
DelayHow long to delay the thread.

◆ 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.

◆ 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.

◆ 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.

◆ GetName()

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

Get the name of this thread.

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

◆ GetPriority()

UBaseType_t cpp_freertos::Thread::GetPriority ( )
inline

Get the priority of this Thread.

Returns
Priority at the time this was called.

◆ 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.

◆ ResetDelayUntil()

void Thread::ResetDelayUntil ( )
protected

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

◆ Resume()

void cpp_freertos::Thread::Resume ( )
inline

Resume a specific thread.

◆ ResumeFromISR()

void cpp_freertos::Thread::ResumeFromISR ( )
inline

Resume a specific thread from ISR context.

◆ 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::Task.

◆ SetPriority()

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

Set the priority of this thread.

Parameters
NewPriorityThe thread's new priority.

◆ Start() [1/2]

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!

◆ Start() [2/2]

bool Thread::Start ( int  core)

Starts the task on the indicated core.

Parameters
core
Returns
true if it could be started successfully

◆ 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.

◆ Suspend()

void cpp_freertos::Thread::Suspend ( )
inline

Suspend this thread.

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

◆ 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.

◆ Yield()

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

Yield the scheduler.

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.


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