ChibiOS/RT
5.1.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 must always be performed in lock-reverse order. This restriction both improves the performance and is required for an efficient implementation of the priority inheritance mechanism.
Operating under this restriction also ensures that deadlocks are no possible.
By default mutexes are not recursive, this mean that it is not possible to take a mutex already owned by the same thread. It is possible to enable the recursive behavior by enabling the option CH_CFG_USE_MUTEXES_RECURSIVE
.
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_CFG_USE_MUTEXES
option must be enabled in chconf.h
. thread_t
structure. Macros | |
#define | _MUTEX_DATA(name) {_THREADS_QUEUE_DATA(name.queue), NULL, NULL, 0} |
Data part of a static mutex initializer. More... | |
#define | MUTEX_DECL(name) mutex_t name = _MUTEX_DATA(name) |
Static mutex initializer. More... | |
Typedefs | |
typedef struct ch_mutex | mutex_t |
Type of a mutex structure. More... | |
Data Structures | |
struct | ch_mutex |
Mutex structure. More... | |
Functions | |
void | chMtxObjectInit (mutex_t *mp) |
Initializes s mutex_t structure. More... | |
void | chMtxLock (mutex_t *mp) |
Locks the specified mutex. More... | |
void | chMtxLockS (mutex_t *mp) |
Locks the specified mutex. More... | |
bool | chMtxTryLock (mutex_t *mp) |
Tries to lock a mutex. More... | |
bool | chMtxTryLockS (mutex_t *mp) |
Tries to lock a mutex. More... | |
void | chMtxUnlock (mutex_t *mp) |
Unlocks the specified mutex. More... | |
void | chMtxUnlockS (mutex_t *mp) |
Unlocks the specified mutex. More... | |
void | chMtxUnlockAllS (void) |
Unlocks all mutexes owned by the invoking thread. More... | |
void | chMtxUnlockAll (void) |
Unlocks all mutexes owned by the invoking thread. More... | |
static bool | chMtxQueueNotEmptyS (mutex_t *mp) |
Returns true if the mutex queue contains at least a waiting thread. More... | |
static mutex_t * | chMtxGetNextMutexS (void) |
Returns the next mutex in the mutexes stack of the current thread. More... | |
#define _MUTEX_DATA | ( | name | ) | {_THREADS_QUEUE_DATA(name.queue), NULL, NULL, 0} |
#define MUTEX_DECL | ( | name | ) | mutex_t name = _MUTEX_DATA(name) |
void chMtxObjectInit | ( | mutex_t * | mp | ) |
Initializes s mutex_t
structure.
[out] | mp | pointer to a mutex_t structure |
Definition at line 103 of file chmtx.c.
References chDbgCheck, ch_mutex::cnt, ch_mutex::owner, ch_mutex::queue, and queue_init().
Referenced by _factory_init(), _heap_init(), and chHeapObjectInit().
void chMtxLock | ( | mutex_t * | mp | ) |
Locks the specified mutex.
[in] | mp | pointer to the mutex_t structure |
Definition at line 123 of file chmtx.c.
References chMtxLockS(), chSysLock(), and chSysUnlock().
void chMtxLockS | ( | mutex_t * | mp | ) |
Locks the specified mutex.
[in] | mp | pointer to the mutex_t structure |
Definition at line 139 of file chmtx.c.
References CH_STATE_CURRENT, CH_STATE_READY, CH_STATE_SNDMSGQ, CH_STATE_WTCOND, CH_STATE_WTMTX, CH_STATE_WTSEM, chDbgAssert, chDbgCheck, chDbgCheckClassS(), chSchGoSleepS(), chSchReadyI(), ch_mutex::cnt, currp, ch_thread::mtxlist, ch_mutex::next, ch_mutex::owner, ch_thread::prio, ch_mutex::queue, queue_dequeue(), queue_prio_insert(), ch_thread::state, ch_thread::u, and ch_thread::wtmtxp.
Referenced by chCondWaitS(), chCondWaitTimeoutS(), and chMtxLock().
bool chMtxTryLock | ( | mutex_t * | mp | ) |
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_t structure |
true | if the mutex has been successfully acquired |
false | if the lock attempt failed. |
Definition at line 255 of file chmtx.c.
References chMtxTryLockS(), chSysLock(), and chSysUnlock().
bool chMtxTryLockS | ( | mutex_t * | mp | ) |
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_t structure |
true | if the mutex has been successfully acquired |
false | if the lock attempt failed. |
Definition at line 282 of file chmtx.c.
References chDbgAssert, chDbgCheck, chDbgCheckClassS(), ch_mutex::cnt, currp, ch_mutex::next, and ch_mutex::owner.
Referenced by chMtxTryLock().
void chMtxUnlock | ( | mutex_t * | mp | ) |
Unlocks the specified mutex.
[in] | mp | pointer to the mutex_t structure |
Definition at line 323 of file chmtx.c.
References chDbgAssert, chDbgCheck, chMtxQueueNotEmptyS(), chSchReadyI(), chSchRescheduleS(), chSysLock(), chSysUnlock(), ch_mutex::cnt, currp, ch_thread::mtxlist, ch_mutex::next, ch_threads_queue::next, ch_mutex::owner, ch_thread::prio, ch_mutex::queue, queue_fifo_remove(), and ch_thread::realprio.
void chMtxUnlockS | ( | mutex_t * | mp | ) |
Unlocks the specified mutex.
[in] | mp | pointer to the mutex_t structure |
Definition at line 410 of file chmtx.c.
References chDbgAssert, chDbgCheck, chDbgCheckClassS(), chMtxQueueNotEmptyS(), chSchReadyI(), ch_mutex::cnt, currp, ch_thread::mtxlist, ch_mutex::next, ch_threads_queue::next, ch_mutex::owner, ch_thread::prio, ch_mutex::queue, queue_fifo_remove(), and ch_thread::realprio.
Referenced by chCondWaitS(), and chCondWaitTimeoutS().
void chMtxUnlockAllS | ( | void | ) |
Unlocks all mutexes owned by the invoking thread.
Definition at line 487 of file chmtx.c.
References chMtxQueueNotEmptyS(), chSchReadyI(), ch_mutex::cnt, currp, ch_thread::mtxlist, ch_mutex::next, ch_mutex::owner, ch_thread::prio, ch_mutex::queue, queue_fifo_remove(), and ch_thread::realprio.
void chMtxUnlockAll | ( | void | ) |
Unlocks all mutexes owned by the invoking thread.
Definition at line 524 of file chmtx.c.
References chMtxQueueNotEmptyS(), chSchReadyI(), chSchRescheduleS(), chSysLock(), chSysUnlock(), ch_mutex::cnt, currp, ch_thread::mtxlist, ch_mutex::next, ch_mutex::owner, ch_thread::prio, ch_mutex::queue, queue_fifo_remove(), and ch_thread::realprio.
|
inlinestatic |
Returns true
if the mutex queue contains at least a waiting thread.
[out] | mp | pointer to a mutex_t structure |
Definition at line 129 of file chmtx.h.
References chDbgCheckClassS(), ch_mutex::queue, and queue_notempty().
Referenced by chMtxUnlock(), chMtxUnlockAll(), chMtxUnlockAllS(), and chMtxUnlockS().
|
inlinestatic |
Returns the next mutex in the mutexes stack of the current thread.
NULL | if the stack is empty. |
Definition at line 144 of file chmtx.h.
References chThdGetSelfX(), and ch_thread::mtxlist.
Referenced by chCondWaitS(), and chCondWaitTimeoutS().