ChibiOS
21.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 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) {__CH_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 thread_t * | chMtxGetOwnerI (mutex_t *mp) |
Returns the mutex owner thread. More... | |
static mutex_t * | chMtxGetNextMutexX (void) |
Returns the next mutex in the mutexes stack of the current thread. More... | |
#define __MUTEX_DATA | ( | name | ) | {__CH_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 ch_queue_init(), chDbgCheck, ch_mutex::cnt, ch_mutex::owner, and ch_mutex::queue.
Referenced by __factory_init().
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 chThdGetSelfX.
Referenced by 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 257 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 284 of file chmtx.c.
References chThdGetSelfX.
Referenced by chMtxTryLock().
void chMtxUnlock | ( | mutex_t * | mp | ) |
Unlocks the specified mutex.
[in] | mp | pointer to the mutex_t structure |
Definition at line 326 of file chmtx.c.
References ch_queue_fifo_remove(), chDbgAssert, chDbgCheck, chMtxQueueNotEmptyS(), chSchReadyI(), chSchRescheduleS(), chSysLock, chSysUnlock, chThdGetSelfX, ch_mutex::cnt, ch_thread::hdr, ch_thread::mtxlist, ch_mutex::next, ch_queue::next, ch_mutex::owner, ch_thread::pqueue, ch_priority_queue::prio, ch_mutex::queue, and ch_thread::realprio.
void chMtxUnlockS | ( | mutex_t * | mp | ) |
Unlocks the specified mutex.
[in] | mp | pointer to the mutex_t structure |
Definition at line 413 of file chmtx.c.
References chThdGetSelfX.
void chMtxUnlockAllS | ( | void | ) |
Unlocks all mutexes owned by the invoking thread.
Definition at line 490 of file chmtx.c.
References ch_queue_fifo_remove(), chMtxQueueNotEmptyS(), chSchReadyI(), chSchRescheduleS(), chThdGetSelfX, ch_mutex::cnt, ch_thread::hdr, ch_thread::mtxlist, ch_mutex::next, ch_mutex::owner, ch_thread::pqueue, ch_priority_queue::prio, ch_mutex::queue, and ch_thread::realprio.
Referenced by chMtxUnlockAll().
void chMtxUnlockAll | ( | void | ) |
Unlocks all mutexes owned by the invoking thread.
Definition at line 531 of file chmtx.c.
References chMtxUnlockAllS(), chSysLock, and chSysUnlock.
|
inlinestatic |
Returns true
if the mutex queue contains at least a waiting thread.
[out] | mp | pointer to a mutex_t structure |
Definition at line 128 of file chmtx.h.
Referenced by chMtxUnlock(), and chMtxUnlockAllS().
Returns the mutex owner thread.
[out] | mp | pointer to a mutex_t structure |
NULL | if the mutex is not owned. |
|
inlinestatic |
Returns the next mutex in the mutexes stack of the current thread.
NULL | if the stack is empty. |
Definition at line 159 of file chmtx.h.
References chThdGetSelfX.
Referenced by chCondWaitS(), and chCondWaitTimeoutS().