Skip to main content

Module sync

Module sync 

Source
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:

  • Mutex sleeps, it does not spin. A failed acquisition marks the caller Blocked on the lock’s LockId and asks the port for an immediate switch — O(1), so the caller leaves the CPU at once. Mutex::unlock wakes 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_for bounds the wait, and Mutex::lock_with_backoff implements 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.
MutexGuard
The guard returned by every successful acquisition.
MutexHeader
The type-erased part of a Mutex, so a registry can release locks without knowing T (see release_all_held_locks).

Enums§

LockError
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.