Recursive mutexes with priority inheritance.
More...
Recursive mutexes with priority inheritance.
The owning thread may lock a mutex again; it is released once every lock has been undone. While a higher priority thread waits, the owner runs at the waiter's priority until it holds no mutex any more. Waiters are served highest priority first, FIFO within a priority, and an unlock hands the mutex directly to the next waiter. Mutexes cannot be used from ISRs.
Non-recursive use is expressed with pbl_mutex_assert_held().
int cache_update(const struct entry *e) {
if (rc != 0) {
return rc;
}
prv_store(e);
return 0;
}
static int pbl_mutex_lock(struct pbl_mutex *m, pbl_timeout_t timeout)
Lock a mutex.
Definition mutex.h:102
void pbl_mutex_unlock(struct pbl_mutex *m)
Undo one lock of a mutex.
#define PBL_MUTEX_DEFINE(name)
Define an unlocked mutex, usable without pbl_mutex_init().
Definition mutex.h:57
#define PBL_MSEC(ms)
Timeout in milliseconds, rounded down to whole ticks.
Definition types.h:62
◆ pbl_mutex
Recursive mutex.
Not usable from ISRs.
| Data Fields |
|
struct pbl_mutex_backend |
backend |
Backend state. |
|
uint32_t |
count |
Recursion depth of the owner, 0 when unlocked. |
|
uintptr_t |
lock_lr |
Return address of the outermost lock, for diagnostics (CONFIG_KERNEL_MUTEX_LOCK_LR). |
|
struct pbl_thread * |
owner |
Owning thread, NULL when unlocked. |
◆ PBL_MUTEX_DEFINE
Define an unlocked mutex, usable without pbl_mutex_init().
- Parameters
-
| name | Name of the mutex variable. |
◆ PBL_MUTEX_INITIALIZER
| #define PBL_MUTEX_INITIALIZER {.owner = NULL, .count = 0, .lock_lr = 0} |
Static initializer for an unlocked mutex.
◆ pbl_mutex_assert_held()
| void pbl_mutex_assert_held |
( |
const struct pbl_mutex * |
m, |
|
|
bool |
held |
|
) |
| |
Assert that ownership of a mutex by the calling thread matches held.
- Parameters
-
| m | Mutex. |
| held | Expected ownership. |
◆ pbl_mutex_deinit()
| void pbl_mutex_deinit |
( |
struct pbl_mutex * |
m | ) |
|
Release a mutex before its memory is reused.
Required for dynamically allocated mutexes. Asserts that the mutex is not held.
- Parameters
-
◆ pbl_mutex_init()
Initialize a mutex in dynamically allocated memory.
- Parameters
-
◆ pbl_mutex_is_owner()
| bool pbl_mutex_is_owner |
( |
const struct pbl_mutex * |
m | ) |
|
Check whether the calling thread owns a mutex.
- Parameters
-
- Returns
- true if the calling thread holds
m.
◆ pbl_mutex_lock()
Lock a mutex.
Succeeds at once if the calling thread already owns it.
- Parameters
-
| m | Mutex. |
| timeout | How long to wait for another owner to release it. |
- Return values
-
| 0 | Locked. |
| -EAGAIN | Timed out. |
| -EBUSY | Held by another thread and timeout is PBL_NO_WAIT. |
| -EINTR | The thread was suspended while waiting. |
References pbl_mutex_lock_lr(), and PBL_RETURN_ADDRESS.
◆ pbl_mutex_lock_lr()
Lock a mutex, recording a given lock site.
For wrappers that want their own caller recorded in pbl_mutex::lock_lr.
- Parameters
-
| m | Mutex. |
| timeout | How long to wait for another owner to release it. |
| lr | Return address to record when this is the outermost lock. |
- Return values
-
| 0 | Locked. |
| -EAGAIN | Timed out. |
| -EBUSY | Held by another thread and timeout is PBL_NO_WAIT. |
| -EINTR | The thread was suspended while waiting. |
Referenced by pbl_mutex_lock().
◆ pbl_mutex_unlock()
| void pbl_mutex_unlock |
( |
struct pbl_mutex * |
m | ) |
|
Undo one lock of a mutex.
Asserts that the calling thread owns it. The outermost unlock hands the mutex to the highest priority waiter and drops any inherited priority boost.
- Parameters
-