core/mutex.doc.md
@defgroup core_sync_mutex Mutex @ingroup core_sync @brief Mutex for thread synchronization
@warning By default, no mitigation against priority inversion is
employed. If your application is subject to priority inversion
and cannot tolerate the additional delay this can cause, use
module core_mutex_priority_inheritance to employ
priority inheritance as mitigation.
A mutex_t contains basically a point, which can have one of the following
values:
NULL, in case it is unlockedMUTEX_LOCKED in case it is locked, but no other thread is waiting on itthread_t structures) blocked waiting for obtaining the mutex. This
list is terminated by NULL, not by MUTEX_LOCKEDThe same information graphically:
Unlocked mutex:
+-------+
| Mutex | --> NULL
+-------+
Locked mutex, no waiters:
+-------+
| Mutex | --> MUTEX_LOCKED
+-------+
Locked mutex, one waiter:
+-------+ +--------+
| Mutex | --> | Waiter | --> NULL
+-------+ +--------+
Locked mutex, 2 waiters:
+-------+ +--------+ +--------+
| Mutex | --> | Waiter | --> | Waiter | --> NULL
+-------+ +--------+ +--------+
If a mutex_lock() is called, one of the following happens:
NULL), its value is changed to
MUTEX_LOCKED and the call to mutex_lock() returns right away without
blocking.MUTEX_LOCKED, it will be changed to point to
the thread_t of the running thread. The single item list is terminated
by setting the thread_t::rq_entry.next of the running thread to NULL.
The running thread blocks as described below.In case 2) and 3), the running thread will mark itself as blocked (waiting
for a mutex) and yields. Once control is transferred back to this thread
(which is done in the call to mutex_unlock()), it has the mutex and the
function mutex_lock() returns.
If mutex_unlock() is called, one of the following happens:
NULL), the call returns
without modifying the mutex.MUTEX_LOCKED), it is
unlocked by setting its value to NULL.thread_t from the linked list of waiters is removed
from the list.
MUTEX_LOCK.The module core_mutex_debug can be used to print on whom mutex_lock()
is waiting. This information includes the thread ID of the owner and the
program counter (PC) from where mutex_lock() was called. Note that the
information is only valid if:
MUTEX_INIT_LOCKEDcpu_get_caller_pc() is implemented for the target
architecture. (The thread ID will remain valid, though.)