Skip to content

Commit

Permalink
added comments to scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
antara-chugh committed Aug 23, 2024
1 parent 243be4d commit 6ca0d91
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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,
Expand All @@ -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++)
{
Expand All @@ -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 = &currentEvent;
Expand All @@ -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,
Expand Down

0 comments on commit 6ca0d91

Please sign in to comment.