Expand description
Sleeping locks need at least a word-wide atomic read-modify-write: the id allocator
and every lock’s owner word are atomics. Targets without one (Cortex-M0, AVR,
riscv32imc) simply do not get this layer — a mutex whose owner field cannot be
updated atomically is not a mutex.
Sleeping locks, static lock-order enforcement, and timeout/back-off recovery.
The scheduler is pure round robin with no priorities, so the classic answers are unavailable: there is no priority inheritance, and a task that spins on a held lock burns a slice that could have made progress. Instead:
Mutexsleeps, it does not spin. A failed acquisition marks the callerBlockedon the lock’sLockIdand asks the port for an immediate switch — O(1), so the caller leaves the CPU at once.Mutex::unlockwakes that id’s waiters.- Lock ordering is enforced, not documented. Each task records the ids of the
locks it holds; acquiring a lock whose id is not greater than the last one held is
refused with
LockError::OrderViolation. A program that creates locks in the order it takes them therefore cannot form a circular wait. Mutex::try_lock_forbounds the wait, andMutex::lock_with_backoffimplements the runtime recovery: release everything held, back off, retry.
Blocking is safe here because a task that blocks never holds a kernel lock: kernel
spinlocks (crate::smp::SpinLock) are only held across straight-line critical
sections that cannot block, and Mutex::lock is only reachable from task code.
Re-exports§
pub use crate::tcb::MAX_HELD_LOCKS;
Structs§
- LockId
- A unique, monotonically increasing lock identity.
- Mutex
- A lock whose waiters sleep rather than spin.
- Mutex
Guard - The guard returned by every successful acquisition.
- Mutex
Header - The type-erased part of a
Mutex, so a registry can release locks without knowingT(seerelease_all_held_locks).
Enums§
- Lock
Error - Why an acquisition failed.
Functions§
- backoff
- A deterministic pseudo-random back-off, in ticks.
- held_
count - How many locks the calling task currently holds.
- release_
all_ held_ locks - Release every lock the calling task holds, returning how many were released.