|
ChibiOS/RT
2.6.0 |
|
Mutexes related APIs and services.
A mutex is a threads synchronization object that can be in two distinct states:
Operations defined for mutexes:
In ChibiOS/RT the Unlock operations are always performed in lock-reverse order. The unlock API does not even have a parameter, the mutex to unlock is selected from an internal, per-thread, stack of owned mutexes. This both improves the performance and is required for an efficient implementation of the priority inheritance mechanism.
The mutexes in ChibiOS/RT implements the full priority inheritance mechanism in order handle the priority inversion problem.
When a thread is queued on a mutex, any thread, directly or indirectly, holding the mutex gains the same priority of the waiting thread (if their priority was not already equal or higher). The mechanism works with any number of nested mutexes and any number of involved threads. The algorithm complexity (worst case) is N with N equal to the number of nested mutexes.
CH_USE_MUTEXES option must be enabled in chconf.h. Thread structure. Data Structures | |
| struct | Mutex |
| Mutex structure. More... | |
Functions | |
| void | chMtxInit (Mutex *mp) |
Initializes s Mutex structure. | |
| void | chMtxLock (Mutex *mp) |
| Locks the specified mutex. | |
| void | chMtxLockS (Mutex *mp) |
| Locks the specified mutex. | |
| bool_t | chMtxTryLock (Mutex *mp) |
| Tries to lock a mutex. | |
| bool_t | chMtxTryLockS (Mutex *mp) |
| Tries to lock a mutex. | |
| Mutex * | chMtxUnlock (void) |
| Unlocks the next owned mutex in reverse lock order. | |
| Mutex * | chMtxUnlockS (void) |
| Unlocks the next owned mutex in reverse lock order. | |
| void | chMtxUnlockAll (void) |
| Unlocks all the mutexes owned by the invoking thread. | |
Macro Functions | |
| #define | chMtxQueueNotEmptyS(mp) notempty(&(mp)->m_queue) |
Returns TRUE if the mutex queue contains at least a waiting thread. | |
Defines | |
| #define | _MUTEX_DATA(name) {_THREADSQUEUE_DATA(name.m_queue), NULL, NULL} |
| Data part of a static mutex initializer. | |
| #define | MUTEX_DECL(name) Mutex name = _MUTEX_DATA(name) |
| Static mutex initializer. | |
Typedefs | |
| typedef struct Mutex | Mutex |
| Mutex structure. | |
| void chMtxInit | ( | Mutex * | mp | ) |
Initializes s Mutex structure.
| [out] | mp | pointer to a Mutex structure |
Definition at line 86 of file chmtx.c.
References chDbgCheck, Mutex::m_owner, Mutex::m_queue, and queue_init.
Referenced by _heap_init(), adcObjectInit(), chHeapInit(), i2cObjectInit(), and spiObjectInit().
| void chMtxLock | ( | Mutex * | mp | ) |
Locks the specified mutex.
| [in] | mp | pointer to the Mutex structure |
Definition at line 103 of file chmtx.c.
References chMtxLockS(), chSysLock, and chSysUnlock.
Referenced by adcAcquireBus(), i2cAcquireBus(), and spiAcquireBus().

| void chMtxLockS | ( | Mutex * | mp | ) |
Locks the specified mutex.
| [in] | mp | pointer to the Mutex structure |
Definition at line 121 of file chmtx.c.
References chDbgAssert, chDbgCheck, chDbgCheckClassS(), chSchGoSleepS(), chSchReadyI(), currp, dequeue(), Mutex::m_next, Mutex::m_owner, Mutex::m_queue, Thread::p_mtxlist, Thread::p_prio, Thread::p_state, prio_insert(), THD_STATE_CURRENT, THD_STATE_READY, THD_STATE_SNDMSGQ, THD_STATE_WTCOND, THD_STATE_WTMTX, THD_STATE_WTSEM, and Thread::wtobjp.
Referenced by chCondWaitS(), chCondWaitTimeoutS(), and chMtxLock().

Tries to lock a mutex.
This function attempts to lock a mutex, if the mutex is already locked by another thread then the function exits without waiting.
| [in] | mp | pointer to the Mutex structure |
| TRUE | if the mutex has been successfully acquired |
| FALSE | if the lock attempt failed. |
Definition at line 206 of file chmtx.c.
References chMtxTryLockS(), chSysLock, and chSysUnlock.

Tries to lock a mutex.
This function attempts to lock a mutex, if the mutex is already taken by another thread then the function exits without waiting.
| [in] | mp | pointer to the Mutex structure |
| TRUE | if the mutex has been successfully acquired |
| FALSE | if the lock attempt failed. |
Definition at line 234 of file chmtx.c.
References chDbgCheck, chDbgCheckClassS(), currp, FALSE, Mutex::m_next, Mutex::m_owner, and TRUE.
Referenced by chMtxTryLock().

| Mutex * chMtxUnlock | ( | void | ) |
Unlocks the next owned mutex in reverse lock order.
Definition at line 257 of file chmtx.c.
References chDbgAssert, chMtxQueueNotEmptyS, chSchWakeupS(), chSysLock, chSysUnlock, currp, fifo_remove(), Mutex::m_next, Mutex::m_owner, Mutex::m_queue, Thread::p_mtxlist, ThreadsQueue::p_next, Thread::p_prio, Thread::p_realprio, and RDY_OK.
Referenced by adcReleaseBus(), i2cReleaseBus(), and spiReleaseBus().

| Mutex * chMtxUnlockS | ( | void | ) |
Unlocks the next owned mutex in reverse lock order.
Definition at line 317 of file chmtx.c.
References chDbgAssert, chDbgCheckClassS(), chMtxQueueNotEmptyS, chSchReadyI(), currp, fifo_remove(), Mutex::m_next, Mutex::m_owner, Mutex::m_queue, Thread::p_mtxlist, ThreadsQueue::p_next, Thread::p_prio, and Thread::p_realprio.
Referenced by chCondWaitS(), and chCondWaitTimeoutS().

| void chMtxUnlockAll | ( | void | ) |
Unlocks all the mutexes owned by the invoking thread.
Definition at line 374 of file chmtx.c.
References chMtxQueueNotEmptyS, chSchReadyI(), chSchRescheduleS(), chSysLock, chSysUnlock, currp, fifo_remove(), Mutex::m_next, Mutex::m_owner, Mutex::m_queue, Thread::p_mtxlist, Thread::p_prio, and Thread::p_realprio.

| #define _MUTEX_DATA | ( | name | ) | {_THREADSQUEUE_DATA(name.m_queue), NULL, NULL} |
| #define MUTEX_DECL | ( | name | ) | Mutex name = _MUTEX_DATA(name) |
Static mutex initializer.
Statically initialized mutexes require no explicit initialization using chMtxInit().
| [in] | name | the name of the mutex variable |
| #define chMtxQueueNotEmptyS | ( | mp | ) | notempty(&(mp)->m_queue) |
Returns TRUE if the mutex queue contains at least a waiting thread.
Definition at line 96 of file chmtx.h.
Referenced by chMtxUnlock(), chMtxUnlockAll(), and chMtxUnlockS().