From 6ca0d910958410b5fa17c35d4c66361963ebfbdd Mon Sep 17 00:00:00 2001 From: antara-chugh Date: Fri, 23 Aug 2024 13:09:59 -0700 Subject: [PATCH] added comments to scheduler --- src/scheduler.cpp | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/scheduler.cpp b/src/scheduler.cpp index 7305b041..e8ee62d7 100644 --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -58,7 +58,7 @@ void Scheduler::initializeScheduler() * * This function iterates through a schedule table to find the event that * should run next based on the current system time. - * It ensures that no overlapping events are scheduled by checking with + * * * @see tests/googletests.cpp for intended behavior * @@ -71,6 +71,7 @@ void Scheduler::initializeScheduler() * @param currentTime The current system time * @return Returns SUCCESS if a suitable event is found, * TASK_SEARCH_FAIL otherwise. + * */ SCH_error_e Scheduler::getNextTask(DeploymentSchedule_t** p_nextEvent, @@ -80,28 +81,45 @@ SCH_error_e Scheduler::getNextTask(DeploymentSchedule_t** p_nextEvent, std::uint32_t minNextTime = UINT32_MAX; DeploymentSchedule_t* nextEvent = nullptr; - // Iterate through each event in the schedule table in reverse order. - + /*Iterate through each event in the schedule table in reverse order, with + the goal of determining if a lower prioirty task can run before a higher + priority task must run. */ for (int idx = tableSize - 1; idx >= 0; idx--) { bool canSet = true; DeploymentSchedule_t& currentEvent = scheduleTable[idx]; StateInformation& currentEventState = scheduleTable[idx].state; std::uint32_t runTime = currentEventState.nextRunTime; + + /* check if a delay exists. Current time comes from clock, which starts + at time 0 and is increasing. if currentTime is ahead of runTime, there + is a delay. We will try to run the task immediately. */ int delay = currentTime - runTime; + if (delay > 0) { runTime = currentTime; } - else + + /*if delay is negative, clock has not reached the proposed runTime of a + task and there is no delay. Set delay to 0*/ + + if(delay<=0) { delay = 0; } + if (delay >= currentEvent.maxDelay) { //! TODO: send warning } + //Finish time of a task is when we want to run the task plus its duration int expected_completion = runTime + currentEvent.maxDuration; + + /*Iterate through all tasks of higher prioirty. If the time the task + will finish overlaps with any task of higher prioirty, we cannot run this + task next. The highest prioirty task will not be checked against other + tasks, so by default that will run*/ int j = 0; for (int j = 0; (j < idx) && canSet; j++) { @@ -110,6 +128,8 @@ SCH_error_e Scheduler::getNextTask(DeploymentSchedule_t** p_nextEvent, canSet = false; } } + /*If there were no conflicts with higher prioirity tasks, we can set the + next task to the task in question*/ if (canSet) { *p_nextEvent = ¤tEvent; @@ -118,7 +138,9 @@ SCH_error_e Scheduler::getNextTask(DeploymentSchedule_t** p_nextEvent, currentEvent.ensembleInterval; currentEventState.measurementCount++; - + + /*If delay was greater than 0, we want to shift all future occurences + of the task by delay amount to re-establish a constant frequency*/ if (delay > 0) { FLOG_AddError(FLOG_SCHEDULER_DELAY_EXCEEDED,