Skyward boardcore
|
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>
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< TaskStatsResult > | getTaskStats () |
![]() | |
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 | |
![]() | |
bool | shouldStop () |
Tells whether or not the ActiveObject should stop its execution. | |
![]() | |
miosix::Thread * | thread = nullptr |
Gives access to the thread object. | |
std::atomic< bool > | stopFlag {false} |
std::atomic< bool > | running {false} |
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.
using Boardcore::TaskScheduler::function_t = std::function<void()> |
Definition at line 66 of file TaskScheduler.h.
|
strong |
Task behavior policy. Determines the behavior of the scheduler for a specific task.
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.
|
explicit |
Definition at line 45 of file TaskScheduler.cpp.
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.
function | Function to be called periodically. |
period | Inter call period [ns]. |
policy | Task policy, default is RECOVER. |
startTime | Absolute system time of the first activation, useful for synchronizing tasks [ns] |
|
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.
function | Function to be called periodically. |
periodMs | Inter call period [ms]. |
policy | Task policy, default is RECOVER. |
startTick | Absolute system tick of the first activation, useful for synchronizing tasks [ms] |
Definition at line 123 of file TaskScheduler.h.
|
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.
function | Function to be called periodically. |
frequency | Task frequency [Hz]. |
policy | Task policy, default is RECOVER. |
startTime | Absolute system time of the first activation, useful for synchronizing tasks [ns] |
Definition at line 150 of file TaskScheduler.h.
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.
void Boardcore::TaskScheduler::enableTask | ( | size_t | id | ) |
Enables the task with the given id.
Definition at line 87 of file TaskScheduler.cpp.
vector< TaskStatsResult > Boardcore::TaskScheduler::getTaskStats | ( | ) |
Definition at line 154 of file TaskScheduler.cpp.
|
overridevirtual |
Start the thread associated with this active object.
Call stop() to terminate execution of the thread.
Reimplemented from Boardcore::ActiveObject.
Definition at line 131 of file TaskScheduler.cpp.
|
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.
|
staticconstexpr |
The maximum number of tasks the scheduler can handle.
Definition at line 71 of file TaskScheduler.h.