Waiting for a mutex with a timeout
What happens when a task stops waiting for a mutex, and how each kernel takes back the priority the holder inherited from it.
Simulated kernel: FreeRTOS V11.3.3 and Zephyr v4.5.0. This scenario and every other one are in the scheduler playground. The simulator and its data files are not covered by the site's CC BY-SA license; see their license notices.
A task does not have to wait for a mutex forever. It can give a timeout: “wait at most three ticks, then tell me you could not get it”. Real-time code uses this a lot, because a task with a deadline would rather do something else, or report an error, than wait without limit.
A timeout makes priority inheritance harder. While the task waits, the holder runs at the waiter’s priority. When the waiter gives up, that boost has no reason to exist any more, and someone has to take it back.
The program
High asks for the mutex M with a timeout of three ticks. In FreeRTOS the call returns pdTRUE
when it got the mutex, so the code that needs the mutex goes inside an if:
if( xSemaphoreTake( M, 3 ) == pdTRUE )
{
vCompute( 1 ); /* work that needs M */
xSemaphoreGive( M );
}
vCompute( 1 ); /* runs either way */
On Zephyr, k_mutex_lock(&M, K_TICKS(3)) returns 0 on success and -EAGAIN when it times out, and
the program has the same shape. The simulator’s Application tab shows the code for the kernel
you have chosen. A task that gives up skips to the line after the if block, so it never gives back a mutex
it does not hold.
The other two tasks: Low takes M at tick 0 and needs it for seven ticks of work. Med wakes at
tick 2 with three ticks of work that have nothing to do with M.
What happens (FreeRTOS)
- Tick 0:
LowtakesM. - Tick 1:
Highwakes and asks forMwith a timeout of 3.Lowinherits priority 3, andHighblocks until tick 4 at the latest. It sits in two lists at once: the mutex’s waiting list and the delayed list. - Ticks 1 to 3:
Lowruns at priority 3.Medwakes at tick 2 but is less urgent thanLow’s borrowed priority, so it waits. So far this is priority inheritance working as intended. - Tick 4:
High’s timeout runs out. The tick moves it back to the ready list, and it runs again insidexSemaphoreTake(). The mutex is still taken and the time is up, so before returningpdFALSEthe function callsvTaskPriorityDisinheritAfterTimeout().Lowdrops back to priority 1 while it still holdsM. Highskips theifblock, does its last tick of work and ends.Medis now the most urgent task and runs ticks 5 to 7.Lowruns again at tick 8 and givesMback at tick 11.
What happens (Zephyr)
Every timeout on Zephyr is one tick longer (see scheduling): High
wakes at tick 2, and its three-tick wait for M ends at tick 6.
- Tick 2:
Highasks forM;Lowgoes from priority 14 to 12.Lowruns ticks 2 to 5, andMed, awake from tick 3, waits. - Tick 6: the timeout fires.
Highruns again insidek_mutex_lock(), which sees that it timed out, lowersLowback to 14 and returns-EAGAIN. Highdoes its last tick of work and ends;Medruns ticks 7 to 9;LowgivesMback at tick 11.
Taking the boost back
Both kernels lower the holder when a waiter gives up, but they compute the new priority from different information.
FreeRTOS looks at the mutex the waiter gave up on. xQueueSemaphoreTake() finds the most urgent
task still in that mutex’s waiting list and passes its priority to
vTaskPriorityDisinheritAfterTimeout(). The holder is set to the higher of that priority and its
own base priority, but only if it holds exactly one mutex. If it holds more, FreeRTOS cannot
tell which one caused the boost, so it leaves the priority alone, the same rule that makes
nested mutexes keep a raised priority. The
FreeRTOS walkthrough shows the
function.
Zephyr recomputes the owner’s priority from scratch, over every mutex the owner holds: the most urgent first waiter of each, or the owner’s original priority if there is none. That is exact. One limitation is documented in the source: only the direct owner is lowered. If that owner was itself waiting for another mutex, the owners further along the chain keep their raised priority until they unlock. See the Zephyr walkthrough.
In this scenario Low holds only M and nobody else waits, so both kernels put it straight back to
its own priority.
A timeout is not a deadline
The timeout bounds how long High waits; it does not guarantee that High runs when it expires. The
expired task is made ready like any other, and runs when it is the most urgent. Here it runs at once,
but only because time slicing lets it in: Low still runs at the priority it borrowed from High,
so the two are equal. Turn time slicing off (below) to see it wait.
What to try
- Turn Time slicing off.
High’s timeout expires on time, butLowruns at the same borrowed priority and nothing makes it give up the CPU, soHighonly runs whenLowreleasesM. Then the kernels differ: FreeRTOS’sxSemaphoreTake()goes round its loop once more, findsMfree and takes it, late (tick 7). Zephyr’sk_mutex_lock()was already woken by its timeout and returns-EAGAIN, althoughMis free by the timeHighruns. - Set Med urgency to 4.
Medis now more urgent than everyone, so inheritance cannot helpLowfinish, andHighgives up withoutLowhaving made much progress. The timeout is the only thing that limitsHigh’s wait.