TaskManagerIO
Loading...
Searching...
No Matches
TaskManager Class Reference

#include <TaskManagerIO.h>

Inheritance diagram for TaskManager:
SimulatedTaskManager

Public Member Functions

 TaskManager ()
taskid_t execute (TimerFn workToDo)
taskid_t execute (Executable *execToDo, bool deleteWhenDone=false)
taskid_t schedule (const TimePeriod &when, TimerFn timerFunction)
taskid_t schedule (const TimePeriod &when, Executable *execRef, bool deleteWhenDone=false)
taskid_t scheduleOnce (uint32_t when, TimerFn timerFunction, TimerUnit timeUnit=TIME_MILLIS)
taskid_t scheduleOnce (uint32_t when, Executable *execRef, TimerUnit timeUnit=TIME_MILLIS, bool deleteWhenDone=false)
taskid_t scheduleFixedRate (uint32_t when, TimerFn timerFunction, TimerUnit timeUnit=TIME_MILLIS)
taskid_t scheduleFixedRate (uint32_t when, Executable *execRef, TimerUnit timeUnit=TIME_MILLIS, bool deleteWhenDone=false)
taskid_t registerEvent (BaseEvent *eventToAdd, bool deleteWhenDone=false)
ISR_ATTR void triggerEvents ()
void addInterrupt (InterruptAbstraction *interruptAbstraction, pintype_t pin, uint8_t mode)
void setInterruptCallback (InterruptFn handler)
void cancelTask (taskid_t task)
void setTaskEnabled (taskid_t task, bool ena)
virtual void yieldForMicros (uint32_t micros)
void runLoop ()
void reset ()
char * checkAvailableSlots (char *slotData, size_t slotDataSize) const
TimerTaskgetFirstTask ()
TimerTaskgetTask (taskid_t task)
uint32_t microsToNextTask ()
TimerTaskgetRunningTask ()

Static Public Member Functions

static void markInterrupted (pintype_t interruptNo)

Protected Attributes

TaskBlock *volatile taskBlocks [DEFAULT_TASK_BLOCKS]
volatile taskid_t numberOfBlocks
tm_internal::TimerTaskAtomicPtr first
volatile pintype_t lastInterruptTrigger
volatile bool interrupted
volatile InterruptFn interruptCallback
tm_internal::TmAtomicBool memLockerFlag
tm_internal::TimerTaskAtomicPtr runningTask

Friends

class TaskExecutionRecorder

Detailed Description

TaskManager is a lightweight cooperative co-routine implementation for Arduino, it works by scheduling tasks to be done either immediately, or at a future point in time. It is quite efficient at scheduling tasks as internally tasks are arranged in time order in a linked list. Tasks can be provided as either a function to be called, or a class that implements the Executable interface. Tasks can be scheduled to run either ASAP, once, or repeated.

Events can be added based on extensions of the BaseEvent class, these can be triggered outside of task manager and they can then "wake up" task manager, events can also be polled. In addition interrupts can be marshalled through task manager, any interrupt managed by task manager will be marshalled into a task. IE outside of an ISR.

There is a globally defined variable called taskManager and you should attach this to your main loop. You can create other task managers on different threads if required. For most use cases, the class is thread safe.

Constructor & Destructor Documentation

◆ TaskManager()

TaskManager::TaskManager ( )

On all platforms there is a default instance of TaskManager called taskManager. You can create other instances that can run on other threads, for example to process long running tasks that should be processed separately.

Member Function Documentation

◆ execute() [1/2]

taskid_t TaskManager::execute ( TimerFn workToDo)
inline

Executes a task manager task as soon as possible. Useful to add work into task manager from another thread of execution. Shorthand for scheduleOnce(2, task);

Why not scheduleOnce with 0 you may ask, taskManager is cooperative which also means it is an unfair scheduler. Given this, it would always add at the front of the queue if the time was 0, but by making the time 2, we ensure it is queued behind other things needing immediate execution.

Parameters
workToDothe work to be done
Returns
the task ID that can be queried and cancelled.

◆ execute() [2/2]

taskid_t TaskManager::execute ( Executable * execToDo,
bool deleteWhenDone = false )
inline

Executes a task manager task as soon as possible. Useful to add work into task manager from another thread of execution. Shorthand for scheduleOnce(2, task);

Why not scheduleOnce with 0 you may ask, taskManager is cooperative which also means it is an unfair scheduler. Given this, it would always add at the front of the queue if the time was 0, but by making the time 2, we ensure it is queued behind other things needing immediate execution.

Parameters
execToDothe work to be done
deleteWhenDonedefault to false, task manager will reclaim the memory when done with this executable.
Returns
the task ID that can be queried and cancelled.

◆ schedule() [1/2]

taskid_t TaskManager::schedule ( const TimePeriod & when,
TimerFn timerFunction )

Schedule using a time period, normally using the helper functions to quickly create the period, underneath this calls one of the existing schedule... methods. This schedules a no parameter function to be called. On larger boards with lambda support enabled, it actually schedules a std::function.

Parameters
whenthe time period providing the schedule details
timerFunctionthe function to call
Returns
the task ID that can be queried and cancelled.

◆ schedule() [2/2]

taskid_t TaskManager::schedule ( const TimePeriod & when,
Executable * execRef,
bool deleteWhenDone = false )

Schedule using a time period, normally using the helper functions to quickly create the period, underneath this calls one of the existing schedule... methods. This schedules an executable to be called back.

Parameters
whenthe time period providing the schedule details
execRefthe function to call
deleteWhenDoneif true, once the task ends (cancelled or finished in once mode) it is deleted.
Returns
the task ID that can be queried and cancelled.

◆ scheduleOnce() [1/2]

taskid_t TaskManager::scheduleOnce ( uint32_t when,
TimerFn timerFunction,
TimerUnit timeUnit = TIME_MILLIS )

Schedules a task for one shot execution in the timeframe provided.

Parameters
millisthe time frame in which to schedule the task
timerFunctionthe function to run at that time
timeUnitdefaults to TIME_MILLIS but can be any of the possible values.

◆ scheduleOnce() [2/2]

taskid_t TaskManager::scheduleOnce ( uint32_t when,
Executable * execRef,
TimerUnit timeUnit = TIME_MILLIS,
bool deleteWhenDone = false )

Schedules a task for one shot execution in the timeframe provided calling back the exec function on the provided class extending Executable.

Parameters
millisthe time frame in which to schedule the task
execRefa reference to a class extending Executable
timeUnitdefaults to TIME_MILLIS but can be any of the possible values.
deleteWhenDonedefault to false, task manager will reclaim the memory when done with this executable.

◆ scheduleFixedRate() [1/2]

taskid_t TaskManager::scheduleFixedRate ( uint32_t when,
TimerFn timerFunction,
TimerUnit timeUnit = TIME_MILLIS )

Schedules a task for repeated execution at the frequency provided.

Parameters
millisthe frequency at which to execute
timerFunctionthe function to run at that time
timeUnitdefaults to TIME_MILLIS but can be any of the possible values.

◆ scheduleFixedRate() [2/2]

taskid_t TaskManager::scheduleFixedRate ( uint32_t when,
Executable * execRef,
TimerUnit timeUnit = TIME_MILLIS,
bool deleteWhenDone = false )

Schedules a task for repeated execution at the frequency provided calling back the exec method on the provided class extending Executable.

Parameters
millisthe frequency at which to execute
execRefa reference to a class extending Executable
timeUnitdefaults to TIME_MILLIS but can be any of the possible values.
deleteWhenDonetrue if taskManager should call delete on the object when done, otherwise false.

◆ registerEvent()

taskid_t TaskManager::registerEvent ( BaseEvent * eventToAdd,
bool deleteWhenDone = false )

Adds an event to task manager that can be triggered either once or can be repeated. See the BaseEvent interface for more details of the interaction with taskManager.

Parameters
eventToAddthe event that is added to task manager
deleteWhenDonetrue if taskManager should call delete on the object when done, otherwise false.

◆ triggerEvents()

ISR_ATTR void TaskManager::triggerEvents ( )
inline

Generally used by the BaseEvent class in the markTriggeredAndNotify to indicate that at least one event has now triggered and needs to be evaluated.

◆ addInterrupt()

void TaskManager::addInterrupt ( InterruptAbstraction * interruptAbstraction,
pintype_t pin,
uint8_t mode )

Adds an interrupt that will be handled by task manager, such that it's marshalled into a task. This registers an interrupt with any IoAbstractionRef.

Parameters
refthe Interrupt abstraction (or IoAbstractionRef) that we want to register the interrupt for
pinthe pin upon which to register (on the IoDevice above)
modethe mode in which to register, eg. CHANGE, RISING, FALLING

◆ setInterruptCallback()

void TaskManager::setInterruptCallback ( InterruptFn handler)

Sets the interrupt callback to be used when an interrupt is signalled. Note that you will be called back by task manager, and you are safe to use any variables as normal. Task manager marshalls the interrupt for you.

Parameters
handlerthe interrupt handler

◆ cancelTask()

void TaskManager::cancelTask ( taskid_t task)

Stop a task from executing or cancel it from executing again if it is a repeating task

Parameters
taskthe task ID returned from the schedule call

◆ setTaskEnabled()

void TaskManager::setTaskEnabled ( taskid_t task,
bool ena )

Sets a tasks enable status. An enabled task is scheduled whereas a disabled task is not scheduled. Note that after disabling a task it may run one more time before switching state. Upon re-enablement then task manager will put the item back into the run queue if needed.

Parameters
taskthe task to change status
enathe enablement status

◆ yieldForMicros()

void TaskManager::yieldForMicros ( uint32_t micros)
virtual

Use instead of delays or wait loops inside code that needs to perform timing functions. It will not call back until at least micros time has passed.

Parameters
microsthe number of microseconds to wait.

Reimplemented in SimulatedTaskManager.

◆ runLoop()

void TaskManager::runLoop ( )

This should be called in the loop() method of your sketch, ensure that your loop method does not do anything that will unduly delay calling this method.

◆ markInterrupted()

ISR_ATTR void TaskManager::markInterrupted ( pintype_t interruptNo)
static

Used internally by the interrupt handlers to tell task manager an interrupt is waiting. Not for external use.

◆ reset()

void TaskManager::reset ( )
inline

Reset the task manager such that all current tasks are cleared, back to power on state.

◆ checkAvailableSlots()

char * TaskManager::checkAvailableSlots ( char * slotData,
size_t slotDataSize ) const

This method fills slotData with the current running conditions of each available task slot. Useful for debugging purposes, where each task the . Key:

  • 'R' repeating task ('r' when currently running)
  • 'U' one shot task ('u' when currently running)
  • 'F' free slot (should never be 'f')
    Parameters
    slotDatathe char array to fill in with task information. Must be as long as number of tasks + 1.
    slotDataSizethe size of the array passed in (normally using sizeof).

◆ getFirstTask()

TimerTask * TaskManager::getFirstTask ( )
inline

Gets the first task in the run queue. Not often useful outside of testing.

◆ getTask()

TimerTask * TaskManager::getTask ( taskid_t task)

Gets the underlying TimerTask variable associated with this task ID.

Parameters
taskthe task's ID
Returns
the task or nullptr.

◆ microsToNextTask()

uint32_t TaskManager::microsToNextTask ( )
inline

Gets the number of microseconds as an unsigned long to the next task execution. To convert to milliseconds: divide by 1000, to seconds divide by 1,000,000.

Returns
the microseconds from now to next execution

◆ getRunningTask()

TimerTask * TaskManager::getRunningTask ( )
inline

Gets the currently running task, this is only useful for places where re-entrant checking is needed to ensure the same task is taking the lock again for example. Never change the task state in this call, and also never store this pointer.

Returns
a temporary pointer to the running task that lasts as long as it is running.

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