WorkerCtx

Struct WorkerCtx 

Source
pub struct WorkerCtx<'a, S, W>
where S: Send + Sync + 'static, W: Send + 'static,
{ /* 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>
where S: Send + Sync + 'static, W: Send + 'static,

Source

pub fn worker_index(&self) -> u16

This worker’s lane index in [0, worker_count()).

Source

pub fn worker_count(&self) -> u16

Total worker count for this tasklet.

Source

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.

Source

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

Source

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.

Source

pub fn cancelled(&self) -> bool

Whether the tasklet has been cancelled.

Source

pub fn shared(&self) -> &S

Read-only access to shared state. For shared mutation, the program uses interior mutability inside S (e.g. atomics).

Source

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.

Source

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.

Source

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.

Source

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

Auto Trait Implementations§

§

impl<'a, S, W> Freeze for WorkerCtx<'a, S, W>

§

impl<'a, S, W> RefUnwindSafe for WorkerCtx<'a, S, W>

§

impl<'a, S, W> Send for WorkerCtx<'a, S, W>

§

impl<'a, S, W> Sync for WorkerCtx<'a, S, W>
where W: Sync,

§

impl<'a, S, W> Unpin for WorkerCtx<'a, S, W>

§

impl<'a, S, W> !UnwindSafe for WorkerCtx<'a, S, W>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.