Skip to main content

Module execution_context

Module execution_context 

Source
Expand description

SRD-88 — the task-local ExecutionContext.

The load-bearing seam for running multiple executions concurrently in one process, all sharing one session. A deeply-nested fiber finds its execution’s isolated state — exec_id, stop flag, and (as the de-globalization proceeds in later pushes) observer / output channel / scene tree — by reading this task-local context instead of a process-global static.

Axiom A1 — additive de-globalization. The process-globals (SESSION_STOP, GLOBAL_OBSERVER, CHANNEL, …) remain the process default. Code that does not run inside scope (bootstrap, the CLI, single-run nmbrs run, tests) reads the default — behavior is identical to before this seam existed. Only concurrent executions scope a context and get isolation, so the migration is safe and incremental: each accessor that learns to consult the context is a no-op until someone scopes one.

Axiom A2 — exec_id is the only new global. Allocating exec_id needs one process-global counter so two concurrent fresh executions can’t both claim the same id. alloc_exec_id is that single synchronization point.

Structs§

ExecutionContext
Per-execution context (SRD-88 §3). Grows as the de-globalization proceeds; Push 1 carries the exec_id, the per-execution stop flag, and the per-execution observer (display/lifecycle routing).

Functions§

alloc_exec_id
Allocate the next process-unique exec_id.
current_channel
The current execution’s output channel, if a scoped context set one. None outside a scope OR when the scoped context has no channel — the caller falls back to the process-global CHANNEL (A1).
current_exec_id
The current execution’s exec_id, or 1 (the legacy single-execution default) outside any execution scope.
current_observer
The current execution’s observer, if a scoped context set one. None outside a scope OR when the scoped context has no observer — the caller falls back to the process-global GLOBAL_OBSERVER (A1).
current_phase_node
The scene node of the phase the current task is executing, if set (inside a run_phase body or a fiber it propagated). None on tasks/threads outside any phase (e.g. the metrics scheduler) — readers fall back to the first-running DFS heuristic (A1). Distinct from current_scene_tree: that is the execution’s whole tree; this is the one node the task is in.
current_phase_start_ms
Epoch millis at which the phase the current task is executing started, if set. None outside any phase body (the metrics scheduler thread, CLI paths, unit tests that never scoped one) — a caller then has no phase to be relative to and must say so rather than invent an origin.
current_scene_tree
The current execution’s scene tree, if scoped AND installed (None otherwise — the caller falls back to the process-global GLOBAL_TREE, A1).
current_stop
The current execution’s stop flag, if scoped (None outside a scope, so the caller falls back to the process-global stop — A1).
install_scene_tree
Install tree into the current execution’s context, returning true if a context was scoped (so the caller installs into the global only when there is no execution scope). The pre-map walker calls this so each execution’s lifecycle mutations land on its own tree.
propagate
Wrap fut so it runs under the CURRENT execution context — captured now (at the call site, which is still inside the parent’s scope) and re-established as the task-local inside a freshly-spawned task. A no-op pass-through when there is no current context (single-run / CLI / tests — axiom A1).
scope
Run fut with ctx as the task-local ExecutionContext for the whole future. Concurrent executions each scope their own context, so their task-local reads (stop flag, exec_id, …) resolve independently.
try_current
The current task’s ExecutionContext, if running inside a scope.
with_current_phase
Run fut as the body of the phase at scene_node_id (SRD-100 P1c): scopes the task-local [CURRENT_PHASE] for the future’s duration. run_phase wraps itself with this so its whole body — the activity loop and every fiber it propagates — resolves to this phase’s node, and any ambient emit nests under its depth.