How the FreeRTOS Timer Service Task Runs Timer Callbacks
See how FreeRTOS keeps software timers: one daemon task, a command queue and two sorted lists, and what that means for callback timing and order.

When you call xTimerStart(), nothing happens to the timer in your calling task. The call drops a message in a queue and returns. Another task, which you did not write and which has the name "Tmr Svc", picks the message up, files the timer in a list, and later, when the time comes, calls your callback. That arrangement explains why callbacks cannot block, why they can be late, and why a timer started a moment ago can still fire on time. This post follows one timer through that machinery on FreeRTOS kernel V11.1.0, using plain language and a small program that proves each step. The source is in timers.c, and the overview is in the guide to software timers and tickless idle.
Three pieces
The timer service task. One task, created by vTaskStartScheduler() when configUSE_TIMERS is 1. Its priority is configTIMER_TASK_PRIORITY and its stack is configTIMER_TASK_STACK_DEPTH words. All callbacks run in it. It is also called the timer daemon.
The command queue. An ordinary FreeRTOS queue of length configTIMER_QUEUE_LENGTH. Every call that changes a timer, start, stop, reset, change period, delete, sends one message to this queue. The message holds the command, the timer handle, and the tick count at the moment you called.
The lists. Two lists of active timers, each sorted by expiry time. One holds timers that expire before the tick counter next wraps around, and the other those that expire after. The task swaps them when the counter overflows.
Proof that callbacks run in the timer task
The task's name is visible from the callback:
#include <stdio.h>
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
static void cb(TimerHandle_t t)
{
(void)t;
printf("[%lu] callback runs in task \"%s\" at priority %lu\n",
(unsigned long)xTaskGetTickCount(),
pcTaskGetName(NULL),
(unsigned long)uxTaskPriorityGet(NULL));
}
void whoami_demo(void)
{
TimerHandle_t t = xTimerCreate("t", pdMS_TO_TICKS(100), pdFALSE, NULL, cb);
xTimerStart(t, 0);
}[100] callback runs in task "Tmr Svc" at priority 30The name is Tmr Svc, and the priority shown is configTIMER_TASK_PRIORITY, taken here as 30 for a system with configMAX_PRIORITIES of 32. Your priority will be whatever you configured. Because the callback runs in that task, it uses that task's stack and has that task's priority, not the priority of the code that started the timer.
What happens on xTimerStart
Follow xTimerStart(t, 0) called from your task at tick 1,000, for a timer with a period of 200 ticks:
xTimerStart()is a macro forxTimerGenericCommand(), which builds a message: commandtmrCOMMAND_START, timert, and the current tick count, 1,000.It sends the message to the command queue with
xQueueSendToBack(). This is a short, bounded operation. The call returnspdPASS.The timer service task, which was blocked waiting on the queue, becomes ready. If its priority is higher than your task's, it runs at once. Otherwise it runs when your task blocks.
The task receives the message and computes the expiry: the tick count stored in the message plus the period, 1,000 plus 200 gives 1,200. This is the important detail. It does not use the current time, so a delay in the task running does not move the expiry.
It inserts the timer into the active list, sorted by that expiry.
It goes back to waiting.
If the timer task was delayed by a higher-priority task and only processed the message at tick 1,050, the timer still expires at 1,200. A command that is processed late still gets the right deadline.
How the task waits
The timer task's loop is short. In words:
Look at the head of the active list. If a timer is there, its expiry is the next time that matters. If the list is empty, there is nothing to wait for.
If the head timer has already expired, run it now.
Otherwise block on the command queue, with a timeout equal to the time until the head timer's expiry. If the list is empty, block with no timeout.
Wake up for one of two reasons: a command arrived, in which case process it and loop, or the timeout ended, in which case the head timer is due, so run it.
That is the whole design, and the one place a task blocks is the queue receive. A command arriving wakes it early, which is how a new timer with an earlier deadline than the head one is noticed.
Running a timer
When the head timer's time arrives, the task:
Removes it from the active list.
For an auto-reload timer, computes the next expiry and puts it back in the list before calling the callback.
Calls the callback.
For an auto-reload timer the next expiry is the previous expiry plus the period, not the time the callback finished. So the callback's run time does not change the schedule. If the task was so late that the next expiry is already in the past, it runs the callback again immediately, once for each missed period, until it catches up.
The ordering of callbacks follows the list order. Two timers with the same expiry run in the order they were inserted, which is the order their start commands were processed.
static void a(TimerHandle_t t) { (void)t; printf("A\n"); }
static void b(TimerHandle_t t) { (void)t; printf("B\n"); }
void order_demo(void)
{
TimerHandle_t ta = xTimerCreate("A", pdMS_TO_TICKS(100), pdFALSE, NULL, a);
TimerHandle_t tb = xTimerCreate("B", pdMS_TO_TICKS(100), pdFALSE, NULL, b);
xTimerStart(ta, 0);
xTimerStart(tb, 0);
}A
BBoth expire at tick 100, and A was started first, so A runs first. Start them in the opposite order and the output swaps.
Why the design has consequences
Callbacks cannot block. The timer task is the only task that drains the command queue and runs due timers. A callback that waits for a semaphore for 50 ms makes every other timer late by 50 ms, and a callback that waits on a queue that only the timer task can drain, for example by sending a timer command with a block time, can deadlock the timer task against itself.
Callbacks can be late. The timer task is a task: if a higher-priority task is running, it waits. The expiry time is when the timer is due, not when the callback starts. Give the timer task a priority that fits how urgent your callbacks are, keeping in mind that a high priority also lets callbacks preempt your application tasks.
Short callbacks are polite. Callbacks run one after another. If three timers expire at the same tick and each takes 2 ms, the third callback starts 4 ms after the tick.
The queue can fill. Commands from many tasks and interrupts all go through configTIMER_QUEUE_LENGTH slots. If the timer task is held off while they pile up, a start call with a zero block time returns pdFAIL and the command is lost. Size the queue for the busiest burst.
The timer task needs stack. A callback that calls printf() uses more stack than one that sets a flag. Size configTIMER_TASK_STACK_DEPTH for the deepest callback.
Seeing the lateness
You can make a callback late on purpose and watch the schedule hold:
static void busy_task(void *arg) /* priority above the timer task */
{
(void)arg;
vTaskDelay(pdMS_TO_TICKS(95));
TickType_t end = xTaskGetTickCount() + pdMS_TO_TICKS(30);
while ((int32_t)(xTaskGetTickCount() - end) < 0) { } /* hog the CPU for 30 ms */
vTaskDelete(NULL);
}
static void periodic_cb(TimerHandle_t t)
{
(void)t;
printf("[%lu] periodic\n", (unsigned long)xTaskGetTickCount());
}Create a 100-tick auto-reload timer and a busy_task with a priority higher than configTIMER_TASK_PRIORITY. The busy task holds the processor from tick 95 to 125. The timer task cannot run at 100, so the first callback is late, and the next ones return to schedule:
[125] periodic
[200] periodic
[300] periodicThe first callback ran at 125 instead of 100, but the second ran at 200, not 225, because the next expiry was computed from the scheduled time. A delayed callback does not push later ones.


