Wrapper class around FreeRTOS's implementation of a task. More...

#include <mn_task.hpp>

+ Inheritance diagram for mn::basic_task:
+ Collaboration diagram for mn::basic_task:

Public Types

enum class  priority {
  Idle = MN_THREAD_CONFIG_CORE_PRIORITY_IDLE , Low = MN_THREAD_CONFIG_CORE_PRIORITY_LOW , Normal = MN_THREAD_CONFIG_CORE_PRIORITY_NORM , HalfCritical = MN_THREAD_CONFIG_CORE_PRIORITY_HALFCRT ,
  Urgent = MN_THREAD_CONFIG_CORE_PRIORITY_URGENT , Critical = MN_THREAD_CONFIG_CORE_PRIORITY_CRITICAL
}
 Task priority. More...
 
enum class  state {
  Running = 0 , Ready , Blocked , Suspended ,
  Deleted
}
 
using native_handle_type = xTaskHandle
 

Public Member Functions

 basic_task () noexcept
 
 basic_task (std::string strName, basic_task::priority uiPriority=basic_task::priority::Normal, unsigned short usStackDepth=MN_THREAD_CONFIG_MINIMAL_STACK_SIZE) noexcept
 
virtual ~basic_task ()
 Our destructor. Delete the task. More...
 
virtual int start (int uiCore=MN_THREAD_CONFIG_DEFAULT_CORE)
 Create and starts the Task. More...
 
int kill ()
 Destroy and delete the task and call the function 'on_kill'. More...
 
bool is_running ()
 Is the Task running? More...
 
std::string get_name ()
 Get the debug name of this task. More...
 
basic_task::priority get_priority ()
 Get the priority of this task. More...
 
unsigned short get_stackdepth ()
 Get the stack depth of this task. More...
 
xTaskHandle get_handle ()
 Accessor to get the task's backing task handle. There is no setter, on purpose. More...
 
int get_return_value ()
 Get the return value of this task - after run. More...
 
timespan_t get_time_since_start () const
 Get the time since start of this task. More...
 
int32_t get_id ()
 Get the FreeRTOS task Numberid of this task. More...
 
int32_t get_on_core ()
 Get the core number of this task run. More...
 
void set_priority (basic_task::priority uiPriority)
 Set the priority of this task. More...
 
void suspend ()
 Suspend this task. More...
 
void resume ()
 Resume a specific task. More...
 
bool joinable () const noexcept
 
int join (unsigned int xTickTimeout=portMAX_DELAY)
 join the task, Wait in other task to end this task. More...
 
int join (timespan_t time)
 join the task, Wait in other task to end this task. More...
 
int wait (unsigned int xTimeOut)
 Wait for start the task. More...
 
int wait (timespan_t time)
 Wait for start the task. More...
 
virtual void on_start ()
 This virtual function call on creating, use for user code It is optional whether you implement this or not. More...
 
virtual void on_kill ()
 This virtual function call on kill, use for user code It is optional whether you implement this or not. More...
 
virtual int on_task ()
 Implementation of your actual task code. You must override this function. More...
 
virtual void on_cleanup ()
 Called on exit from your on_task() routine. More...
 
state get_state ()
 Get the state of the task. More...
 
 operator xTaskHandle ()
 Operator to get the task's backing task handle. More...
 
 operator int ()
 Operator to get the ID assigned to the task. More...
 
bool operator== (const basic_task &r) const
 
bool operator!= (const basic_task &r) const
 
bool operator< (const basic_task &r) const
 
bool operator> (const basic_task &r) const
 

Static Public Member Functions

static void suspend (basic_task *t)
 Suspend the given task. More...
 
static void resume (basic_task *t)
 Resume the given task. More...
 
static void yield ()
 Yield the scheduler. More...
 
static void sleep (unsigned int secs)
 sleep this task for n seconds More...
 
static void usleep (unsigned int usec)
 sleep this task for n micro seconds More...
 
static void nsleep (const timespan_t &req, timespan_t *rem)
 pause execution for a specified time More...
 
static uint32_t get_tasks ()
 Get current number of tasks. More...
 
static bool is_current (basic_task *task)
 Is the given task the current running task ? More...
 
static basic_taskget_self ()
 Get the current task. More...
 

Static Protected Member Functions

static void runtaskstub (void *parm)
 Adapter function that allows you to write a class specific on_task() function that interfaces with FreeRTOS. More...
 

Protected Attributes

LockType_t m_runningMutex
 Lock Objekt for task safty. More...
 
LockType_t m_contextMutext
 
LockType_t m_continuemutex
 
std::string m_strName
 The name of this task. More...
 
basic_task::priority m_uiPriority
 A saved / cached copy of what the task's priority is. More...
 
unsigned short m_usStackDepth
 Stack depth of this task, in words. More...
 
int m_retval
 The return value from user task routine. More...
 
bool m_bRunning
 Flag whether or not the task was started. More...
 
int32_t m_iID
 The FreeRTOS task Number. More...
 
int32_t m_iCore
 A saved / cached copy of which core this task is running on. More...
 
native_handle_type m_pHandle
 Reference to the underlying task handle for this task. More...
 
event_group_t m_eventGroup
 

Detailed Description

Wrapper class around FreeRTOS's implementation of a task.

Note
To use this, you need to subclass it. All of your task should be derived from the basic_task class. Then implement the virtual on_task function.
#include <miniThread.hpp>
// The number of test tasks
#define NUMBER_OF_TEST_TASK 2
// The tost task class
class hello_world_task : public basic_task {
public:
hello_world_task() : basic_task("HelloWorld", 1) { }
virtual void* on_task() override {
int id = get_id(); // get the task id from this task
int core = get_on_core(); // get the core on run this task
while(true) { // infinity run
// print the hello world message to the std output
// out on core: 0 and with id: 0: "[0 @ 0] Hello World!"
printf("[%d @ %d] Hello World!!\n", id, core );
}
return NULL;
}
};
extern "C" void app_main() {
hello_world_task tasks[NUMBER_OF_TEST_TASK];
for(int i = 0; i < NUMBER_OF_TEST_TASK; i++) {
tasks[i].start( i % 2 );
}
mn_panic();
}
// Examplo output:
// [0 @ 0] Hello World!
// [1 @ 1] Hello World!
// [0 @ 0] Hello World!
// ...
basic_task() noexcept
Definition: mn_task.hpp:117
int32_t get_on_core()
Get the core number of this task run.
Definition: mn_task.cpp:228
int32_t get_id()
Get the FreeRTOS task Numberid of this task.
Definition: mn_task.cpp:220
virtual int on_task()
Implementation of your actual task code. You must override this function.
Definition: mn_task.hpp:306
The basic include file for using this library. This file is part of the Mini Thread Library (https://...
Author
Amber-Sophia Schröck

Member Typedef Documentation

◆ native_handle_type

Member Enumeration Documentation

◆ priority

Task priority.

Enumerator
Idle 

Priority for no real time operations - idle task

Low 

Priority for low operation

Normal 

Priority for tasks for normal operatins - user interfaces for example

HalfCritical 

Priority for tasks with normal deadlines and not a lot of processings

Urgent 

priority for tasks with short deadlines and a lot of processings

Critical 

Priority for critical tasks - the highest priority

◆ state

enum mn::basic_task::state
strong

Task states returned by get_state().

Enumerator
Running 

A task is querying the state of itself, so must be running.

Ready 

The task being queried is in a read or pending ready list.

Blocked 

The task being queried is in the Blocked state.

Suspended 

The task being queried is in the Suspended state, or is in the Blocked state with an infinite time out.

Deleted 

The task being queried has been deleted, but its TCB has not yet been freed.

Constructor & Destructor Documentation

◆ basic_task() [1/2]

mn::basic_task::basic_task ( )
inlinenoexcept

Basic Constructor for this task. The priority is PriorityNormal and use MN_THREAD_CONFIG_MINIMAL_STACK_SIZE for the stack size

◆ basic_task() [2/2]

mn::basic_task::basic_task ( std::string  strName,
basic_task::priority  uiPriority = basic_task::priority::Normal,
unsigned short  usStackDepth = MN_THREAD_CONFIG_MINIMAL_STACK_SIZE 
)
explicitnoexcept

Constructor for this task.

Parameters
strNameName of the Task. Only useful for debugging.
uiPriorityFreeRTOS priority of this Task.
usStackDepthNumber of "words" allocated for the Task stack. default MN_THREAD_CONFIG_MINIMAL_STACK_SIZE

◆ ~basic_task()

mn::basic_task::~basic_task ( )
virtual

Our destructor. Delete the task.

Member Function Documentation

◆ get_handle()

xTaskHandle mn::basic_task::get_handle ( )

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

Returns
FreeRTOS task handle.

◆ get_id()

int32_t mn::basic_task::get_id ( )

Get the FreeRTOS task Numberid of this task.

Returns
The FreeRTOS task Number

◆ get_name()

std::string mn::basic_task::get_name ( )

Get the debug name of this task.

Returns
The name of this task

◆ get_on_core()

int32_t mn::basic_task::get_on_core ( )

Get the core number of this task run.

Returns
The core number

◆ get_priority()

basic_task::priority mn::basic_task::get_priority ( )

Get the priority of this task.

Returns
The priority

◆ get_return_value()

int mn::basic_task::get_return_value ( )

Get the return value of this task - after run.

Returns
The return value

◆ get_self()

basic_task * mn::basic_task::get_self ( )
static

Get the current task.

Returns
The current task

◆ get_stackdepth()

unsigned short mn::basic_task::get_stackdepth ( )

Get the stack depth of this task.

Returns
The stack depth

◆ get_state()

basic_task::state mn::basic_task::get_state ( )

Get the state of the task.

Returns
The state of the task at the time the function was called.

◆ get_tasks()

uint32_t mn::basic_task::get_tasks ( )
static

Get current number of tasks.

Returns
The number of tasks that the real time kernel is currently managing. This includes all ready, blocked and suspended tasks. A task that has been deleted but not yet freed by the idle task will also be included in the count.

◆ get_time_since_start()

timespan_t mn::basic_task::get_time_since_start ( ) const

Get the time since start of this task.

Returns
The time since start of this task

◆ is_current()

static bool mn::basic_task::is_current ( basic_task task)
inlinestatic

Is the given task the current running task ?

◆ is_running()

bool mn::basic_task::is_running ( )

Is the Task running?

Returns
true If the task running, false If not

◆ join() [1/2]

int mn::basic_task::join ( timespan_t  time)

join the task, Wait in other task to end this task.

Parameters
timeThe maximum amount of time to wait.
Note
call never in the this task, then wait this task to end this task.
Returns
  • ERR_TASK_NOTRUNNING Call start first.
  • ERR_TASK_CALLFROMSELFTASK Don't do this ... see the notes
  • NO_ERROR No error

◆ join() [2/2]

int mn::basic_task::join ( unsigned int  xTickTimeout = portMAX_DELAY)

join the task, Wait in other task to end this task.

Parameters
xTickTimeoutThe maximum amount of ticks to wait.
Note
call never in the this task, then wait this task to end this task.
Returns
  • ERR_TASK_NOTRUNNING Call start first.
  • ERR_TASK_CALLFROMSELFTASK Don't do this ... see the notes
  • NO_ERROR No error

◆ joinable()

bool mn::basic_task::joinable ( ) const
noexcept

◆ kill()

int mn::basic_task::kill ( )

Destroy and delete the task and call the function 'on_kill'.

Returns
  • ERR_TASK_OK The task are destroyed
  • ERR_TASK_NOTRUNNING The task was not running

◆ nsleep()

static void mn::basic_task::nsleep ( const timespan_t req,
timespan_t rem 
)
inlinestatic

pause execution for a specified time

Note
see Linux nanosleep function

◆ on_cleanup()

virtual void mn::basic_task::on_cleanup ( )
inlinevirtual

Called on exit from your on_task() routine.

Note
It is optional whether you implement this or not. If you allow your task to exit its on_task method,

◆ on_kill()

virtual void mn::basic_task::on_kill ( )
inlinevirtual

This virtual function call on kill, use for user code It is optional whether you implement this or not.

◆ on_start()

virtual void mn::basic_task::on_start ( )
inlinevirtual

This virtual function call on creating, use for user code It is optional whether you implement this or not.

◆ on_task()

virtual int mn::basic_task::on_task ( )
inlinevirtual

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

Returns
Your return your task function, get with get_return_value()

Reimplemented in mn::queue::work_queue_task, mn::basic_thread, mn::ext::basic_message_task, and mn::ext::foreign_task.

◆ operator int()

mn::basic_task::operator int ( )
inline

Operator to get the ID assigned to the task.

Returns
The ID assigned to the task being queried.

◆ operator xTaskHandle()

mn::basic_task::operator xTaskHandle ( )
inline

Operator to get the task's backing task handle.

Returns
FreeRTOS task handle.

◆ operator!=()

bool mn::basic_task::operator!= ( const basic_task r) const
inline

◆ operator<()

bool mn::basic_task::operator< ( const basic_task r) const
inline

◆ operator==()

bool mn::basic_task::operator== ( const basic_task r) const
inline

◆ operator>()

bool mn::basic_task::operator> ( const basic_task r) const
inline

◆ resume() [1/2]

void mn::basic_task::resume ( )

Resume a specific task.

◆ resume() [2/2]

static void mn::basic_task::resume ( basic_task t)
inlinestatic

Resume the given task.

Parameters
tThe given task to resume

◆ runtaskstub()

void mn::basic_task::runtaskstub ( void *  parm)
staticprotected

Adapter function that allows you to write a class specific on_task() function that interfaces with FreeRTOS.

◆ set_priority()

void mn::basic_task::set_priority ( basic_task::priority  uiPriority)

Set the priority of this task.

Parameters
uiPriorityThe task's new priority.

◆ sleep()

static void mn::basic_task::sleep ( unsigned int  secs)
inlinestatic

sleep this task for n seconds

Parameters
secsHow long seconds to sleep the task.

◆ start()

int mn::basic_task::start ( int  uiCore = MN_THREAD_CONFIG_DEFAULT_CORE)
virtual

Create and starts the Task.

This is the API call that actually starts the Task running. It creates a backing FreeRTOS task. By separating object creation from starting the Task, it solves the pure virtual fuction call failure case. Call after creating the Task the function on_start

Parameters
uiCoreIf the value is MN_THREAD_CONFIG_CORE_IFNO, the created task is not pinned to any CPU, and the scheduler can run it on any core available. Other values indicate the index number of the CPU which the task should be pinned to. Specifying values larger than (portNUM_PROCESSORS - 1) will cause the function to fail.
Returns
  • ERR_TASK_OK The task are creating,
  • ERR_TASK_CANTINITMUTEX on error creating the using LockObjets, the task is not created.
  • ERR_TASK_ALREADYRUNNING the Task is allready running.
  • ERR_TASK_CANTSTARTTHREAD can't create the tas.
  • ERR_TASK_CANTCREATEEVENTGROUP can't create the event group, the task is not created.

Reimplemented in mn::ext::foreign_task.

◆ suspend() [1/2]

void mn::basic_task::suspend ( )

Suspend this task.

Note
While a task can suspend() itself, it cannot resume() itself, becauseit's suspended.

◆ suspend() [2/2]

static void mn::basic_task::suspend ( basic_task t)
inlinestatic

Suspend the given task.

Parameters
tThe given task to suspend
Note
While a task can suspend() itself, it cannot resume() itself, becauseit's suspended.

◆ usleep()

static void mn::basic_task::usleep ( unsigned int  usec)
inlinestatic

sleep this task for n micro seconds

Parameters
secsHow long micro seconds to sleep the task.

◆ wait() [1/2]

int mn::basic_task::wait ( timespan_t  time)

Wait for start the task.

Parameters
xTickTimeoutThe maximum amount of time to wait.
Note
call never in the this task, then wait this task to start this task.
Returns
  • ERR_TASK_NOTRUNNING Call start first.
  • ERR_TASK_CALLFROMSELFTASK Don't do this ... see the notes
  • NO_ERROR No error

◆ wait() [2/2]

int mn::basic_task::wait ( unsigned int  xTimeOut)

Wait for start the task.

Parameters
xTickTimeoutThe maximum amount of ticks to wait.
Note
call never in the this task, then wait this task to start this task.
Returns
  • ERR_TASK_NOTRUNNING Call start first.
  • ERR_TASK_CALLFROMSELFTASK Don't do this ... see the notes
  • NO_ERROR No error

◆ yield()

static void mn::basic_task::yield ( )
inlinestatic

Yield the scheduler.

Member Data Documentation

◆ m_bRunning

bool mn::basic_task::m_bRunning
protected

Flag whether or not the task was started.

◆ m_contextMutext

LockType_t mn::basic_task::m_contextMutext
protected

◆ m_continuemutex

LockType_t mn::basic_task::m_continuemutex
protected

◆ m_eventGroup

event_group_t mn::basic_task::m_eventGroup
protected

◆ m_iCore

int32_t mn::basic_task::m_iCore
protected

A saved / cached copy of which core this task is running on.

◆ m_iID

int32_t mn::basic_task::m_iID
protected

The FreeRTOS task Number.

◆ m_pHandle

native_handle_type mn::basic_task::m_pHandle
protected

Reference to the underlying task handle for this task.

Note
Can be obtained from get_handle().

◆ m_retval

int mn::basic_task::m_retval
protected

The return value from user task routine.

◆ m_runningMutex

LockType_t mn::basic_task::m_runningMutex
mutableprotected

Lock Objekt for task safty.

◆ m_strName

std::string mn::basic_task::m_strName
protected

The name of this task.

◆ m_uiPriority

basic_task::priority mn::basic_task::m_uiPriority
protected

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

◆ m_usStackDepth

unsigned short mn::basic_task::m_usStackDepth
protected

Stack depth of this task, in words.


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