Expand description
Shared state for a controller that runs on more than one thread.
The crate is single-threaded by construction, and that splits it in two. A device is one
event loop, one RefCell per piece of state, and no atomics an MCU would have to pay for.
A controller on a multi-threaded runtime is none of those things: it drives many nodes
from a work-stealing pool, and a future that is !Send cannot be spawned there at all.
Shared is the seam. Without the sync-mutex feature it is a core::cell::RefCell and the
controller stays single-threaded and cheap; with it, a Mutex, and the
state becomes Send + Sync so a run future holding it can be spawned across threads.
use matter_kit::sync::Shared;
let fabrics = Shared::new(FabricTable::new());
// The same two lines compile either way.
let count = fabrics.borrow().len();
fabrics.borrow_mut().remove(index);§One rule, and it is not optional
Never hold a borrow across an await, and never take a second borrow while one is live.
With a RefCell the second of those panics at the point of the mistake; with a Mutex it
deadlocks, silently, somewhere else. That asymmetry is why this type exists rather than a
bare cfg on each declaration: a crate that can be built both ways is one where the
single-threaded build finds the bug and the multi-threaded build would only have hung.
The crate’s own device clusters deliberately keep their core::cell::RefCells: a device is
single-threaded by construction and converting them would trade a panic that points at the
mistake for a deadlock that does not.
Structs§
- Shared
sync-mutex - State shared between the parts of a controller, across threads.