27#include <debug/debug.h>
104 explicit TaskScheduler(miosix::Priority priority = miosix::PRIORITY_MAX -
127 auto period = std::chrono::milliseconds{periodMs};
128 auto startTime = std::chrono::time_point<std::chrono::steady_clock>{
129 std::chrono::milliseconds{startTick}};
131 return addTask(function, period, policy, startTime);
152 std::chrono::time_point<std::chrono::steady_clock>
153 startTime = std::chrono::steady_clock::now())
155 auto period = std::chrono::nanoseconds{
156 static_cast<int64_t
>(
sToNs(1) / frequency.
value())};
158 return addTask(function, period, policy, startTime);
179 std::chrono::time_point<std::chrono::steady_clock>
180 startTime = std::chrono::steady_clock::now());
192 bool start()
override;
194 void stop()
override;
208 Stats activationStats;
211 uint32_t missedEvents;
212 uint32_t failedEvents;
232 Task(
const Task& other) =
delete;
233 Task& operator=(
const Task& other) =
delete;
237 Task(Task&& other) =
default;
238 Task& operator=(Task&& other) =
default;
243 bool empty()
const {
return !function; }
251 Event(
size_t taskId, int64_t nextTime)
252 : taskId(taskId), nextTime(nextTime)
262 bool operator>(
const Event& other)
const
264 return this->nextTime > other.nextTime;
271 std::priority_queue<Event, std::vector<Event>, std::greater<Event>>;
277 static EventQueue makeAgenda();
286 void populateAgenda();
299 void updateStats(
const Event& event, int64_t startTime, int64_t endTime);
312 void enqueue(
Event event, int64_t startTime);
314 static TaskStatsResult fromTaskIdPairToStatsResult(
const Task& task,
317 return TaskStatsResult{id,
318 std::chrono::nanoseconds{task.period},
319 task.activationStats.getStats(),
320 task.periodStats.getStats(),
321 task.workloadStats.getStats(),
326 miosix::FastMutex mutex;
327 std::vector<Task> tasks;
329 miosix::ConditionVariable condvar;
static PrintLogger getLogger(const string &name)
Computes on-line statistics of a dataset.
The Task Scheduler allow to manage simple tasks with a single thread. All the task added must not tak...
std::vector< TaskStatsResult > getTaskStats()
void enableTask(size_t id)
Enables the task with the given id.
bool start() override
Start the thread associated with this active object.
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.
void disableTask(size_t id)
Disables the task with the given id, preventing it from executing.
Policy
Task behavior policy. Determines the behavior of the scheduler for a specific task.
@ ONE_SHOT
Run the task one single timer.
@ RECOVER
Prioritize the number of executions over the period.
void stop() override
Signals the runner thread to terminate and joins the thread.
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.
std::function< void()> function_t
TaskScheduler(miosix::Priority priority=miosix::PRIORITY_MAX - 1)
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.
static constexpr size_t MAX_TASKS
The maximum number of tasks the scheduler can handle.
constexpr float value() const
Return the value of the unit in the target ratio.
long long getOldTick()
Get the current time in milliseconds.
This file includes all the types the logdecoder script will decode.
constexpr long long sToNs(long long s)
Convert seconds to nanoseconds.