diff --git a/src/shared/scheduler/TaskScheduler.cpp b/src/shared/scheduler/TaskScheduler.cpp index fd522b481a65c66a689f8f8afdc5391a86397753..844fea5e9377f5a358e34f64b33bc403122d3eb3 100644 --- a/src/shared/scheduler/TaskScheduler.cpp +++ b/src/shared/scheduler/TaskScheduler.cpp @@ -41,8 +41,7 @@ TaskScheduler::TaskScheduler(miosix::Priority priority) // Initialize the vector elements for (size_t i = 1; i < TASKS_SIZE; i++) { - function_t function; - (*tasks)[i] = makeTask(function, 0, i, false, Policy::SKIP); + (*tasks)[i] = Task(); } } @@ -73,8 +72,8 @@ size_t TaskScheduler::addTask(function_t function, uint32_t period, return 0; } - // Register the task into the map - (*tasks)[id] = makeTask(function, period, id, true, policy); + // Create a new task with the given parameters + (*tasks)[id] = Task(function, period, policy); if (policy == Policy::ONE_SHOT) { @@ -137,11 +136,12 @@ vector<TaskStatsResult> TaskScheduler::getTaskStats() vector<TaskStatsResult> result; - for (auto const& task : (*tasks)) + for (size_t id = 1; id < TASKS_SIZE; id++) { + const Task& task = (*tasks)[id]; if (task.valid) { - result.push_back(fromTaskIdPairToStatsResult(task)); + result.push_back(fromTaskIdPairToStatsResult(task, id)); } } @@ -291,11 +291,18 @@ void TaskScheduler::enqueue(Event event, int64_t startTick) condvar.broadcast(); } -TaskScheduler::Task TaskScheduler::makeTask(function_t function, - uint32_t period, size_t id, - bool validity, Policy policy) +TaskScheduler::Task::Task() + : function(nullptr), period(0), valid(false), policy(Policy::SKIP), + lastCall(-1), activationStats(), periodStats(), workloadStats(), + missedEvents(0), failedEvents(0) +{ +} + +TaskScheduler::Task::Task(function_t function, uint32_t period, Policy policy) + : function(function), period(period), valid(true), policy(policy), + lastCall(-1), activationStats(), periodStats(), workloadStats(), + missedEvents(0), failedEvents(0) { - return Task{function, period, id, validity, policy, -1, {}, {}, {}, 0, 0}; } } // namespace Boardcore diff --git a/src/shared/scheduler/TaskScheduler.h b/src/shared/scheduler/TaskScheduler.h index 97d4ef87e28be1bd0ff555c45e72a24eada002bd..6d76a8a86a96f0730fdf0dd0b798dfb55bfa1b0b 100644 --- a/src/shared/scheduler/TaskScheduler.h +++ b/src/shared/scheduler/TaskScheduler.h @@ -144,7 +144,6 @@ private: { function_t function; uint32_t period; // [ms] - size_t id; bool valid; Policy policy; int64_t lastCall; ///< Last activation tick for statistics computation. @@ -153,6 +152,20 @@ private: Stats workloadStats; ///< Stats about time the task takes to compute. uint32_t missedEvents; ///< Number of events that could not be run. uint32_t failedEvents; ///< Number of events ended with exceptions. + + /** + * @brief Default constructor that creates an empty, invalid task + */ + Task(); + + /** + * @brief Creates a task with the given parameters + * + * @param function The std::function to be called + * @param period The Period in [ms] + * @param policy The task policy in case of a miss + */ + explicit Task(function_t function, uint32_t period, Policy policy); }; struct Event @@ -208,23 +221,10 @@ private: */ void enqueue(Event event, int64_t startTick); - /** - * @brief Creates a task with the passed values - * - * @param function The std::function to be called - * @param period The Period in [ms] - * @param id The task intrinsic id - * @param validity The validity of the task (false if not initialized or if - * removed) - * @param policy The task policy in case of a miss - */ - Task makeTask(function_t function, uint32_t period, size_t id, - bool validity, Policy policy); - - static TaskStatsResult fromTaskIdPairToStatsResult(Task task) + static TaskStatsResult fromTaskIdPairToStatsResult(Task task, size_t id) { - return TaskStatsResult{task.id, + return TaskStatsResult{id, task.period, task.activationStats.getStats(), task.periodStats.getStats(),