Skyward boardcore
Loading...
Searching...
No Matches
Boardcore::TaskScheduler Class Reference

The Task Scheduler allow to manage simple tasks with a single thread. All the task added must not take more than 1ms to execute and should take less time as possible to ensure other tasks are executed as they are supposed to. More...

#include <TaskScheduler.h>

Inheritance diagram for Boardcore::TaskScheduler:
Collaboration diagram for Boardcore::TaskScheduler:

Public Types

enum class  Policy : uint8_t { ONE_SHOT , SKIP , RECOVER }
 Task behavior policy. Determines the behavior of the scheduler for a specific task. More...
 
using function_t = std::function<void()>
 

Public Member Functions

 TaskScheduler (miosix::Priority priority=miosix::PRIORITY_MAX - 1)
 
size_t addTask (function_t function, uint32_t periodMs, Policy policy=Policy::RECOVER, int64_t startTick=Kernel::getOldTick())
 Add a millisecond-period task function to the scheduler with an auto generated ID.
 
size_t addTask (function_t function, Units::Frequency::Hertz frequency, Policy policy=Policy::RECOVER, std::chrono::time_point< std::chrono::steady_clock > startTime=std::chrono::steady_clock::now())
 Add a task function with the given frequency to the scheduler with an auto generated ID.
 
size_t addTask (function_t function, std::chrono::nanoseconds period, Policy policy=Policy::RECOVER, std::chrono::time_point< std::chrono::steady_clock > startTime=std::chrono::steady_clock::now())
 Add a task function with the given period to the scheduler with an auto generated ID.
 
void enableTask (size_t id)
 Enables the task with the given id.
 
void disableTask (size_t id)
 Disables the task with the given id, preventing it from executing.
 
bool start () override
 Start the thread associated with this active object.
 
void stop () override
 Signals the runner thread to terminate and joins the thread.
 
std::vector< TaskStatsResultgetTaskStats ()
 
- Public Member Functions inherited from Boardcore::ActiveObject
 ActiveObject (unsigned int stacksize=miosix::STACK_DEFAULT_FOR_PTHREAD, miosix::Priority priority=miosix::MAIN_PRIORITY)
 
virtual ~ActiveObject ()
 
bool isRunning ()
 

Static Public Attributes

static constexpr size_t MAX_TASKS = 256
 The maximum number of tasks the scheduler can handle.
 

Additional Inherited Members

- Protected Member Functions inherited from Boardcore::ActiveObject
bool shouldStop ()
 Tells whether or not the ActiveObject should stop its execution.
 
- Protected Attributes inherited from Boardcore::ActiveObject
miosix::Thread * thread = nullptr
 Gives access to the thread object.
 
std::atomic< bool > stopFlag {false}
 
std::atomic< bool > running {false}
 

Detailed Description

The Task Scheduler allow to manage simple tasks with a single thread. All the task added must not take more than 1ms to execute and should take less time as possible to ensure other tasks are executed as they are supposed to.

HOW TO USE THE TASK SCHEDULER

TaskScheduler.add(nonblocking_std::function_without_sleeps, millisec, id); and.. it works like magic. :)

Example: void magic_std::function() { // do something NONBLOCKING and WITHOUT SLEEPS } TaskScheduler.add(magic_std::function, 150);

Definition at line 63 of file TaskScheduler.h.

Member Typedef Documentation

◆ function_t

using Boardcore::TaskScheduler::function_t = std::function<void()>

Definition at line 66 of file TaskScheduler.h.

Member Enumeration Documentation

◆ Policy

enum class Boardcore::TaskScheduler::Policy : uint8_t
strong

Task behavior policy. Determines the behavior of the scheduler for a specific task.

  • ONE_SHOT: Runs the task once and subsequently removes it from the task list. This is useful for one-off tasks.
  • SKIP: Skips missed executions. This is useful for tasks that need to execute periodically but can skip some executions. If the task misses one or more executions, the scheduler will skip the missed executions, run the task once and re-schedule the task for future execution. The scheduler will try to align the task execution time with the original start time, but actual execution time is not guaranteed to be aligned with the period.
  • RECOVER: Recovers missed executions. This is useful for tasks that need to reach an overall number of iterations, but don't care too much about timing. Missed executions are recovered immediately, so this may cause one or more tasks to clump at the beginning of the task queue until all missed executions are recovered, causing the period to not be respected (see issue #91).
Enumerator
ONE_SHOT 

Run the task one single timer.

SKIP 
RECOVER 

Prioritize the number of executions over the period.

Definition at line 96 of file TaskScheduler.h.

Constructor & Destructor Documentation

◆ TaskScheduler()

Boardcore::TaskScheduler::TaskScheduler ( miosix::Priority priority = miosix::PRIORITY_MAX - 1)
explicit

Definition at line 45 of file TaskScheduler.cpp.

Member Function Documentation

◆ addTask() [1/3]

size_t Boardcore::TaskScheduler::addTask ( function_t function,
std::chrono::nanoseconds period,
Policy policy = Policy::RECOVER,
std::chrono::time_point< std::chrono::steady_clock > startTime = std::chrono::steady_clock::now() )

Add a task function with the given period to the scheduler with an auto generated ID.

Note that each task has it's own unique ID, even one shot tasks!

For one shot tasks, the period is used as a delay. If 0 the task will be executed immediately, otherwise after the given period.

Parameters
functionFunction to be called periodically.
periodInter call period [ns].
policyTask policy, default is RECOVER.
startTimeAbsolute system time of the first activation, useful for synchronizing tasks [ns]
Returns
The ID of the task if it was added successfully, 0 otherwise.

◆ addTask() [2/3]

size_t Boardcore::TaskScheduler::addTask ( function_t function,
uint32_t periodMs,
Policy policy = Policy::RECOVER,
int64_t startTick = Kernel::getOldTick() )
inline

Add a millisecond-period task function to the scheduler with an auto generated ID.

Note that each task has it's own unique ID, even one shot tasks!

For one shot tasks, the period is used as a delay. If 0 the task will be executed immediately, otherwise after the given period.

Parameters
functionFunction to be called periodically.
periodMsInter call period [ms].
policyTask policy, default is RECOVER.
startTickAbsolute system tick of the first activation, useful for synchronizing tasks [ms]
Returns
The ID of the task if it was added successfully, 0 otherwise.

Definition at line 123 of file TaskScheduler.h.

◆ addTask() [3/3]

size_t Boardcore::TaskScheduler::addTask ( function_t function,
Units::Frequency::Hertz frequency,
Policy policy = Policy::RECOVER,
std::chrono::time_point< std::chrono::steady_clock > startTime = std::chrono::steady_clock::now() )
inline

Add a task function with the given frequency to the scheduler with an auto generated ID.

Note that each task has it's own unique ID, even one shot tasks!

For one shot tasks, the period is used as a delay. If 0 the task will be executed immediately, otherwise after the given period.

Parameters
functionFunction to be called periodically.
frequencyTask frequency [Hz].
policyTask policy, default is RECOVER.
startTimeAbsolute system time of the first activation, useful for synchronizing tasks [ns]
Returns
The ID of the task if it was added successfully, 0 otherwise.

Definition at line 150 of file TaskScheduler.h.

◆ disableTask()

void Boardcore::TaskScheduler::disableTask ( size_t id)

Disables the task with the given id, preventing it from executing.

Definition at line 114 of file TaskScheduler.cpp.

◆ enableTask()

void Boardcore::TaskScheduler::enableTask ( size_t id)

Enables the task with the given id.

Definition at line 87 of file TaskScheduler.cpp.

◆ getTaskStats()

vector< TaskStatsResult > Boardcore::TaskScheduler::getTaskStats ( )

Definition at line 154 of file TaskScheduler.cpp.

◆ start()

bool Boardcore::TaskScheduler::start ( )
overridevirtual

Start the thread associated with this active object.

Warning
The method is not thread safe.

Call stop() to terminate execution of the thread.

Returns
true if the thread has been started.

Reimplemented from Boardcore::ActiveObject.

Definition at line 131 of file TaskScheduler.cpp.

◆ stop()

void Boardcore::TaskScheduler::stop ( )
overridevirtual

Signals the runner thread to terminate and joins the thread.

This is a blocking call that will not return until the thread terminates! Your run() implementation must check shouldStop() and terminate ASAP if it returns true.

Reimplemented from Boardcore::ActiveObject.

Definition at line 146 of file TaskScheduler.cpp.

Member Data Documentation

◆ MAX_TASKS

constexpr size_t Boardcore::TaskScheduler::MAX_TASKS = 256
staticconstexpr

The maximum number of tasks the scheduler can handle.

Definition at line 71 of file TaskScheduler.h.


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