orocos-toolchain / rtt

Orocos Real-Time Toolkit
http://www.orocos.org
Other
72 stars 79 forks source link

Enable priority inheritance for RTT mutexes (gnulinux) #312

Open meyerj opened 4 years ago

meyerj commented 4 years ago

By default pthread mutexes used in the implementation of the gnulinux target do not have priority inheritance enabled. Priority inheritance is one way to prevent priorty inversion issues in real-time systems. So this is a pretty important feature for Orocos RTT, too.

The behavior of setting the attribute PTHREAD_PRIO_INHERIT is well explained in the POSIX documentation for pthread_mutexattr_setprotocol:

The protocol attribute defines the protocol to be followed in utilizing mutexes. The value of protocol may be one of:

PTHREAD_PRIO_INHERIT PTHREAD_PRIO_NONE PTHREAD_PRIO_PROTECT

which are defined in the <pthread.h> header. The default value of the attribute shall be PTHREAD_PRIO_NONE.

When a thread owns a mutex with the PTHREAD_PRIO_NONE protocol attribute, its priority and scheduling shall not be affected by its mutex ownership.

When a thread is blocking higher priority threads because of owning one or more non-robust mutexes with the PTHREAD_PRIO_INHERIT protocol attribute, it shall execute at the higher of its priority or the priority of the highest priority thread waiting on any of the non-robust mutexes owned by this thread and initialized with this protocol.

[...]

Xenomai mutex services enforce a priority inheritance protocol in order to solve priority inversions (https://xenomai.org/documentation/xenomai-2.6/html/api/group__mutex.html).

I am not sure whether priority inheritance could be enabled for pthread mutexes on macOS X, too. But before #310 RTT used the Boost.Thread mutex implementation to implement timed mutexes. With #310 Boost.Thread usage has been dropped, but because waiting on a mutex held by another thread is actually implemented by waiting on a condition variable, priority inheritance cannot easily be applied.