Reputation: 13116
As known, priority inversion problem - when a thread with higher priority waits for a thread with lower-priority: https://en.wikipedia.org/wiki/Priority_inversion
It happen when we have 3 threads: L (low), M (medium), H (high-priority). L and H use the same mutex, but L acquire it early than H, and H blocked and goes to sleep. And then M occupies CPU-core because has higher priority than L, and L goes to sleep, but mutex still acquired. L & H are sleeping, but M is working.
There are some solutions of priority inversion:
Does Linux use some of the solutions of priority inversion, and which of its?
Upvotes: 2
Views: 4510
Reputation: 13116
Linux uses Priority inheritance.
Priority inversion can be solved by using function int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol);
Where protocol
is:
PTHREAD_PRIO_NONE
- nothing going on when acquires mutex ownershipPTHREAD_PRIO_INHERIT
- Priority inheritancePTHREAD_PRIO_PROTECT
- uses a fixed level of the priority in accordance with prioceiling
value which got by function pthread_mutexattr_getprioceiling()
When a thread owns one or more mutexes initialized with the PTHREAD_PRIO_PROTECT protocol, it shall execute at the higher of its priority or the highest of the priority ceilings of all the mutexes owned by this thread and initialized with this attribute, regardless of whether other threads are blocked on any of these mutexes or not.
Upvotes: 4