pub struct WorkerCtx<'a, S, W>{ /* private fields */ }Expand description
Programmer-facing worker context.
Workers may read shared state, mutate their own scratch, observe cancellation, and synchronize at the implicit tasklet-wide barrier. Workers must not perform authority-bearing hostcalls.
Implementations§
Source§impl<'a, S, W> WorkerCtx<'a, S, W>
impl<'a, S, W> WorkerCtx<'a, S, W>
Sourcepub fn worker_index(&self) -> u16
pub fn worker_index(&self) -> u16
This worker’s lane index in [0, worker_count()).
Sourcepub fn worker_count(&self) -> u16
pub fn worker_count(&self) -> u16
Total worker count for this tasklet.
Sourcepub fn data_worker_index(&self) -> u32
pub fn data_worker_index(&self) -> u32
Index of this worker among data workers (excluding the coordinator lane).
Data workers are wasm worker indices 1..worker_count(),
exposed here as 0..data_worker_count(). Use this for
partitioning input across data workers via partition::range or
partition::tiles. Mirrors the wasm32 guest helper.
Under the Phase 48.3 coordinator-lane programming model, worker
0 only runs the coordinator closure, so this method is only ever
called from a worker closure invocation where worker_index() >= 1, and the subtraction is structurally safe.
Sourcepub fn data_worker_count(&self) -> u32
pub fn data_worker_count(&self) -> u32
Number of data workers (worker lanes excluding the coordinator lane).
Equal to worker_count() - 1. Always at least 1 for a valid
shared-memory tasklet (the SDK builder requires workers >= 2).
Sourcepub fn barrier(&self) -> Result<(), Cancelled>
pub fn barrier(&self) -> Result<(), Cancelled>
Behavior diverges across targets. See the “Source portability”
section of docs/grafos/shared-memory-tasklet-programming-model.md.
Wait at the implicit tasklet-wide barrier. Returns
[Err(Cancelled)] if the tasklet was cancelled by the time this
worker releases from the barrier.
On the host mock this IS a real barrier among the data
workers: the underlying std::sync::Barrier is sized to
data_worker_count() (not worker_count()), and the
coordinator lane is not a participant. That means host-side
w.barrier() rendezvous with other data workers, but NOT with
the coordinator.
On wasm32 ([super::guest::WorkerCtx::barrier]) this
rendezvous with the coordinator lane as well, because worker 0
re-enters tasklet_run and participates in fb_barrier_wait().
The asymmetry in a single sentence: on host mock, coord.barrier()
is a no-op-with-cancel-check while w.barrier() is a real
data-worker rendezvous. On wasm32 both are real full-lane
barriers. Source that relies on cross-lane coordinator/worker
rendezvous must #[cfg]-gate this call.
Read-only access to shared state. For shared mutation, the program
uses interior mutability inside S (e.g. atomics).
Sourcepub fn input(&self) -> &'a [u8] ⓘ
pub fn input(&self) -> &'a [u8] ⓘ
Input bytes provided to the tasklet at submission time.
Mirrors CoordinatorCtx::input: every worker sees the same
bytes read-only. Workers must not mutate these bytes; for
cross-worker communication use the Shared region with atomics.
The returned slice is tied to the context’s lifetime rather
than to &self, so callers can hold it across a
scratch_mut() borrow.
Sourcepub fn scratch_mut(&mut self) -> &mut W
pub fn scratch_mut(&mut self) -> &mut W
Mutable access to this worker’s private scratch. The type system guarantees that no other worker can touch this slot.
Sourcepub fn scratch(&self) -> &W
pub fn scratch(&self) -> &W
Target-specific. Read-only access to this worker’s scratch.
Host mock only — the wasm32 guest’s WorkerCtx exposes
scratch_mut(&mut self) only and has no read-only accessor.
Sourcepub fn fuel_checkpoint(&mut self, amount: u64) -> Result<(), FuelExhausted>
pub fn fuel_checkpoint(&mut self, amount: u64) -> Result<(), FuelExhausted>
Cooperatively reconcile fuel with the lease-wide shared pool.
Returns Ok(()) on a successful debit. Returns
Err(FuelExhausted) when the shared pool is exhausted, in which
case the worker MUST stop computing immediately — there is no
recovery path; a subsequent fuel-consuming instruction will trap
fail-closed.
V2 (shared-pool) workers MUST call this at safe points within
any compute loop that may consume more fuel than the initial
precharge. On V1 (per-worker-slice) launches this method always
returns Err(FuelExhausted) because no shared pool is installed
— V1 source should not call it.
amount == 0 is a silent no-op returning Ok(()).
Mirrors [super::guest::WorkerCtx::fuel_checkpoint].