embassy_supervisor/lib.rs
1// `no_std` for the shipped crate and the embedded build; under `cargo test` the
2// crate is built for the host, where the test harness and the unit tests need `std`.
3#![cfg_attr(not(test), no_std)]
4#![forbid(unsafe_code)]
5#![deny(missing_docs)]
6//! # embassy-supervisor — a task-lifecycle supervisor for [embassy](https://embassy.dev)
7//!
8//! Application- and HAL-agnostic primitives for orchestrating a set of embassy
9//! tasks: bringing them up in dependency order, tearing them down in reverse,
10//! scaling an elastic worker pool with load, placing nodes on interrupt-priority
11//! tiers or a second core, and starting/stopping/pausing/resuming individual
12//! tasks at runtime while keeping the dependency graph consistent. The supervisor
13//! orchestrates task *lifecycle* and leaves the rest — allocation, HAL, power,
14//! what the tasks do — to the application.
15//!
16//! ## The model
17//!
18//! * The graph is declared once with the [`supervisor_graph!`] macro: each
19//! managed task becomes a [`TaskNode`] `static`, and the macro bundles the node
20//! slots, dependency table, and a topological order computed **at compile time**
21//! into a single [`Graph`] (`GRAPH`). The whole graph is validated at compile
22//! time — a dependency cycle, an unknown or duplicate dependency, a duplicate
23//! name, or bad pool bounds are compile errors.
24//! * [`Supervisor::new`] takes `&GRAPH` (no work, no failure) and uses the order
25//! to bring tasks up in dependency order ([`Supervisor::start`]) and tear them
26//! down in reverse ([`Supervisor::teardown`]).
27//! * `executor NAME;` items declare runtime-filled [`SpawnerSlot`]s, and
28//! `executor: NAME` on a node (or a whole pool) routes its spawn through one —
29//! an interrupt-priority tier or the second core. Bring-up *awaits* the slot
30//! (bounded), so an executor that comes up late — or on another core — is a
31//! rendezvous, not a race.
32//! * `resources: [NAME: Type, ..]` on a `task:` node threads **owned resources
33//! from `main`** into the worker through macro-emitted [`ResourceSlot`]s —
34//! compile-time exclusive ownership (the `Peripherals` field is consumed, no
35//! `steal()` inside the task), fail-closed provisioning (an unprovided slot
36//! fails `start` with `SpawnError::Busy`), and restore-on-exit so a respawn
37//! re-takes the *same instance*.
38//! * Two flags span every lifecycle operation: **disabled** (stopped until an
39//! explicit `Activate` — declared `disabled` in the graph or control-stopped;
40//! see [`TaskNode::set_disabled`]) and **detached** (self-managed: after
41//! [`TaskNode::set_detached`] no supervisor operation touches the node).
42//! * Each node carries a `TaskHandle` of per-node atomic flags and
43//! single-consumer `Signal`s. Every node is single-instance — no counts, no
44//! fan-out. See [`TaskHandle`].
45//!
46//! ## Three lifecycles, distinguished by [`Mode`]
47//!
48//! * [`Mode::Terminate`] — the task exits its loop on shutdown and is respawned
49//! on the next bring-up. Stateless services (a network listener, a logger).
50//! * [`Mode::Pause`] — the task acks the shutdown then parks on
51//! `wait_resume()`; it is resumed in place, never respawned. Tasks that
52//! retain a resource across the pause (an open peripheral handle, a socket).
53//! * [`Mode::OnDemand`] — like `Terminate`, but not started at boot and not
54//! auto-respawned; the supervisor brings it up and down at runtime to scale
55//! an elastic worker pool ([`ElasticPool`]) with load.
56//!
57//! ## Writing a supervised task
58//!
59//! A supervised task is a plain `#[embassy_executor::task]` whose first parameter
60//! is its node (the macro's `spawn:` glue passes it; extra arguments come from the
61//! partial-call spawn form). Four rules cover the task side of the protocol:
62//!
63//! 1. select long-lived work against [`TaskNode::wait_shutdown`] — that's how a
64//! stop reaches you;
65//! 2. ack exactly once per stop with [`TaskNode::ack_dropped`]: on exit
66//! (`Terminate`/`OnDemand`), or on each pause (`Pause`) *before* parking on
67//! [`TaskNode::wait_resume`];
68//! 3. an autonomous exit acks too, so the supervisor sees the node as down;
69//! 4. resources follow the mode: a `Terminate` task re-acquires everything on
70//! respawn (drop-on-exit is the cleanup), a `Pause` task keeps what it holds
71//! across the park.
72//!
73//! Pool workers additionally report load with [`TaskNode::mark_busy`] /
74//! [`TaskNode::mark_idle`] (a real transition fires the scale signal itself), and
75//! a self-managed daemon or run-once job opts out of supervision with
76//! [`TaskNode::set_detached`]. The README's *Writing supervised tasks* section has
77//! per-mode skeletons.
78//!
79//! ## What the supervisor does *not* do
80//!
81//! * It does not model any power-state transition (sleep/wake): it reacts to
82//! "teardown" and "bring-up" requests; the application drives them.
83//! * It does not allocate, and does no work at construction: the topological
84//! sort runs at compile time (see the `supervisor_graph!` macro).
85//! * It does not observe task internals. Tasks self-report their drop state via
86//! `ack_dropped()`; a task that fails to ack within a timeout panics the
87//! supervisor with the offending node's name.
88//!
89//! ## Cargo features
90//!
91//! * `control` *(default)* — the runtime control plane: [`ControlOp`],
92//! [`request_control`], [`Supervisor::apply_control`].
93//! * `pool` *(default)* — elastic worker pools: [`ElasticPool`],
94//! [`Supervisor::run_pools`], and the `pools` field of [`Graph`].
95//! * `defmt` — route the supervisor's logs through `defmt`; without it the log
96//! macros are no-ops.
97//! * `trace` family (all opt-in) — `trace`: the [`trace`] recorders consuming
98//! embassy-executor's `_embassy_trace_*` hooks; `trace-hooks`:
99//! `supervisor_graph!` also *defines* the hook symbols; `trace-names`: node
100//! names stamped into task Metadata for external consumers; `trace-nested`:
101//! preemption-exact accounting (a nested higher-tier poll credits its time
102//! back to the window it interrupted).
103//!
104//! Build with `default-features = false` for a minimal core that only does
105//! dependency-ordered bring-up/teardown (drops the control plane and pools,
106//! trimming flash and a couple of statics).
107//!
108//! ## Example
109//!
110//! [`supervisor_graph!`] declares the whole graph once — it generates the node
111//! `static`s and a single [`Graph`] value `GRAPH` bundling the node slots, dep
112//! table, and compile-time topological order (a dependency cycle is a compile
113//! error), which [`Supervisor::new`] consumes.
114//!
115//! ```ignore
116//! use embassy_executor::Spawner;
117//! use embassy_supervisor::{supervisor_graph, Supervisor, wait_control};
118//!
119//! // `app` depends on `net`; each `spawn:` names a task fn spawned with the node.
120//! supervisor_graph! {
121//! node NET = Terminate, deps: [], spawn: net_task;
122//! node APP = Terminate, deps: [NET], spawn: app_task;
123//! }
124//!
125//! #[embassy_executor::task]
126//! async fn supervisor_task(spawner: Spawner) {
127//! let sup = Supervisor::new(&GRAPH);
128//! sup.start(spawner).await.expect("initial spawn"); // brings up `net`, then `app`
129//! loop {
130//! // Apply runtime start/stop/pause/resume requests in dependency order.
131//! let cmd = wait_control().await;
132//! sup.apply_control(cmd, spawner).await;
133//! }
134//! // With the `pool` feature you'd instead drive scaling and control together:
135//! // `select(sup.run_pools(spawner), wait_control())` (see the `firmware` crate).
136//! }
137//! ```
138//!
139//! The `firmware` crate in the [repository](https://github.com/cedrivard/embassy-supervisor)
140//! is a complete working example (USB-net, an HTTP control plane, an elastic pool,
141//! and OTA).
142
143#[macro_use]
144mod fmt;
145
146use core::cell::Cell;
147use core::sync::atomic::Ordering;
148
149use embassy_executor::{SendSpawner, SpawnError, Spawner};
150use embassy_futures::select::{Either, select};
151use embassy_sync::blocking_mutex::Mutex as BlockingMutex;
152use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
153#[cfg(feature = "control")]
154use embassy_sync::channel::Channel;
155use embassy_sync::signal::Signal;
156use embassy_time::{Timer, with_timeout};
157use portable_atomic::AtomicBool;
158#[cfg(feature = "trace")]
159use portable_atomic::AtomicU32;
160
161// ─── Scale-request signal (task → supervisor) ──────────────────────────────
162//
163// Elastic pool workers fire this when their busy/idle status changes; the
164// supervisor's `run_pools` loop awaits it and re-runs the pool policies
165// (`ElasticPool`). Single-consumer `Signal`: many tasks may `signal()`, only the
166// supervisor `wait()`s. This is the *only* path by which task status reaches the
167// supervisor — it never polls.
168#[cfg(feature = "pool")]
169static SCALE_REQ: Signal<CriticalSectionRawMutex, ()> = Signal::new();
170
171/// Fire the scale-request signal. Called by a task on a busy/idle transition.
172/// A no-op when the `pool` feature is disabled (no pools to re-evaluate).
173pub fn request_scale() {
174 #[cfg(feature = "pool")]
175 SCALE_REQ.signal(());
176}
177
178/// Await the next scale request. The supervisor's driver loop selects this
179/// against its other wake sources and runs the scaling policy on each wake.
180#[cfg(feature = "pool")]
181pub async fn wait_scale() {
182 SCALE_REQ.wait().await;
183}
184
185// ─── Runtime control commands (app → supervisor) ───────────────────────────
186//
187// An application's control surface (e.g. a network endpoint) usually can't drive
188// the supervisor directly: the `Supervisor` and the `Spawner` live on the
189// supervisor task's stack, not in a `static`. So control is decoupled via this
190// channel — the caller `request_control()`s a (node, op) pair and returns
191// immediately; the supervisor's driver loop `wait_control()`s it and runs the
192// dependency-honoring `apply_control`. A `Channel` (not a `Signal`) so
193// back-to-back requests aren't coalesced; capacity 4 is ample for hand-driven
194// control and a full channel simply drops the surplus (`try_send`).
195
196/// Which way to drive a node. Higher-level verbs fold onto these two:
197/// `start`/`resume` → `Activate`, `stop`/`pause` → `Deactivate`. The concrete
198/// mechanism (respawn vs resume vs leave-to-pool) is then chosen per node `Mode`
199/// by the supervisor when it applies the command ([`Supervisor::apply_control`]).
200#[cfg(feature = "control")]
201#[derive(Clone, Copy, PartialEq, Eq, Debug)]
202pub enum ControlOp {
203 /// Bring the node up (start a stopped `Terminate` node, resume a `Pause` node).
204 Activate,
205 /// Take the node down (and its dependents, per the graph).
206 Deactivate,
207}
208
209/// A runtime control request: drive `node` (and, per the dependency graph and
210/// pool membership, the nodes it implies) in the `op` direction.
211#[cfg(feature = "control")]
212#[derive(Clone, Copy, Debug)]
213pub struct ControlCommand {
214 /// The node to drive.
215 pub node: &'static TaskNode,
216 /// The direction to drive it.
217 pub op: ControlOp,
218}
219
220/// App → supervisor control mailbox. `&'static TaskNode` is `Copy + Sync`, so
221/// the target rides the channel directly — no name lookup needed supervisor-side.
222#[cfg(feature = "control")]
223static CONTROL_REQ: Channel<CriticalSectionRawMutex, ControlCommand, 4> = Channel::new();
224
225/// Enqueue a control request. Non-blocking; drops if the mailbox is full (4
226/// outstanding), which is harmless for low-frequency manual control. Called by
227/// the application's control surface.
228#[cfg(feature = "control")]
229pub fn request_control(node: &'static TaskNode, op: ControlOp) {
230 let _ = CONTROL_REQ.try_send(ControlCommand { node, op });
231}
232
233/// Await the next control request. Selected by the supervisor's driver loop
234/// against pool scaling and any other application wake sources.
235#[cfg(feature = "control")]
236pub async fn wait_control() -> ControlCommand {
237 CONTROL_REQ.receive().await
238}
239
240/// Per-node timeout for `wait_dropped`. A task that doesn't ack within this
241/// window is a bug (e.g. a missing `ack_dropped()` call) and panics the
242/// supervisor with the offending node's name. 2 s comfortably exceeds a typical
243/// task's poll period and peripheral settle time.
244const SHUTDOWN_ACK_TIMEOUT_MS: u64 = 2_000;
245
246/// How long the supervisor's bring-up waits for a node's `executor:`
247/// [`SpawnerSlot`] to be filled before failing the spawn with
248/// [`SpawnError::Busy`]. A genuine cross-core rendezvous resolves in microseconds;
249/// a slot empty this long is a misconfiguration (the app never registered that
250/// executor's spawner). Bounded, so a misconfigured graph fails loudly instead of
251/// hanging bring-up forever.
252const SLOT_READY_TIMEOUT: embassy_time::Duration = embassy_time::Duration::from_millis(100);
253
254// ─── Mode ────────────────────────────────────────────────────────────────
255
256/// Lifecycle policy for a managed task: what the task does on shutdown and what
257/// the supervisor does to bring it back.
258#[derive(Clone, Copy, PartialEq, Eq, Debug)]
259pub enum Mode {
260 /// Task exits its loop on shutdown. The supervisor respawns it via the
261 /// node's `spawn` fn from `respawn_terminate`.
262 Terminate,
263 /// Task acks shutdown and parks on `wait_resume()`. The supervisor resumes
264 /// it from `resume_pausable`; the task is never respawned, so it keeps any
265 /// resource it holds (a peripheral handle, a socket) across the pause.
266 Pause,
267 /// Like `Terminate` (exits on shutdown), but **not** started at boot and
268 /// **not** auto-respawned. The supervisor brings it up and down at runtime
269 /// via `start_node` / `stop_node` in response to load — see [`ElasticPool`].
270 /// `start()` skips it; `respawn_terminate()` leaves it down (it
271 /// re-grows under demand); `teardown()` only acts on it while it is running.
272 OnDemand,
273}
274
275impl Mode {
276 /// Stable lower-case wire name, used both for serialization (e.g. a JSON
277 /// task-state view) and for `defmt` logging — the single source of these
278 /// strings.
279 pub fn as_str(&self) -> &'static str {
280 match self {
281 Mode::Terminate => "terminate",
282 Mode::Pause => "pause",
283 Mode::OnDemand => "ondemand",
284 }
285 }
286}
287
288#[cfg(feature = "defmt")]
289impl defmt::Format for Mode {
290 fn format(&self, f: defmt::Formatter) {
291 defmt::write!(f, "{}", self.as_str());
292 }
293}
294
295// ─── TaskHandle ──────────────────────────────────────────────────────────
296
297/// Coordination state for one task. Embedded inside [`TaskNode`].
298///
299/// Every node is single-instance, so each field is a per-node atomic flag or a
300/// single-consumer signal — no counts, no fan-out. Written by one side (task or
301/// supervisor) and read by the other:
302/// * `shutdown` / `shutdown_wake` — supervisor requests exit; the task parks
303/// on the signal and reads the flag.
304/// * `dropped` / `dropped_wake` — the task acks its exit; the supervisor
305/// parks on the signal (with a timeout) and reads the flag.
306/// * `resume_wake` — supervisor resumes a parked Pause-mode task.
307/// * `running` — supervisor's record that the node is spawned; `busy` — the
308/// task's active/idle status. Both read by the elastic scaling policy.
309/// * `disabled` — the node has been manually deactivated; see below.
310pub struct TaskHandle {
311 /// Set true by the supervisor when shutdown is requested.
312 /// Cleared by `reset()` before the next spawn.
313 shutdown: AtomicBool,
314 /// Wake source for `wait_shutdown()`. Fired by `signal_shutdown()`.
315 shutdown_wake: Signal<CriticalSectionRawMutex, ()>,
316 /// Set true by the instance when it acks the shutdown (a bool, not a count,
317 /// since every node is single-instance). Cleared by `reset()`.
318 dropped: AtomicBool,
319 /// Wake source for `wait_dropped()`. Fired by `ack_dropped()`.
320 dropped_wake: Signal<CriticalSectionRawMutex, ()>,
321 /// True while the supervisor has the node spawned and it hasn't exited.
322 /// Always-on nodes are set true by `start()`; `OnDemand` nodes are set
323 /// true/false by `start_node()` / `stop_node()`. `teardown()` only acts on
324 /// `running` nodes, so a down `OnDemand` node doesn't stall it.
325 running: AtomicBool,
326 /// True while the task is actively serving (its active/idle status). Set by
327 /// `mark_busy()` / `mark_idle()`; read by the scaling policy.
328 busy: AtomicBool,
329 /// Wake source for `wait_resume()` on Pause-mode tasks. Fired by
330 /// `signal_resume()`.
331 resume_wake: Signal<CriticalSectionRawMutex, ()>,
332 /// True while the node has been manually deactivated (stopped/paused) via the
333 /// runtime control interface (`Supervisor::deactivate`). Unlike the other
334 /// flags this one is **lifecycle-spanning**: it is *not* cleared by
335 /// `reset()`, so a manual stop "sticks" — the automatic bring-up paths
336 /// (`start`, `respawn_terminate`, `resume_pausable`, and the elastic pool's
337 /// grow) skip a node while it is set. Cleared only by `Supervisor::activate`.
338 /// Because it lives in a `static`, it also survives a power-state transition
339 /// that retains RAM (e.g. a warm-resume from deep sleep).
340 disabled: AtomicBool,
341 /// Self-managed: while set, the supervisor never drives this node — teardown,
342 /// deactivate/activate, `stop_node`, respawn, and pause-resume all skip it. Not
343 /// cleared by `reset()`. Full rationale on [`TaskNode::set_detached`].
344 detached: AtomicBool,
345 /// The executor task id currently running this node (`TaskRef::id()`, captured
346 /// from the `SpawnToken` by the macro's spawn glue). `0` = unknown (not yet
347 /// spawned, or a parked/closure-spawned node that never registered). Overwritten
348 /// on every (re)spawn, so — unlike an external tracker — it stays correct across
349 /// respawns without any unlinking.
350 #[cfg(feature = "trace")]
351 task_id: AtomicU32,
352 /// Accumulated executor-poll time for this node, in embassy-time ticks,
353 /// wrapping. Consumers sample twice and `wrapping_sub` to get a rate; the
354 /// crate does no windowing.
355 #[cfg(feature = "trace")]
356 exec_ticks: AtomicU32,
357 /// Number of executor polls of this node, wrapping.
358 #[cfg(feature = "trace")]
359 polls: AtomicU32,
360 /// Longest single poll ever observed, in ticks — the "never yields" watermark.
361 /// A large value names the node that hogged the executor even after the fact,
362 /// which a live check cannot do from the blocked executor itself.
363 #[cfg(feature = "trace")]
364 max_poll_ticks: AtomicU32,
365}
366
367impl TaskHandle {
368 const fn new(disabled_at_boot: bool) -> Self {
369 Self {
370 shutdown: AtomicBool::new(false),
371 shutdown_wake: Signal::new(),
372 dropped: AtomicBool::new(false),
373 dropped_wake: Signal::new(),
374 running: AtomicBool::new(false),
375 busy: AtomicBool::new(false),
376 resume_wake: Signal::new(),
377 disabled: AtomicBool::new(disabled_at_boot),
378 detached: AtomicBool::new(false),
379 #[cfg(feature = "trace")]
380 task_id: AtomicU32::new(0),
381 #[cfg(feature = "trace")]
382 exec_ticks: AtomicU32::new(0),
383 #[cfg(feature = "trace")]
384 polls: AtomicU32::new(0),
385 #[cfg(feature = "trace")]
386 max_poll_ticks: AtomicU32::new(0),
387 }
388 }
389}
390
391// ─── Executor spawner slots ──────────────────────────────────────────────
392
393/// A runtime-filled slot holding the [`SendSpawner`] of an executor other than
394/// the one the supervisor runs on — an `InterruptExecutor` tier, the second
395/// core's executor, any foreign thread executor (via `Spawner::make_send()`).
396///
397/// Declared by the `executor NAME;` item of [`supervisor_graph!`]; nodes carrying
398/// `executor: NAME` are spawned through the slot instead of the supervisor's own
399/// `Spawner`. The application fills it once at startup — before, or concurrently
400/// with, [`Supervisor::start`] (e.g. from the second core's bring-up):
401///
402/// ```ignore
403/// static EXECUTOR_HIGH: InterruptExecutor = InterruptExecutor::new();
404/// HIGH.set(EXECUTOR_HIGH.start(interrupt::SWI_IRQ_0));
405/// sup.start(spawner).await?; // nodes declared `executor: HIGH` spawn on that tier
406/// ```
407///
408/// The supervisor's bring-up (`start` / `start_node` / `respawn_terminate`) awaits
409/// [`ready`](Self::ready) for a node's slot before spawning it, so a tier filled
410/// late — or from another core — is handled without a race; a slot still empty after
411/// the supervisor's bounded wait fails the spawn with [`SpawnError::Busy`] rather
412/// than silently dropping the task. Spawned futures must be `Send` (a non-`Send`
413/// `executor:` task is a compile error at the glue).
414pub struct SpawnerSlot {
415 slot: BlockingMutex<CriticalSectionRawMutex, Cell<Option<SendSpawner>>>,
416 /// Wakes a `ready()` waiter when `set` fills the slot (cross-core safe:
417 /// `Signal` is critical-section based and latches).
418 filled: Signal<CriticalSectionRawMutex, ()>,
419}
420
421impl SpawnerSlot {
422 /// An empty slot (`const` — it lives in a `static` the macro emits).
423 pub const fn new() -> Self {
424 Self {
425 slot: BlockingMutex::new(Cell::new(None)),
426 filled: Signal::new(),
427 }
428 }
429
430 /// Fill the slot (last set wins) and wake a [`ready`](Self::ready) waiter.
431 /// Call before [`Supervisor::start`] — or from the other core's bring-up,
432 /// with the supervisor awaiting `ready()`.
433 pub fn set(&self, spawner: SendSpawner) {
434 self.slot.lock(|c| c.set(Some(spawner)));
435 self.filled.signal(());
436 }
437
438 /// The registered spawner, or `None` while unfilled.
439 pub fn get(&self) -> Option<SendSpawner> {
440 self.slot.lock(Cell::get)
441 }
442
443 /// Await the slot and return the spawner. The rendezvous primitive: the
444 /// supervisor's bring-up awaits this for a node's `executor:` slot before
445 /// spawning it (bounded, see [`Supervisor::start`]), so a tier filled late — or
446 /// from another core — is handled without a race. Returns immediately once the
447 /// slot is filled, so any number of *late* callers are fine (an application can
448 /// gate work on the executor being up). While the slot is still empty, at most
449 /// one task should be parked here: the underlying `Signal` holds a single waker,
450 /// so a second pre-fill waiter would displace the first.
451 pub async fn ready(&self) -> SendSpawner {
452 loop {
453 if let Some(sp) = self.get() {
454 return sp;
455 }
456 // `Signal` latches: a `set()` racing between the check above and
457 // this wait still wakes us.
458 self.filled.wait().await;
459 }
460 }
461}
462
463impl Default for SpawnerSlot {
464 fn default() -> Self {
465 Self::new()
466 }
467}
468
469// ─── ResourceSlot ────────────────────────────────────────────────────────
470
471/// Type-erased readiness view of a [`ResourceSlot`], for the supervisor's
472/// bring-up wait.
473///
474/// A `TaskNode` can gate on any number of slots of *different* `T`s, so the node
475/// stores `&'static [&'static dyn ResourceGate]` (object-safe: no `T` in the
476/// signatures). Same shape as embassy's `dyn` driver registries — see
477/// <https://doc.rust-lang.org/reference/items/traits.html#object-safety>.
478/// The supervisor only needs "is it filled?" plus the signal to park on; taking
479/// the value stays in the generated spawn glue, where the concrete `T` is known.
480pub trait ResourceGate: Sync {
481 /// Non-consuming "is the slot currently filled" check.
482 fn is_filled(&self) -> bool;
483 /// The latching [`Signal`] fired by `provide`/`restore`, for the supervisor's
484 /// bounded pre-spawn wait (see [`Supervisor::start`]).
485 fn filled_signal(&self) -> &Signal<CriticalSectionRawMutex, ()>;
486}
487
488/// A one-value handoff cell threading an owned resource from `main` into a
489/// supervised task — the safe replacement for `Peripherals::steal()` inside
490/// the task body.
491///
492/// Declared (as a `pub static`) by [`supervisor_graph!`] for each entry in a
493/// node's `resources:` clause. The protocol:
494///
495/// 1. `main` splits `Peripherals` and **moves** the resource in with
496/// [`provide`](Self::provide). This is where the compile-time guarantee
497/// lives: the singleton field is *consumed*, so no second owner — and no
498/// `unsafe` steal — can exist.
499/// 2. The generated spawn glue [`take`](Self::take)s it just before spawning
500/// the node. An empty slot fails the spawn with `SpawnError::Busy` — a
501/// fail-closed error out of [`Supervisor::start`], not a panic inside the
502/// task (compare `static_cell::StaticCell`, which panics on misuse).
503/// 3. The generated task shell hands the worker `&mut T` and
504/// [`restore`](Self::restore)s the value after the worker returns, so a
505/// `Terminate` respawn re-takes the *same instance* instead of stealing a
506/// fresh one. (A `Pause` worker never returns — it parks — so it simply
507/// retains the resource, exactly like a hand-written parked task.)
508///
509/// Same primitives as [`SpawnerSlot`]: a critical-section
510/// [`BlockingMutex`]`<`[`Cell`]`<Option<T>>>` for the value (`Sync` for
511/// `T: Send`, provided by embassy-sync — no `unsafe` here) plus a latching
512/// [`Signal`] so the supervisor can await late provisioning (bounded; see
513/// [`Supervisor::start`]).
514pub struct ResourceSlot<T> {
515 slot: BlockingMutex<CriticalSectionRawMutex, Cell<Option<T>>>,
516 /// Wakes the supervisor's pre-spawn wait when `provide`/`restore` fills the
517 /// slot (latching, so a fill racing the check-then-wait still wakes it).
518 filled: Signal<CriticalSectionRawMutex, ()>,
519}
520
521impl<T> ResourceSlot<T> {
522 /// An empty slot (`const` — it lives in a `static` the macro emits).
523 pub const fn new() -> Self {
524 Self {
525 slot: BlockingMutex::new(Cell::new(None)),
526 filled: Signal::new(),
527 }
528 }
529
530 /// Move the resource in (from `main`'s `Peripherals` split) and wake the
531 /// supervisor's pre-spawn wait. Call before [`Supervisor::start`]; a slot
532 /// still empty after the supervisor's bounded wait fails that node's spawn
533 /// with `SpawnError::Busy`. Filling an occupied slot replaces (drops) the
534 /// old value — don't: one resource, one slot, moved exactly once.
535 pub fn provide(&self, value: T) {
536 self.slot.lock(|c| c.set(Some(value)));
537 self.filled.signal(());
538 }
539
540 /// Take the resource out, leaving the slot empty. Called by the generated
541 /// spawn glue just before the spawn; `None` means "not provided yet" or
542 /// "currently held by a live task instance".
543 pub fn take(&self) -> Option<T> {
544 self.slot.lock(Cell::take)
545 }
546
547 /// Put the resource back for the next spawn. Called by the generated task
548 /// shell after the worker returns (i.e. after its clean shutdown ack), so a
549 /// respawn re-takes the same instance.
550 pub fn restore(&self, value: T) {
551 self.provide(value);
552 }
553}
554
555// `T: Send` (not just any `T`): the gate is reachable from the supervisor task,
556// which may run on a different core than the provider — the same bound the
557// inner `BlockingMutex` requires for `Sync`, restated here so the `dyn` upcast
558// can't outrun it.
559impl<T: Send> ResourceGate for ResourceSlot<T> {
560 fn is_filled(&self) -> bool {
561 // Peek without consuming: `Cell` has no `&T` access (no `T: Copy`
562 // here), so take-and-put-back under the same critical section.
563 self.slot.lock(|c| {
564 let v = c.take();
565 let filled = v.is_some();
566 c.set(v);
567 filled
568 })
569 }
570
571 fn filled_signal(&self) -> &Signal<CriticalSectionRawMutex, ()> {
572 &self.filled
573 }
574}
575
576impl<T> Default for ResourceSlot<T> {
577 fn default() -> Self {
578 Self::new()
579 }
580}
581
582// ─── TaskNode ────────────────────────────────────────────────────────────
583
584/// A node in the supervisor's task graph.
585///
586/// Designed to live in `static` memory: every field is `Sync`, all constructors
587/// are `const`. Declared by [`supervisor_graph!`], which emits one per managed
588/// task along with the [`Graph`] (`GRAPH`) that [`Supervisor::new`] consumes.
589pub struct TaskNode {
590 /// Human-readable name. Used in defmt logs and panic messages.
591 pub name: &'static str,
592 /// Lifecycle policy. See [`Mode`].
593 pub mode: Mode,
594 /// App-provided spawn function (typically an inline closure at the node's
595 /// declaration). Called once at boot from `Supervisor::start`, again from
596 /// `respawn_terminate` for Terminate nodes, and at runtime from `start_node`
597 /// for `OnDemand` nodes. `None` for a **parked** node the application spawns
598 /// itself (e.g. a `Pause` sensor holding a peripheral handle): the supervisor
599 /// tracks its lifecycle but never spawns it.
600 pub spawn: Option<fn(Spawner) -> Result<(), SpawnError>>,
601 /// The executor [`SpawnerSlot`] this node spawns through (`executor: NAME` in
602 /// the graph), or `None` to spawn on the supervisor's own `Spawner`. When
603 /// `Some`, the supervisor awaits the slot's [`ready`](SpawnerSlot::ready)
604 /// (bounded by [`SLOT_READY_TIMEOUT`]) *before* invoking `spawn`, so the
605 /// generated glue's own non-blocking `SpawnerSlot::get` is already filled. Set
606 /// by the macro via [`with_executor`](Self::with_executor); `const`, zero-cost.
607 spawn_slot: Option<&'static SpawnerSlot>,
608 /// The [`ResourceSlot`]s this node's spawn takes from (`resources:` in the
609 /// graph), type-erased to their [`ResourceGate`] readiness view. The
610 /// supervisor awaits every gate being filled (bounded by
611 /// [`SLOT_READY_TIMEOUT`]) *before* invoking `spawn`, so (a) a `main` that
612 /// provides late is tolerated and (b) a respawn cannot race the previous
613 /// instance's shell restoring the value (the restore happens after the
614 /// worker's shutdown ack). Empty for nodes without `resources:`. Set by the
615 /// macro via [`with_resources`](Self::with_resources); `const`, zero-cost.
616 resource_gates: &'static [&'static dyn ResourceGate],
617 handle: TaskHandle,
618}
619
620impl TaskNode {
621 /// A single-instance node started at boot (`Terminate`/`Pause`) or on demand
622 /// (`Mode::OnDemand`). Every node is single-instance; an elastic service is
623 /// modelled as several `OnDemand` nodes of the same pooled task fn.
624 ///
625 /// A `TaskNode` carries only its own identity and behaviour; the graph's
626 /// dependency edges live in the compile-time index table that
627 /// [`supervisor_graph!`] emits and [`Supervisor::new`] consumes.
628 /// `disabled_at_boot` seeds the node's disabled flag so a control-started node
629 /// (e.g. an OTA task) can be declared down and started later via a control op.
630 /// `spawn` is `None` for a parked node the application spawns itself.
631 pub const fn new(
632 name: &'static str,
633 mode: Mode,
634 spawn: Option<fn(Spawner) -> Result<(), SpawnError>>,
635 disabled_at_boot: bool,
636 ) -> Self {
637 Self {
638 name,
639 mode,
640 spawn,
641 spawn_slot: None,
642 resource_gates: &[],
643 handle: TaskHandle::new(disabled_at_boot),
644 }
645 }
646
647 /// Route this node's spawn through the given executor [`SpawnerSlot`] (the
648 /// `executor: NAME` graph annotation). The supervisor awaits the slot before
649 /// spawning the node, so a tier filled late — or from another core — is handled
650 /// without a race, and the generated glue's non-blocking `get` is already filled.
651 /// `const` and chainable in a `static` initializer; emitted by [`supervisor_graph!`].
652 pub const fn with_executor(mut self, slot: &'static SpawnerSlot) -> Self {
653 self.spawn_slot = Some(slot);
654 self
655 }
656
657 /// Declare the [`ResourceSlot`]s this node's spawn takes from (the
658 /// `resources:` graph clause). The supervisor awaits every gate being
659 /// filled before spawning the node, so the generated glue's non-blocking
660 /// `take()` finds the value. `const` and chainable in a `static`
661 /// initializer; emitted by [`supervisor_graph!`].
662 pub const fn with_resources(mut self, gates: &'static [&'static dyn ResourceGate]) -> Self {
663 self.resource_gates = gates;
664 self
665 }
666
667 // ── Task-side API ────────────────────────────────────────────────────
668 //
669 // Called from inside the `#[embassy_executor::task] async fn` body. The
670 // whole task-side protocol is four rules (the README's "Writing supervised
671 // tasks" section has per-mode skeletons):
672 // 1. select long-lived work against `wait_shutdown()`;
673 // 2. `ack_dropped()` exactly once per stop — on exit (Terminate/OnDemand)
674 // or on each pause (Pause), before parking on `wait_resume()`;
675 // 3. an autonomous exit acks too;
676 // 4. resources follow the mode: Terminate re-acquires on respawn, Pause
677 // retains across park.
678
679 /// True iff the supervisor has requested shutdown. Checked at the loop top
680 /// alongside `wait_shutdown()` in a `select`.
681 pub fn shutdown_requested(&self) -> bool {
682 self.handle.shutdown.load(Ordering::Acquire)
683 }
684
685 /// Park until shutdown is requested. Returns immediately if shutdown has
686 /// already been requested. Use this for single-instance tasks in a `select`
687 /// against the task's main work future.
688 pub async fn wait_shutdown(&self) {
689 // Fast path — already requested. (Important because the signal is
690 // edge-triggered: if `signal()` fired before we got here, the bare
691 // `wait()` below would block forever.)
692 if self.handle.shutdown.load(Ordering::Acquire) {
693 return;
694 }
695 self.handle.shutdown_wake.wait().await;
696 }
697
698 /// Mark this instance as having shut down: clears the running flag and acks
699 /// the teardown handshake (so the supervisor's `wait_dropped` completes).
700 /// Every instance must call this exactly once on exit (Terminate/OnDemand
701 /// mode) or on each pause (Pause mode). It also covers an **autonomous** exit
702 /// the supervisor didn't request — e.g. a pool worker backing off — so the
703 /// pool sees the instance as down and can re-grow it under later demand.
704 pub fn ack_dropped(&self) {
705 self.handle.running.store(false, Ordering::Release);
706 self.handle.dropped.store(true, Ordering::Release);
707 self.handle.dropped_wake.signal(());
708 }
709
710 /// Pause-mode only: park until the supervisor signals resume. Call *after*
711 /// [`ack_dropped`](Self::ack_dropped) — ack the pause, then park; held
712 /// resources stay owned across the park.
713 pub async fn wait_resume(&self) {
714 self.handle.resume_wake.wait().await;
715 }
716
717 /// Report that this task started serving a request (active). Fires the
718 /// scale-request signal on a real idle→busy transition so the scaling policy
719 /// can react (e.g. grow the pool); a redundant call doesn't re-signal.
720 pub fn mark_busy(&self) {
721 if !self.handle.busy.swap(true, Ordering::Release) {
722 request_scale();
723 }
724 }
725
726 /// Report that this task finished serving and is idle again. Fires the
727 /// scale-request signal on a real busy→idle transition so the scaling policy
728 /// can react (e.g. shrink the pool); a redundant call doesn't re-signal.
729 pub fn mark_idle(&self) {
730 if self.handle.busy.swap(false, Ordering::Release) {
731 request_scale();
732 }
733 }
734
735 /// True while this task is actively serving. Read by the scaling policy.
736 pub fn is_busy(&self) -> bool {
737 self.handle.busy.load(Ordering::Acquire)
738 }
739
740 /// True while the supervisor has this node spawned (and it hasn't exited).
741 /// Read by the scaling policy to count live instances, and by a task-state
742 /// view.
743 pub fn is_running(&self) -> bool {
744 self.handle.running.load(Ordering::Acquire)
745 }
746
747 /// True while the node is disabled: declared `disabled` in the graph
748 /// (stopped-at-boot, up on an explicit `Activate`), or manually deactivated
749 /// via the control interface and not yet re-activated. Read by a task-state
750 /// view and by the automatic bring-up paths (which skip a disabled node).
751 pub fn is_disabled(&self) -> bool {
752 self.handle.disabled.load(Ordering::Acquire)
753 }
754
755 /// Mark/clear this node as **detached**: a self-managing node the supervisor
756 /// brings up once (via [`start`](Supervisor::start)) and then stops managing
757 /// **entirely**. Every runtime lifecycle operation skips a detached node: full
758 /// [`teardown`](Supervisor::teardown), the control deactivate/activate cascades,
759 /// [`stop_node`](Supervisor::stop_node), [`respawn_terminate`](Supervisor::respawn_terminate),
760 /// and pause-resume. It keeps running (or, for a one-shot, stays exited) across a
761 /// teardown/wake cycle instead of being stopped, re-enabled, or re-spawned. Use it
762 /// for a task that must outlive the teardown it participates in — e.g. a sleep/power
763 /// coordinator that tears the graph down, sleeps, then wakes it — or a self-managed
764 /// one-shot whose `deps:` exist only for start-ordering. The node owns its own
765 /// shutdown; the supervisor will not drive it.
766 pub fn set_detached(&self, detached: bool) {
767 self.handle.detached.store(detached, Ordering::Release);
768 }
769
770 /// True while this node is [detached](Self::set_detached): self-managed, skipped by
771 /// every runtime lifecycle operation (teardown, deactivate/activate, `stop_node`,
772 /// respawn, pause-resume). Only the initial `start` brings it up.
773 pub fn is_detached(&self) -> bool {
774 self.handle.detached.load(Ordering::Acquire)
775 }
776
777 // ── Trace/observability API (features `trace`/`trace-names`) ───────────
778
779 /// Record the executor task id (`SpawnToken::id()` / `TaskRef::id()`) currently
780 /// backing this node, so the [`trace`] recorders can attribute executor polls to
781 /// it. Called automatically by the spawn glue `supervisor_graph!` generates;
782 /// call it manually only for a **parked** node (no `spawn:`) or a verbatim-closure
783 /// `spawn:`, where the macro cannot see the token. Overwrites on every (re)spawn.
784 #[cfg(feature = "trace")]
785 pub fn set_task_id(&self, id: u32) {
786 self.handle.task_id.store(id, Ordering::Release);
787 }
788
789 /// Register an externally-spawned token as this node's live task: records
790 /// the task id for the [`trace`] recorders and (feature `trace-names`)
791 /// stamps the node name into the task Metadata. One call replaces the
792 /// manual [`set_task_id`](Self::set_task_id) dance wherever the macro can't
793 /// see the token — parked nodes and verbatim-closure `spawn:` forms:
794 ///
795 /// ```ignore
796 /// let t = environment_task(i2c_dev)?;
797 /// BME280.adopt(&t);
798 /// high_spawner.spawn(t);
799 /// ```
800 #[cfg(feature = "trace")]
801 pub fn adopt<S>(&self, token: &embassy_executor::SpawnToken<S>) {
802 self.set_task_id(token.id());
803 #[cfg(feature = "trace-names")]
804 token.metadata().set_name(self.name);
805 }
806
807 /// The executor task id last recorded by [`set_task_id`](Self::set_task_id)
808 /// (`0` = never spawned / not registered).
809 #[cfg(feature = "trace")]
810 pub fn task_id(&self) -> u32 {
811 self.handle.task_id.load(Ordering::Acquire)
812 }
813
814 /// Accumulated executor-poll time of this node, in embassy-time ticks. Wrapping:
815 /// sample twice and `wrapping_sub` the readings to get a rate over a window.
816 #[cfg(feature = "trace")]
817 pub fn exec_ticks(&self) -> u32 {
818 self.handle.exec_ticks.load(Ordering::Relaxed)
819 }
820
821 /// Number of executor polls of this node (wrapping counter).
822 #[cfg(feature = "trace")]
823 pub fn poll_count(&self) -> u32 {
824 self.handle.polls.load(Ordering::Relaxed)
825 }
826
827 /// Longest single executor poll of this node ever observed, in ticks — the
828 /// "never yields" watermark. A poll is expected to be microseconds; a large
829 /// value names the node that hogged its executor, even after the fact.
830 #[cfg(feature = "trace")]
831 pub fn max_poll_ticks(&self) -> u32 {
832 self.handle.max_poll_ticks.load(Ordering::Relaxed)
833 }
834
835 // ── Supervisor-side API ──────────────────────────────────────────────
836 //
837 // Driven by the `Supervisor` struct. Kept `pub(crate)` so app code doesn't
838 // accidentally bypass the supervisor's orchestration.
839
840 pub(crate) fn signal_shutdown(&self) {
841 self.handle.shutdown.store(true, Ordering::Release);
842 self.handle.shutdown_wake.signal(());
843 }
844
845 pub(crate) fn signal_resume(&self) {
846 self.handle.resume_wake.signal(());
847 }
848
849 pub(crate) fn set_running(&self, running: bool) {
850 self.handle.running.store(running, Ordering::Release);
851 }
852
853 /// Set/clear the manual-deactivation flag. Set by `Supervisor::deactivate`,
854 /// cleared by `Supervisor::activate`. Deliberately *not* touched by
855 /// `reset()`, so a manual stop survives respawn cycles and RAM-retaining
856 /// power-state transitions.
857 ///
858 /// Public so an application can pre-disable a `Terminate` node *before*
859 /// `Supervisor::start`, making it a stopped-at-boot task that only comes up on
860 /// an explicit `Activate` control (a node started by control rather than at boot).
861 pub fn set_disabled(&self, disabled: bool) {
862 self.handle.disabled.store(disabled, Ordering::Release);
863 }
864
865 /// Wait until the instance has called `ack_dropped()`. Single-instance, so
866 /// one ack ends the wait. The fast-path flag check handles the ack landing
867 /// before this await (the `dropped_wake` signal is edge-triggered).
868 pub(crate) async fn wait_dropped(&self) {
869 if self.handle.dropped.load(Ordering::Acquire) {
870 return;
871 }
872 self.handle.dropped_wake.wait().await;
873 }
874
875 /// Clear the shutdown flag, dropped flag, busy flag, and the shutdown /
876 /// dropped wake-signals so the next cycle starts clean. Doesn't touch
877 /// `running` (managed around spawn/stop), `resume_wake` (`resume_pausable`
878 /// fires that for Pause nodes), or `disabled` (lifecycle-spanning).
879 pub(crate) fn reset(&self) {
880 self.handle.shutdown.store(false, Ordering::Release);
881 self.handle.dropped.store(false, Ordering::Release);
882 self.handle.busy.store(false, Ordering::Release);
883 self.handle.shutdown_wake.reset();
884 self.handle.dropped_wake.reset();
885 }
886}
887
888/// Manual impl: the private `TaskHandle` (Signals + atomics) has no `Debug`, and a
889/// snapshot of the *live* flags is more useful than raw handle internals anyway.
890/// `finish_non_exhaustive` marks the elided fields (`spawn`, the handle).
891impl core::fmt::Debug for TaskNode {
892 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
893 f.debug_struct("TaskNode")
894 .field("name", &self.name)
895 .field("mode", &self.mode)
896 .field("running", &self.is_running())
897 .field("busy", &self.is_busy())
898 .field("disabled", &self.is_disabled())
899 .field("detached", &self.is_detached())
900 .finish_non_exhaustive()
901 }
902}
903
904// ─── Graph ───────────────────────────────────────────────────────────────
905
906/// The compile-time task graph produced by [`supervisor_graph!`]: the node slots,
907/// the dependency-index table, the topological order, and the elastic pools — the
908/// single value [`Supervisor::new`] consumes. The macro emits one `pub static GRAPH`
909/// of this type. The fields are public so the application can read them directly
910/// (e.g. a status endpoint iterating `GRAPH.nodes` / `GRAPH.deps`).
911///
912/// `N` is capped at 256 (graph indices are `u8`); the macro enforces this at
913/// expansion time.
914pub struct Graph<const N: usize> {
915 /// Node slots, one per declared node. `None` marks a `#[cfg]`-ed-out node.
916 pub nodes: &'static [Option<&'static TaskNode>; N],
917 /// Per-node dependency indices into `nodes` (`deps[i]` lists node `i`'s deps).
918 pub deps: &'static [&'static [u8]; N],
919 /// Topologically sorted indices into `nodes` (dependencies before dependents;
920 /// reverse iteration is the teardown order). A dependency cycle is a compile error.
921 pub order: [u8; N],
922 /// Elastic worker pools to register with the supervisor (empty when unused).
923 #[cfg(feature = "pool")]
924 pub pools: &'static [&'static dyn Pool],
925}
926
927// ─── Supervisor ──────────────────────────────────────────────────────────
928
929/// Orchestrates a set of managed tasks across spawn / teardown / bring-up.
930///
931/// Owned by a single supervisor task. Concurrent access from other tasks goes
932/// through each [`TaskNode`]'s own atomic state, not the `Supervisor` struct.
933pub struct Supervisor<const N: usize> {
934 /// Node slots, one per declared node. `None` marks a slot whose node was
935 /// `#[cfg]`-ed out of the build (feature-gated); every method skips those.
936 nodes: &'static [Option<&'static TaskNode>],
937 /// Per-node dependency indices into `nodes` (`deps[i]` lists the indices of
938 /// the nodes that node `i` depends on). The single runtime source of graph
939 /// topology, generated alongside `order` by the `supervisor_graph!` macro.
940 deps: &'static [&'static [u8]],
941 /// Topologically sorted indices into `nodes`: dependencies before their
942 /// dependents; reverse iteration is the teardown order. Precomputed at
943 /// compile time (a cycle is a compile error), so construction does no work.
944 order: [u8; N],
945 /// Elastic pools, so the control interface can co-control a whole pool from
946 /// any one member (`apply_control` expands the target through
947 /// [`Pool::members`]) — the same registry `run_pools` drives. Taken from
948 /// `GRAPH.pools` at construction (empty when no pool is declared).
949 #[cfg(feature = "pool")]
950 pools: &'static [&'static dyn Pool],
951}
952
953/// Await a node's `executor:` [`SpawnerSlot`] (if it has one), bounded by
954/// [`SLOT_READY_TIMEOUT`]. A slot still empty after the wait yields
955/// [`SpawnError::Busy`] — a loud misconfiguration, not a silent hang. A node with no
956/// slot returns immediately, so a same-executor bring-up never touches the timer.
957async fn await_spawn_slot(node: &'static TaskNode) -> Result<(), SpawnError> {
958 if let Some(slot) = node.spawn_slot {
959 with_timeout(SLOT_READY_TIMEOUT, slot.ready())
960 .await
961 .map_err(|_| SpawnError::Busy)?;
962 }
963 Ok(())
964}
965
966/// Await every [`ResourceSlot`] a node's `resources:` clause takes from being
967/// filled, bounded by [`SLOT_READY_TIMEOUT`] per gate. Covers two windows:
968/// `main` providing after `start` was entered, and — on respawn — the previous
969/// instance's shell still between the shutdown ack and its `restore()` call
970/// (on another core the two can genuinely overlap). A gate still empty at the
971/// deadline yields [`SpawnError::Busy`] — an unprovided slot is a loud
972/// misconfiguration, not a silent hang. Nodes without `resources:` have an
973/// empty gate list and never touch the timer. Same check-then-park loop as
974/// [`SpawnerSlot::ready`]; the `filled` signal latches, so a fill racing the
975/// check still wakes the wait (and the same single-pre-fill-waiter caveat
976/// applies — the supervisor task is the only intended waiter).
977async fn await_resources(node: &'static TaskNode) -> Result<(), SpawnError> {
978 for gate in node.resource_gates {
979 let wait = async {
980 loop {
981 if gate.is_filled() {
982 break;
983 }
984 gate.filled_signal().wait().await;
985 }
986 };
987 with_timeout(SLOT_READY_TIMEOUT, wait)
988 .await
989 .map_err(|_| SpawnError::Busy)?;
990 }
991 Ok(())
992}
993
994impl<const N: usize> Supervisor<N> {
995 /// Build a supervisor from a precomputed [`Graph`] — the `GRAPH` that
996 /// `supervisor_graph!` emits (node slots, dependency-index table, compile-time
997 /// topological `order`, and the elastic pools). A dependency cycle is a
998 /// *compile* error, so construction is infallible and does no work —
999 /// `start` / `teardown` / `respawn_terminate` just iterate.
1000 pub const fn new(graph: &'static Graph<N>) -> Self {
1001 Self {
1002 nodes: graph.nodes,
1003 deps: graph.deps,
1004 order: graph.order,
1005 #[cfg(feature = "pool")]
1006 pools: graph.pools,
1007 }
1008 }
1009
1010 /// Spawn every boot node in dependency order. Called once at boot.
1011 /// `Mode::OnDemand` nodes are skipped — they're brought up at runtime by
1012 /// `start_node`. A **parked** node (no `spawn` fn) is spawned externally by
1013 /// `main()` (with hardware handles main owns); it's still marked `running`
1014 /// here. Disabled nodes, and `#[cfg]`-ed-out slots, are skipped.
1015 ///
1016 /// Async because an `executor: NAME` node first awaits its [`SpawnerSlot::ready`]
1017 /// (bounded by `SLOT_READY_TIMEOUT` — the rendezvous with a tier or second core
1018 /// that comes up asynchronously); a slot still empty at the deadline fails the
1019 /// bring-up with [`SpawnError::Busy`]. A node with no `executor:` slot never
1020 /// touches the timer.
1021 pub async fn start(&self, spawner: Spawner) -> Result<(), SpawnError> {
1022 // Register the node slots with the trace recorders.
1023 #[cfg(feature = "trace")]
1024 trace::register_graph(self.nodes);
1025
1026 for i in self.order.iter() {
1027 let Some(node) = self.nodes[*i as usize] else {
1028 continue;
1029 };
1030 if matches!(node.mode, Mode::OnDemand) || node.is_disabled() {
1031 continue;
1032 }
1033 info!("supervisor: spawning {} ({})", node.name, node.mode);
1034 if let Some(spawn) = node.spawn {
1035 // For an `executor:` node, wait (bounded) for its slot to be filled
1036 // before spawning; a same-executor node has no slot, so this is an
1037 // immediate no-op and the bring-up loop stays tight. Then wait for
1038 // the node's `resources:` slots (if any) so the glue's take() finds
1039 // the value even if main provides late.
1040 await_spawn_slot(node).await?;
1041 await_resources(node).await?;
1042 spawn(spawner)?;
1043 }
1044 node.set_running(true);
1045 }
1046 Ok(())
1047 }
1048
1049 /// Start a single node at runtime — e.g. growing an elastic pool. Resets the
1050 /// handle, spawns one instance via the node's `spawn` fn (which must launch
1051 /// exactly one), and marks it `running`. Returns `SpawnError::Busy` if the
1052 /// underlying embassy task pool is exhausted (the ceiling), which the caller
1053 /// treats as "can't grow".
1054 pub async fn start_node(
1055 &self,
1056 node: &'static TaskNode,
1057 spawner: Spawner,
1058 ) -> Result<(), SpawnError> {
1059 node.reset();
1060 if let Some(spawn) = node.spawn {
1061 await_spawn_slot(node).await?;
1062 await_resources(node).await?;
1063 spawn(spawner)?;
1064 }
1065 node.set_running(true);
1066 info!("supervisor: started {}", node.name);
1067 Ok(())
1068 }
1069
1070 /// Signal `node` to shut down, wait for its ack (panicking on timeout — a
1071 /// missing `ack_dropped()` somewhere), then clear `running`. Shared by
1072 /// `stop_node` and `teardown`; the caller must have checked `is_running`.
1073 async fn shutdown_and_wait(&self, node: &'static TaskNode) {
1074 node.signal_shutdown();
1075 if let Either::Second(()) = select(
1076 node.wait_dropped(),
1077 Timer::after_millis(SHUTDOWN_ACK_TIMEOUT_MS),
1078 )
1079 .await
1080 {
1081 panic!(
1082 "supervisor: task {} did not ack shutdown within {}ms",
1083 node.name, SHUTDOWN_ACK_TIMEOUT_MS,
1084 );
1085 }
1086 node.set_running(false);
1087 }
1088
1089 /// Stop a single running node at runtime — e.g. shrinking an elastic pool.
1090 /// Signals shutdown, waits for the ack, clears `running`. No-op if the node
1091 /// isn't running, or is [detached](TaskNode::set_detached) (self-managed — the
1092 /// supervisor never stops it). Panics if it doesn't ack within the timeout.
1093 pub async fn stop_node(&self, node: &'static TaskNode) {
1094 if !node.is_running() || node.is_detached() {
1095 return;
1096 }
1097 self.shutdown_and_wait(node).await;
1098 info!("supervisor: stopped {}", node.name);
1099 }
1100
1101 /// Signal every **running** node to shut down in **reverse** topological
1102 /// order, awaiting each node's ack before moving to its dependency. Down
1103 /// `OnDemand` nodes are skipped (no instance to ack). Pause-mode nodes ack
1104 /// and park on `wait_resume()`; Terminate/OnDemand nodes exit. Panics if a
1105 /// running node fails to ack within `SHUTDOWN_ACK_TIMEOUT_MS`.
1106 pub async fn teardown(&self) {
1107 for i in self.order.iter().rev() {
1108 let Some(node) = self.nodes[*i as usize] else {
1109 continue;
1110 };
1111 if !node.is_running() {
1112 continue;
1113 }
1114 // A detached node is self-managed; never tear it down. See
1115 // [`TaskNode::set_detached`].
1116 if node.is_detached() {
1117 continue;
1118 }
1119 info!("supervisor: tearing down {}", node.name);
1120 self.shutdown_and_wait(node).await;
1121 }
1122 }
1123
1124 /// Signal every Pause-mode node to resume. Cheap and synchronous — the tasks
1125 /// were parked on `wait_resume()` and pick up immediately. Called separately
1126 /// from `respawn_terminate` so the application can fire resume independently
1127 /// of the respawn step. Disabled (manually-paused) nodes are skipped so a
1128 /// manual pause sticks, and detached (self-managed) Pause nodes are left
1129 /// parked; there is intentionally no dependency gate here.
1130 pub fn resume_pausable(&self) {
1131 for i in self.order.iter() {
1132 let Some(node) = self.nodes[*i as usize] else {
1133 continue;
1134 };
1135 if matches!(node.mode, Mode::Pause) && !node.is_disabled() && !node.is_detached() {
1136 node.reset();
1137 info!("supervisor: resuming {}", node.name);
1138 node.signal_resume();
1139 node.set_running(true);
1140 }
1141 }
1142 }
1143
1144 /// Reset and re-spawn every Terminate-mode node in dependency order.
1145 /// Pause-mode nodes are untouched (use `resume_pausable`); `OnDemand` nodes
1146 /// are left down — they re-grow under load via `start_node`. Disabled nodes
1147 /// are skipped so a manual stop sticks across the bring-up. Detached nodes are
1148 /// skipped too: `teardown` never brought them down, so they are still running
1149 /// and re-spawning would double-spawn them (see [`TaskNode::set_detached`]). The
1150 /// reset happens before the spawn so newly-running tasks see a clean handle.
1151 pub async fn respawn_terminate(&self, spawner: Spawner) -> Result<(), SpawnError> {
1152 for i in self.order.iter() {
1153 let Some(node) = self.nodes[*i as usize] else {
1154 continue;
1155 };
1156 if matches!(node.mode, Mode::Terminate) && !node.is_disabled() && !node.is_detached() {
1157 node.reset();
1158 info!("supervisor: respawning {}", node.name);
1159 if let Some(spawn) = node.spawn {
1160 await_spawn_slot(node).await?;
1161 // A `resources:` node's previous instance restores its slot
1162 // value only after the shutdown ack, so wait (bounded) for
1163 // the restore before the glue's take().
1164 await_resources(node).await?;
1165 spawn(spawner)?;
1166 }
1167 node.set_running(true);
1168 }
1169 }
1170 Ok(())
1171 }
1172}
1173
1174// ─── Runtime control (dependency- and pool-honoring start/stop) ────────────
1175//
1176// The `apply_control` entry point drives one `ControlCommand` from the
1177// application's control surface. Unlike the pool's bare `start_node`/`stop_node`,
1178// these honor the graph: a stop cascades through dependents (so nothing is left
1179// running without a dependency), a start cascades through deps (so nothing comes
1180// up before what it needs), and either expands across a whole `ElasticPool` so
1181// the pool is controlled as a unit. A manual stop/pause also sets the
1182// lifecycle-spanning `disabled` flag, so it sticks against the elastic policy and
1183// the wake respawn.
1184
1185// Graph-index helpers used by BOTH the control plane and the pool driver, so they
1186// are gated on either feature — `pool` alone (no `control`) must still compile.
1187#[cfg(any(feature = "control", feature = "pool"))]
1188impl<const N: usize> Supervisor<N> {
1189 /// Position of `node` in `self.nodes` (pointer identity — every node is a
1190 /// `&'static`). `None` only if the node isn't in this graph (impossible for
1191 /// targets sourced from `GRAPH.nodes`; treated as a no-op by callers).
1192 fn index_of(&self, node: &'static TaskNode) -> Option<usize> {
1193 self.nodes
1194 .iter()
1195 .position(|n| n.is_some_and(|x| core::ptr::eq(x, node)))
1196 }
1197
1198 /// Whether every dependency of `node` is currently running, resolved through
1199 /// the graph's index table. The pool driver checks this before growing a
1200 /// worker, so a pool member is never spawned while one of its dependencies is
1201 /// down.
1202 #[cfg(feature = "pool")]
1203 pub(crate) fn deps_running(&self, node: &'static TaskNode) -> bool {
1204 match self.index_of(node) {
1205 Some(i) => self.deps[i]
1206 .iter()
1207 .all(|&di| self.nodes[di as usize].is_some_and(|n| n.is_running())),
1208 None => false,
1209 }
1210 }
1211}
1212
1213#[cfg(feature = "control")]
1214impl<const N: usize> Supervisor<N> {
1215 /// Seed a membership set with `target` plus — if `target` belongs to an
1216 /// elastic pool — every member of that pool, so control is applied to the
1217 /// whole pool atomically. Pool membership is read from `GRAPH.pools`; with no
1218 /// pools (the `pool` feature off, or none declared) this is just `{target}`.
1219 fn seed(&self, target: &'static TaskNode, set: &mut [bool; N]) {
1220 if let Some(i) = self.index_of(target) {
1221 set[i] = true;
1222 }
1223 #[cfg(feature = "pool")]
1224 for pool in self.pools {
1225 let members = pool.members();
1226 if members.iter().any(|m| core::ptr::eq(*m, target)) {
1227 for m in members {
1228 if let Some(i) = self.index_of(m) {
1229 set[i] = true;
1230 }
1231 }
1232 }
1233 }
1234 }
1235
1236 /// Apply one control command, honoring pool membership and the dependency
1237 /// graph. Run from the supervisor's driver loop (never concurrently with
1238 /// itself), so the cascade is atomic from the application's perspective.
1239 pub async fn apply_control(&self, cmd: ControlCommand, spawner: Spawner) {
1240 match cmd.op {
1241 ControlOp::Deactivate => self.deactivate(cmd.node).await,
1242 ControlOp::Activate => self.activate(cmd.node, spawner).await,
1243 }
1244 }
1245
1246 /// Bring `target` (and its pool, and every transitive dependent) down, in
1247 /// reverse-topological order so each dependent stops before the dependency it
1248 /// relies on. Marks the whole set `disabled` so the stop sticks against the
1249 /// elastic policy and the wake respawn until a matching `activate`.
1250 async fn deactivate(&self, target: &'static TaskNode) {
1251 let mut set = [false; N];
1252 self.seed(target, &mut set);
1253
1254 // Grow the set to include transitive dependents. `order` is
1255 // dependency-first, so when we reach a node its deps are already decided;
1256 // a node joins if any dep it declares is already in the set.
1257 for i in self.order.iter() {
1258 let j = *i as usize;
1259 if set[j] {
1260 continue;
1261 }
1262 let Some(node) = self.nodes[j] else {
1263 continue;
1264 };
1265 // A detached node declares its dep only for start ordering and intends
1266 // to outlive it, so it's never pulled into the cascade.
1267 if node.is_detached() {
1268 continue;
1269 }
1270 if self.deps[j].iter().any(|&di| set[di as usize]) {
1271 set[j] = true;
1272 }
1273 }
1274
1275 // Tear down in reverse topo order (dependents before their deps).
1276 for i in self.order.iter().rev() {
1277 let j = *i as usize;
1278 if !set[j] {
1279 continue;
1280 }
1281 let Some(node) = self.nodes[j] else {
1282 continue;
1283 };
1284 // A detached node is self-managed — never control-stop it. The growth loop
1285 // keeps detached *dependents* out of the set; this also covers a detached
1286 // node that was seeded directly (or a detached pool member). Without it a
1287 // detached one-shot that already exited (stale `is_running`, no ack path)
1288 // would be signalled a shutdown it can never acknowledge, panicking here.
1289 if node.is_detached() {
1290 continue;
1291 }
1292 node.set_disabled(true);
1293 if node.is_running() {
1294 info!("supervisor: control-stop {}", node.name);
1295 self.shutdown_and_wait(node).await;
1296 }
1297 }
1298 }
1299
1300 /// Bring `target` (and its pool, and every transitive dependency) up, in
1301 /// topological order so each dependency starts before its dependent. Clears
1302 /// `disabled` across the set. `OnDemand` (pool) members are only re-enabled,
1303 /// not force-spawned — the elastic policy re-grows them under load, which is
1304 /// the whole point of the pool.
1305 async fn activate(&self, target: &'static TaskNode, spawner: Spawner) {
1306 let mut set = [false; N];
1307 self.seed(target, &mut set);
1308
1309 // Grow the set to include transitive deps. Walk dependents-first
1310 // (reverse topo); when a set member is seen, pull in its direct deps.
1311 // A detached member's `deps:` are start-ordering only (the node is
1312 // self-managed), so don't expand from it — mirrors deactivate's guard;
1313 // otherwise activating a detached target would un-disable deps that
1314 // were independently disabled.
1315 for i in self.order.iter().rev() {
1316 let j = *i as usize;
1317 if set[j] && !self.nodes[j].is_some_and(|n| n.is_detached()) {
1318 for &di in self.deps[j] {
1319 set[di as usize] = true;
1320 }
1321 }
1322 }
1323
1324 // Bring up in topo order (deps before dependents).
1325 for i in self.order.iter() {
1326 let j = *i as usize;
1327 if !set[j] {
1328 continue;
1329 }
1330 let Some(node) = self.nodes[j] else {
1331 continue;
1332 };
1333 // A detached node is self-managed — the supervisor never re-enables or
1334 // re-starts it, even when it is a dependency of an activated target.
1335 if node.is_detached() {
1336 continue;
1337 }
1338 node.set_disabled(false);
1339 if node.is_running() {
1340 continue;
1341 }
1342 match node.mode {
1343 Mode::Terminate => {
1344 info!("supervisor: control-start {}", node.name);
1345 // SpawnError::Busy (pool exhausted) → can't start, skip.
1346 let _ = self.start_node(node, spawner).await;
1347 }
1348 Mode::Pause => {
1349 info!("supervisor: control-resume {}", node.name);
1350 node.reset();
1351 node.signal_resume();
1352 node.set_running(true);
1353 }
1354 // Pool worker — leave it down; the elastic policy regrows it on
1355 // demand now that `disabled` is cleared.
1356 Mode::OnDemand => {}
1357 }
1358 }
1359 }
1360}
1361
1362// ─── Topological sort (Kahn's algorithm, const) ───────────────────────────
1363//
1364// Computes the topological order at *compile time* over a per-node
1365// dependency-index table; a dependency cycle is a compile error.
1366
1367/// Topologically sort a graph given as a per-node dependency-index table.
1368///
1369/// `deps[i]` lists the indices of the nodes that node `i` depends on; the result
1370/// lists node indices in dependency-first order (a dependency appears before its
1371/// dependents). The supervisor iterates it forward for `start` /
1372/// `respawn_terminate` and in reverse for `teardown`.
1373///
1374/// Evaluated at compile time by the code `supervisor_graph!` generates — a
1375/// dependency **cycle is a compile error** (the `panic!` fires during const
1376/// evaluation). `#[doc(hidden)]`: an engine for the macro, not a user-facing API.
1377///
1378/// Supports at most 256 nodes: indices are `u8`, so a larger `N` would truncate.
1379/// The macro rejects bigger graphs at expansion; the assert below is defense in
1380/// depth for a manual caller (a const-eval panic, i.e. a compile error).
1381#[doc(hidden)]
1382#[must_use]
1383pub const fn topo_sort_const<const N: usize>(deps: &[&'static [u8]; N]) -> [u8; N] {
1384 assert!(
1385 N <= 256,
1386 "supervisor graph exceeds 256 node slots (indices are u8)"
1387 );
1388 // in_degree[i] = number of deps of node i not yet resolved.
1389 let mut in_degree = [0u8; N];
1390 let mut i = 0;
1391 while i < N {
1392 in_degree[i] = deps[i].len() as u8;
1393 i += 1;
1394 }
1395
1396 // Queue (fixed array, head/tail indices) seeded with the dependency-free nodes.
1397 let mut queue = [0u8; N];
1398 let mut tail = 0;
1399 i = 0;
1400 while i < N {
1401 if in_degree[i] == 0 {
1402 queue[tail] = i as u8;
1403 tail += 1;
1404 }
1405 i += 1;
1406 }
1407
1408 let mut order = [0u8; N];
1409 let mut produced = 0;
1410 let mut head = 0;
1411 while head < tail {
1412 let node = queue[head] as usize;
1413 head += 1;
1414 order[produced] = node as u8;
1415 produced += 1;
1416
1417 // Decrement the in-degree of every node that depends on `node`.
1418 let mut j = 0;
1419 while j < N {
1420 if in_degree[j] != 0 {
1421 let mut depends = false;
1422 let mut k = 0;
1423 while k < deps[j].len() {
1424 if deps[j][k] as usize == node {
1425 depends = true;
1426 }
1427 k += 1;
1428 }
1429 if depends {
1430 in_degree[j] -= 1;
1431 if in_degree[j] == 0 {
1432 queue[tail] = j as u8;
1433 tail += 1;
1434 }
1435 }
1436 }
1437 j += 1;
1438 }
1439 }
1440
1441 // A cycle leaves some nodes unproduced. During const eval this panic is a
1442 // compile error, so cyclic graphs are rejected at build time. `core::panic!`
1443 // (not the crate's defmt-shimmed `panic!`) keeps this const-evaluable.
1444 if produced != N {
1445 core::panic!("supervisor_graph!: dependency cycle");
1446 }
1447 order
1448}
1449
1450#[cfg(feature = "pool")]
1451mod pool;
1452#[cfg(feature = "pool")]
1453pub use pool::*;
1454
1455#[cfg(feature = "trace")]
1456pub mod trace;
1457
1458/// Declare a supervised task graph and compute its topological order at compile
1459/// time (single source of nodes, deps, pool, and order). See the
1460/// `embassy-supervisor-macros` crate for the surface syntax.
1461#[cfg(feature = "macros")]
1462pub use embassy_supervisor_macros::supervisor_graph;
1463
1464// ─── Tests (host-only) ─────────────────────────────────────────────────────
1465//
1466// Run on the host: `cargo test -p embassy-supervisor --target x86_64-unknown-linux-gnu`
1467// (the workspace `.cargo/config.toml` pins the embedded target, so `--target` is
1468// required to override it). These exercise the compile-time `topo_sort_const`
1469// over index adjacency tables — exactly what `supervisor_graph!` generates.
1470#[cfg(test)]
1471mod tests {
1472 use super::topo_sort_const;
1473
1474 /// Position of index `x` within `order`.
1475 fn pos<const N: usize>(order: &[u8; N], x: u8) -> usize {
1476 order.iter().position(|&y| y == x).expect("index present")
1477 }
1478
1479 #[test]
1480 fn linear_chain_orders_deps_before_dependents() {
1481 // A=0, B=1 dep A, C=2 dep B.
1482 const DEPS: [&[u8]; 3] = [&[], &[0], &[1]];
1483 const ORDER: [u8; 3] = topo_sort_const(&DEPS);
1484 assert_eq!(ORDER, [0, 1, 2]);
1485 }
1486
1487 #[test]
1488 fn diamond_puts_root_first_and_join_last() {
1489 // A=0; B=1 dep A; C=2 dep A; D=3 dep B,C.
1490 const DEPS: [&[u8]; 4] = [&[], &[0], &[0], &[1, 2]];
1491 const ORDER: [u8; 4] = topo_sort_const(&DEPS);
1492 assert_eq!(ORDER[0], 0, "root first");
1493 assert_eq!(ORDER[3], 3, "join last");
1494 assert!(pos(&ORDER, 1) < pos(&ORDER, 3), "B before D");
1495 assert!(pos(&ORDER, 2) < pos(&ORDER, 3), "C before D");
1496 }
1497
1498 #[test]
1499 fn independent_nodes_all_present() {
1500 const DEPS: [&[u8]; 2] = [&[], &[]];
1501 const ORDER: [u8; 2] = topo_sort_const(&DEPS);
1502 assert!(ORDER.contains(&0) && ORDER.contains(&1));
1503 }
1504
1505 #[test]
1506 fn unsorted_input_is_sorted() {
1507 // Declared out of dependency order: node 0 depends on 1 and 2, node 2 on 1.
1508 const DEPS: [&[u8]; 3] = [&[1, 2], &[], &[1]];
1509 const ORDER: [u8; 3] = topo_sort_const(&DEPS);
1510 assert!(pos(&ORDER, 1) < pos(&ORDER, 2), "1 before 2");
1511 assert!(pos(&ORDER, 2) < pos(&ORDER, 0), "2 before 0");
1512 }
1513
1514 #[test]
1515 fn evaluates_at_compile_time() {
1516 // The sort runs in a `const` context, proving it is const-evaluable.
1517 // (A cyclic table here would be a *compile* error, not a test failure.)
1518 const DEPS: [&[u8]; 3] = [&[], &[0], &[1]];
1519 const _: () = {
1520 let order = topo_sort_const(&DEPS);
1521 assert!(order[0] == 0 && order[1] == 1 && order[2] == 2);
1522 };
1523 }
1524
1525 // Uncommenting this must fail to compile ("dependency cycle"):
1526 // const CYCLE: [&[u8]; 2] = [&[1], &[0]];
1527 // const _BAD: [u8; 2] = topo_sort_const(&CYCLE);
1528}