Nested mutexes and when an inherited priority drops
A task that holds two mutexes inherits a priority through one of them. FreeRTOS keeps it until both are released; Zephyr drops it as soon as nobody waits.
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.
Priority inheritance raises a task that holds a mutex while a more urgent task waits for it. The interesting question is when the raised priority comes down again. With one mutex the answer is obvious: when the mutex is released. With two, it is not, and the two kernels in the simulator answer it differently.
The three tasks
Lowtakes mutexA, works one tick, takes mutexBinside it, works three ticks, releasesB, works two more ticks, releasesA, and works one last tick.Bis nested insideA.Medwakes at tick 2 and has three ticks of work. It uses no mutex.Highwakes at tick 3 and needsBfor two ticks of work.
Nobody but Low ever asks for A.
What happens (FreeRTOS)
The priorities are 1 (Low), 2 (Med) and 3 (High).
- Ticks 0 and 1:
LowtakesAand thenB. - Tick 2:
Medwakes and preemptsLow. - Tick 3:
Highwakes, preemptsMedand asks forB.Lowholds it, soLowinherits priority 3 andHighblocks.Lowruns ticks 3 and 4. - Tick 5:
LowgivesBback.Highis made ready, and getsBwhen it runs. ButLowstays at priority 3. It still holdsA, and FreeRTOS only restores the base priority when a task holds no mutex at all. HighandLownow have the same priority, so they take turns, one time slice each:Lowruns tick 5,Hightick 6,Lowtick 7,Hightick 8.- Tick 9:
LowgivesAback, and only now drops to priority 1.Highfinishes and ends at tick 9. Medruns ticks 9 and 10;Lowruns its last tick, 11, and ends.
Between ticks 5 and 9, Low ran at priority 3 although nobody was waiting for anything it held. It
took two ticks that High should have had, and it kept Med, a more urgent task, waiting.
What happens (Zephyr)
The priorities are 14 (Low), 13 (Med) and 12 (High); in Zephyr a lower number is more urgent.
Every wake-up is a tick later than on FreeRTOS (see scheduling), so
Med wakes at tick 3 and High at tick 4.
- Tick 4:
Highasks forB,Lowis raised from 14 to 12, andHighblocks.Lowruns tick 4, the last tick of its work insideB. - Tick 5:
LowunlocksB. Zephyr works the priority out again from the mutexesLowstill holds.Ahas no waiters, soLowgoes straight back to 14.Bis handed toHigh, which runs ticks 5 and 6 and ends. Medruns ticks 7 and 8;Lowfinishes its work inAand ends at tick 12.
High waited one tick for B and was never slowed down after it got it.
Two ways to keep the books
FreeRTOS keeps very little per task: its base priority (uxBasePriority) and a count of the
mutexes it holds (uxMutexesHeld). When a mutex is given back, it cannot tell which of the held
mutexes caused the inheritance, so it plays safe and keeps the raised priority until the count is
zero. The FreeRTOS walkthrough shows the two conditions in
xTaskPriorityDisinherit(). The source calls the rule a simplification, in the comment of the
timeout variant, vTaskPriorityDisinheritAfterTimeout().
Zephyr v4.5.0 keeps a list of the mutexes each thread holds. On every unlock it looks at the first
waiter of each mutex that is still held and takes the most urgent of their priorities, or the
thread’s original priority when nobody waits. The
Zephyr walkthrough goes through
held_mutexes_highest_waiter_prio(). Older Zephyr versions stored the original priority on the
mutex instead, and older articles describe that.
Neither is wrong. The FreeRTOS rule errs on the side of running the holder too urgently, never too little, and costs nothing to compute. Zephyr’s rule is exact but walks a list on every unlock.
What it means for your program
- Inheritance is a safety net, not a design. The time a task runs at a borrowed priority is time that more urgent tasks may lose. On FreeRTOS that time includes everything up to the release of the outermost mutex.
- Keep nested sections short. On FreeRTOS, whatever work is left in the outer section after the inner mutex is released runs at the borrowed priority. Better still, avoid holding one mutex while taking another: it is also the first step towards a deadlock.
- Always take nested mutexes in the same order in every task. This program does (
A, thenB).
What to try
- Turn Time slicing off. On FreeRTOS,
Lownow keeps the CPU at priority 3 from tick 5 until it releasesAat tick 7, whileHighwaits with the mutex it wanted already free. On Zephyr nothing changes. - Set Med urgency to 4.
Medis now the most urgent task and runs its three ticks first; inheritance starts later, butLowagain keeps priority 3 on FreeRTOS until it releasesA.