URL
https://opencores.org/ocsvn/test_project/test_project/trunk
Subversion Repositories test_project
[/] [test_project/] [trunk/] [linux_sd_driver/] [Documentation/] [rt-mutex.txt] - Rev 62
Compare with Previous | Blame | View Log
RT-mutex subsystem with PI support----------------------------------RT-mutexes with priority inheritance are used to support PI-futexes,which enable pthread_mutex_t priority inheritance attributes(PTHREAD_PRIO_INHERIT). [See Documentation/pi-futex.txt for more detailsabout PI-futexes.]This technology was developed in the -rt tree and streamlined forpthread_mutex support.Basic principles:-----------------RT-mutexes extend the semantics of simple mutexes by the priorityinheritance protocol.A low priority owner of a rt-mutex inherits the priority of a higherpriority waiter until the rt-mutex is released. If the temporarilyboosted owner blocks on a rt-mutex itself it propagates the priorityboosting to the owner of the other rt_mutex it gets blocked on. Thepriority boosting is immediately removed once the rt_mutex has beenunlocked.This approach allows us to shorten the block of high-prio tasks onmutexes which protect shared resources. Priority inheritance is not amagic bullet for poorly designed applications, but it allowswell-designed applications to use userspace locks in critical parts ofan high priority thread, without losing determinism.The enqueueing of the waiters into the rtmutex waiter list is done inpriority order. For same priorities FIFO order is chosen. For eachrtmutex, only the top priority waiter is enqueued into the owner'spriority waiters list. This list too queues in priority order. Wheneverthe top priority waiter of a task changes (for example it timed out orgot a signal), the priority of the owner task is readjusted. [Thepriority enqueueing is handled by "plists", see include/linux/plist.hfor more details.]RT-mutexes are optimized for fastpath operations and have no internallocking overhead when locking an uncontended mutex or unlocking a mutexwithout waiters. The optimized fastpath operations require cmpxchgsupport. [If that is not available then the rt-mutex internal spinlockis used]The state of the rt-mutex is tracked via the owner field of the rt-mutexstructure:rt_mutex->owner holds the task_struct pointer of the owner. Bit 0 and 1are used to keep track of the "owner is pending" and "rtmutex haswaiters" state.owner bit1 bit0NULL 0 0 mutex is free (fast acquire possible)NULL 0 1 invalid stateNULL 1 0 Transitional state*NULL 1 1 invalid statetaskpointer 0 0 mutex is held (fast release possible)taskpointer 0 1 task is pending ownertaskpointer 1 0 mutex is held and has waiterstaskpointer 1 1 task is pending owner and mutex has waitersPending-ownership handling is a performance optimization:pending-ownership is assigned to the first (highest priority) waiter ofthe mutex, when the mutex is released. The thread is woken up and onceit starts executing it can acquire the mutex. Until the mutex is takenby it (bit 0 is cleared) a competing higher priority thread can "steal"the mutex which puts the woken up thread back on the waiters list.The pending-ownership optimization is especially important for theuninterrupted workflow of high-prio tasks which repeatedlytakes/releases locks that have lower-prio waiters. Without thisoptimization the higher-prio thread would ping-pong to the lower-priotask [because at unlock time we always assign a new owner].(*) The "mutex has waiters" bit gets set to take the lock. If the lockdoesn't already have an owner, this bit is quickly cleared if there areno waiters. So this is a transitional state to synchronize with lookingat the owner field of the mutex and the mutex owner releasing the lock.
