diff --git a/src/main/fc/tasks.c b/src/main/fc/tasks.c index a8a41c945e..3c1316a8ab 100644 --- a/src/main/fc/tasks.c +++ b/src/main/fc/tasks.c @@ -312,7 +312,7 @@ static void taskCameraControl(uint32_t currentTime) task_t tasks[TASK_COUNT]; // Task ID data in .data (initialised data) -task_id_t task_ids[TASK_COUNT] = { +task_attr_t task_attrs[TASK_COUNT] = { [TASK_SYSTEM] = DEFINE_TASK("SYSTEM", "LOAD", NULL, taskSystemLoad, TASK_PERIOD_HZ(10), TASK_PRIORITY_MEDIUM_HIGH), [TASK_MAIN] = DEFINE_TASK("SYSTEM", "UPDATE", NULL, taskMain, TASK_PERIOD_HZ(1000), TASK_PRIORITY_MEDIUM_HIGH), [TASK_SERIAL] = DEFINE_TASK("SERIAL", NULL, NULL, taskHandleSerial, TASK_PERIOD_HZ(100), TASK_PRIORITY_LOW), // 100 Hz should be enough to flush up to 115 bytes @ 115200 baud @@ -423,7 +423,7 @@ task_t *getTask(unsigned taskId) void tasksInit(void) { for (int i = 0; i < TASK_COUNT; i++) { - tasks[i].id = &task_ids[i]; + tasks[i].attr = &task_attrs[i]; } schedulerInit(); diff --git a/src/main/scheduler/scheduler.c b/src/main/scheduler/scheduler.c index 8555c494ab..d1e552a943 100644 --- a/src/main/scheduler/scheduler.c +++ b/src/main/scheduler/scheduler.c @@ -126,7 +126,7 @@ bool queueAdd(task_t *task) return false; } for (int ii = 0; ii <= taskQueueSize; ++ii) { - if (taskQueueArray[ii] == NULL || taskQueueArray[ii]->id->staticPriority < task->id->staticPriority) { + if (taskQueueArray[ii] == NULL || taskQueueArray[ii]->attr->staticPriority < task->attr->staticPriority) { memmove(&taskQueueArray[ii+1], &taskQueueArray[ii], sizeof(task) * (taskQueueSize - ii)); taskQueueArray[ii] = task; ++taskQueueSize; @@ -202,10 +202,10 @@ void getCheckFuncInfo(cfCheckFuncInfo_t *checkFuncInfo) void getTaskInfo(taskId_e taskId, taskInfo_t * taskInfo) { taskInfo->isEnabled = queueContains(getTask(taskId)); - taskInfo->desiredPeriodUs = getTask(taskId)->id->desiredPeriodUs; - taskInfo->staticPriority = getTask(taskId)->id->staticPriority; - taskInfo->taskName = getTask(taskId)->id->taskName; - taskInfo->subTaskName = getTask(taskId)->id->subTaskName; + taskInfo->desiredPeriodUs = getTask(taskId)->attr->desiredPeriodUs; + taskInfo->staticPriority = getTask(taskId)->attr->staticPriority; + taskInfo->taskName = getTask(taskId)->attr->taskName; + taskInfo->subTaskName = getTask(taskId)->attr->subTaskName; taskInfo->maxExecutionTimeUs = getTask(taskId)->maxExecutionTimeUs; taskInfo->totalExecutionTimeUs = getTask(taskId)->totalExecutionTimeUs; taskInfo->averageExecutionTimeUs = getTask(taskId)->anticipatedExecutionTime >> TASK_EXEC_TIME_SHIFT; @@ -230,11 +230,11 @@ void rescheduleTask(taskId_e taskId, timeDelta_t newPeriodUs) } else { return; } - task->id->desiredPeriodUs = MAX(SCHEDULER_DELAY_LIMIT, newPeriodUs); // Limit delay to 100us (10 kHz) to prevent scheduler clogging + task->attr->desiredPeriodUs = MAX(SCHEDULER_DELAY_LIMIT, newPeriodUs); // Limit delay to 100us (10 kHz) to prevent scheduler clogging // Catch the case where the gyro loop is adjusted if (taskId == TASK_GYRO) { - desiredPeriodCycles = (int32_t)clockMicrosToCycles((uint32_t)getTask(TASK_GYRO)->id->desiredPeriodUs); + desiredPeriodCycles = (int32_t)clockMicrosToCycles((uint32_t)getTask(TASK_GYRO)->attr->desiredPeriodUs); } } @@ -242,7 +242,7 @@ void setTaskEnabled(taskId_e taskId, bool enabled) { if (taskId == TASK_SELF || taskId < TASK_COUNT) { task_t *task = taskId == TASK_SELF ? currentTask : getTask(taskId); - if (enabled && task->id->taskFunc) { + if (enabled && task->attr->taskFunc) { queueAdd(task); } else { queueRemove(task); @@ -336,7 +336,7 @@ void schedulerInit(void) taskGuardDeltaDownCycles = clockMicrosToCycles(1) / TASK_GUARD_MARGIN_DOWN_STEP; taskGuardDeltaUpCycles = clockMicrosToCycles(1) / TASK_GUARD_MARGIN_UP_STEP; - desiredPeriodCycles = (int32_t)clockMicrosToCycles((uint32_t)getTask(TASK_GYRO)->id->desiredPeriodUs); + desiredPeriodCycles = (int32_t)clockMicrosToCycles((uint32_t)getTask(TASK_GYRO)->attr->desiredPeriodUs); lastTargetCycles = getCycleCounter(); @@ -367,12 +367,12 @@ FAST_CODE timeUs_t schedulerExecuteTask(task_t *selectedTask, timeUs_t currentTi taskNextStateTime = -1; float period = currentTimeUs - selectedTask->lastExecutedAtUs; selectedTask->lastExecutedAtUs = currentTimeUs; - selectedTask->lastDesiredAt += selectedTask->id->desiredPeriodUs; + selectedTask->lastDesiredAt += selectedTask->attr->desiredPeriodUs; selectedTask->dynamicPriority = 0; // Execute task const timeUs_t currentTimeBeforeTaskCallUs = micros(); - selectedTask->id->taskFunc(currentTimeBeforeTaskCallUs); + selectedTask->attr->taskFunc(currentTimeBeforeTaskCallUs); taskExecutionTimeUs = micros() - currentTimeBeforeTaskCallUs; taskTotalExecutionTime += taskExecutionTimeUs; if (!ignoreCurrentTaskExecRate) { @@ -559,14 +559,14 @@ FAST_CODE void scheduler(void) // Update task dynamic priorities for (task_t *task = queueFirst(); task != NULL; task = queueNext()) { - if (task->id->staticPriority != TASK_PRIORITY_REALTIME) { + if (task->attr->staticPriority != TASK_PRIORITY_REALTIME) { // Task has checkFunc - event driven - if (task->id->checkFunc) { + if (task->attr->checkFunc) { // Increase priority for event driven tasks if (task->dynamicPriority > 0) { - task->taskAgeCycles = 1 + (cmpTimeUs(currentTimeUs, task->lastSignaledAtUs) / task->id->desiredPeriodUs); - task->dynamicPriority = 1 + task->id->staticPriority * task->taskAgeCycles; - } else if (task->id->checkFunc(currentTimeUs, cmpTimeUs(currentTimeUs, task->lastExecutedAtUs))) { + task->taskAgeCycles = 1 + (cmpTimeUs(currentTimeUs, task->lastSignaledAtUs) / task->attr->desiredPeriodUs); + task->dynamicPriority = 1 + task->attr->staticPriority * task->taskAgeCycles; + } else if (task->attr->checkFunc(currentTimeUs, cmpTimeUs(currentTimeUs, task->lastExecutedAtUs))) { const uint32_t checkFuncExecutionTimeUs = cmpTimeUs(micros(), currentTimeUs); #if !defined(UNIT_TEST) DEBUG_SET(DEBUG_SCHEDULER, 3, checkFuncExecutionTimeUs); @@ -577,16 +577,16 @@ FAST_CODE void scheduler(void) checkFuncMaxExecutionTimeUs = MAX(checkFuncMaxExecutionTimeUs, checkFuncExecutionTimeUs); task->lastSignaledAtUs = currentTimeUs; task->taskAgeCycles = 1; - task->dynamicPriority = 1 + task->id->staticPriority; + task->dynamicPriority = 1 + task->attr->staticPriority; } else { task->taskAgeCycles = 0; } } else { // Task is time-driven, dynamicPriority is last execution age (measured in desiredPeriods) // Task age is calculated from last execution - task->taskAgeCycles = (cmpTimeUs(currentTimeUs, task->lastExecutedAtUs) / task->id->desiredPeriodUs); + task->taskAgeCycles = (cmpTimeUs(currentTimeUs, task->lastExecutedAtUs) / task->attr->desiredPeriodUs); if (task->taskAgeCycles > 0) { - task->dynamicPriority = 1 + task->id->staticPriority * task->taskAgeCycles; + task->dynamicPriority = 1 + task->attr->staticPriority * task->taskAgeCycles; } } @@ -667,5 +667,5 @@ uint16_t getAverageSystemLoadPercent(void) float schedulerGetCycleTimeMultiplier(void) { - return (float)clockMicrosToCycles(getTask(TASK_GYRO)->id->desiredPeriodUs) / desiredPeriodCycles; + return (float)clockMicrosToCycles(getTask(TASK_GYRO)->attr->desiredPeriodUs) / desiredPeriodCycles; } diff --git a/src/main/scheduler/scheduler.h b/src/main/scheduler/scheduler.h index fb379acba0..2a8fbd662e 100644 --- a/src/main/scheduler/scheduler.h +++ b/src/main/scheduler/scheduler.h @@ -189,11 +189,11 @@ typedef struct { void (*taskFunc)(timeUs_t currentTimeUs); timeDelta_t desiredPeriodUs; // target period of execution const int8_t staticPriority; // dynamicPriority grows in steps of this size -} task_id_t; +} task_attr_t; typedef struct { // Task static data - task_id_t *id; + task_attr_t *attr; // Scheduling uint16_t dynamicPriority; // measurement of how old task was last executed, used to avoid task starvation diff --git a/src/test/unit/scheduler_unittest.cc b/src/test/unit/scheduler_unittest.cc index c0243db93b..172a0f73cb 100644 --- a/src/test/unit/scheduler_unittest.cc +++ b/src/test/unit/scheduler_unittest.cc @@ -95,7 +95,7 @@ extern "C" { extern task_t *queueFirst(void); extern task_t *queueNext(void); - task_id_t task_ids[TASK_COUNT] = { + task_attr_t task_attrs[TASK_COUNT] = { [TASK_SYSTEM] = { .taskName = "SYSTEM", .taskFunc = taskSystemLoad, @@ -170,18 +170,18 @@ extern "C" { TEST(SchedulerUnittest, SetupTasks) { for (int i = 0; i < TASK_COUNT; ++i) { - tasks[i].id = &task_ids[i]; + tasks[i].attr = &task_attrs[i]; } } TEST(SchedulerUnittest, TestPriorites) { - EXPECT_EQ(TASK_PRIORITY_MEDIUM_HIGH, tasks[TASK_SYSTEM].id->staticPriority); - EXPECT_EQ(TASK_PRIORITY_REALTIME, tasks[TASK_GYRO].id->staticPriority); - EXPECT_EQ(TASK_PRIORITY_MEDIUM, tasks[TASK_ACCEL].id->staticPriority); - EXPECT_EQ(TASK_PRIORITY_LOW, tasks[TASK_SERIAL].id->staticPriority); - EXPECT_EQ(TASK_PRIORITY_MEDIUM, tasks[TASK_BATTERY_VOLTAGE].id->staticPriority); + EXPECT_EQ(TASK_PRIORITY_MEDIUM_HIGH, tasks[TASK_SYSTEM].attr->staticPriority); + EXPECT_EQ(TASK_PRIORITY_REALTIME, tasks[TASK_GYRO].attr->staticPriority); + EXPECT_EQ(TASK_PRIORITY_MEDIUM, tasks[TASK_ACCEL].attr->staticPriority); + EXPECT_EQ(TASK_PRIORITY_LOW, tasks[TASK_SERIAL].attr->staticPriority); + EXPECT_EQ(TASK_PRIORITY_MEDIUM, tasks[TASK_BATTERY_VOLTAGE].attr->staticPriority); } TEST(SchedulerUnittest, TestQueueInit) @@ -283,7 +283,7 @@ TEST(SchedulerUnittest, TestQueueArray) EXPECT_EQ(enqueuedTasks, taskQueueSize); for (int taskId = 0; taskId < TASK_COUNT_UNITTEST - 1; ++taskId) { - if (tasks[taskId].id->taskFunc) { + if (tasks[taskId].attr->taskFunc) { setTaskEnabled(static_cast(taskId), true); enqueuedTasks++; EXPECT_EQ(enqueuedTasks, taskQueueSize);