What is priority inversion?
Short answer: Priority inversion happens when a high-priority task is blocked waiting for a resource (e.g. a mutex) held by a low-priority task, and a medium-priority task that does not need the resource pre-empts the low-priority task. The high-priority task now effectively waits for the medium-priority task, possibly for an unbounded time, which breaks the priority scheme.
Timeline
L takes mutex M
H becomes ready, pre-empts L, tries to take M → blocks
M (medium) becomes ready, pre-empts L (L holds M but has low priority)
M runs for a long time... H waits for M, although H has the highest priority
L finally runs, releases M → H runs (too late)
Bounded vs unbounded: H waiting for L to finish a short critical section is bounded inversion and acceptable. The problem is the unbounded part caused by medium-priority tasks running instead of L.
Real-world example: NASA's Mars Pathfinder (1997) kept resetting because a high-priority bus-management task waited on a mutex held by a low-priority meteorological task while medium-priority communication tasks ran; a watchdog then reset the system. Engineers enabled priority inheritance on that VxWorks mutex remotely.
Fixes:
- Priority inheritance on mutexes (next question): the default for FreeRTOS and Zephyr mutexes.
- Priority ceiling protocol.
- Keep critical sections short and avoid sharing resources across very different priorities; use message passing to a single owner task.
- Disable pre-emption or interrupts for tiny critical sections (no inversion possible, but adds latency).
Remember: semaphores (no owner) cannot do priority inheritance, so using a binary semaphore as a lock reintroduces the problem.