Priority inversion: review shared-resource wait times
A high-priority task can need a resource currently held by a lower-priority task. Without an appropriate protocol, intermediate-priority work may delay the owner and therefore indirectly delay the waiting task. The resource relationship matters as much as the nominal priorities. A lock that protects a tiny update should not accidentally protect a long blocking operation.
The contract
Start by listing each resource, the contexts that acquire it, and the largest amount of time they can hold it under the stated assumptions. Then identify whether the selected RTOS implements inheritance for that kind of lock. A binary semaphore and a mutex may have different ownership semantics; refer to the exact RTOS documentation rather than relying on a name.
A small executable example
#include <stdint.h>
#include <assert.h>
#include <stdio.h>
/* Toy budget: known non-overlapping components, not a scheduler model. */
static uint64_t budget(uint32_t execution, uint32_t blocking, uint32_t interference) {
return (uint64_t)execution + blocking + interference;
}
int main(void) {
assert(budget(20, 15, 40) == 75);
assert(budget(20, 15, 40) <= 100);
assert(budget(20, 60, 40) > 100);
puts("toy timing budgets checked");
}
toy timing budgets checked
Compiled with -Wall -Wextra -Werror and run under AddressSanitizer + UBSan.
Learning this for work or an interview? Lessons 1 and 2 of DSA for Firmware are free, with labs and C exercises. Start Lesson 1
What the example does not establish
The arithmetic here combines already established bounds in a simplified model. It does not compute a response-time bound for a real periodic task set. In actual analysis, interference can recur during the response interval, multiple resources can interact, and scheduler policy, interrupt time and release jitter matter. Use a scheduling analysis appropriate to the target and verify the model's assumptions.
Checks to add in your own project
Review nested lock ordering and whether a path performs I/O while holding a lock. Test under realistic contention on the target, with timing instrumentation that does not materially change behavior. Record the workload and the difference between a measured maximum and a justified upper bound. If a requirement cannot be met, reduce or restructure the shared-resource dependency rather than assuming a priority adjustment will solve it.
DSA for Firmware teaches it one real bug at a time.
- 28 lessons, each starting from a real firmware bug.
- 110 C exercises with tests, and a reference solution for every one.
- Lessons 1 and 2 are free. No sign-up.
Get the next article by email
One short email when a new article or free lesson is published. No spam. Unsubscribe at any time by replying to any email. See the privacy policy.