Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions source/utils/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ int scheduler_add_timer_task(struct scheduler *sched, bool high_prio, int *id,
unsigned int repetitions, bool start_immediately)
{
struct timer_task *tt;
struct timespec t_now;
struct
{
queue_t *timer_list;
Expand Down Expand Up @@ -150,6 +151,9 @@ int scheduler_add_timer_task(struct scheduler *sched, bool high_prio, int *id,

if (start_immediately) {
clock_gettime(CLOCK_MONOTONIC, &(tt->timeout));
} else {
clock_gettime(CLOCK_MONOTONIC, &t_now);
timespecadd(&t_now, &(tt->interval), &(tt->timeout));
}

pthread_mutex_lock(&sched->lock);
Expand Down Expand Up @@ -484,7 +488,7 @@ static int scheduler_calculate_timeout(struct scheduler *sched, struct timespec
printf("Error: **** Timer task expired again before previous execution to complete !!!\n");
}
tt->execute = timespecisset(&tt->timeout);
timespecadd(&t_now, &(tt->interval), &(tt->timeout));
timespecadd(&(tt->timeout), &(tt->interval), &(tt->timeout));
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The timeout calculation has been changed from current-time-based to previous-timeout-based to maintain fixed intervals and prevent timing drift. This ensures timer tasks execute consistently even with small microsecond variations in scheduler timing.

However, be aware that if a timer task execution is significantly delayed (e.g., system suspension, heavy load, or long-running callbacks), the task may execute multiple times in quick succession to catch up to the intended schedule. Consider whether this catch-up behavior is appropriate for all use cases, and document it if it's intentional.

The same consideration applies to the high-priority task timeout calculation on line 501.

Copilot uses AI. Check for mistakes.
}
}
for (i = 0; i < sched->num_hp_tasks; i++) {
Expand All @@ -494,7 +498,7 @@ static int scheduler_calculate_timeout(struct scheduler *sched, struct timespec
printf("Error: **** Timer task expired again before previous execution to complete (high priority) !!!\n");
}
tt->execute = timespecisset(&tt->timeout);
timespecadd(&t_now, &(tt->interval), &(tt->timeout));
timespecadd(&(tt->timeout), &(tt->interval), &(tt->timeout));
}
}
return 0;
Expand Down
Loading