pub struct TaskScheduler { /* private fields */ }Expand description
Scheduler that tracks spawned async tasks by their future ID.
The VM’s SpawnTask opcode registers a callable here. When the VM later
suspends on WaitType::Future { id }, the host looks up the callable,
executes it, and stores the result so the VM can resume.
Supports both inline tasks (callable executed synchronously at await-time) and external tasks (completed by background Tokio tasks via oneshot channels).
Implementations§
Source§impl TaskScheduler
impl TaskScheduler
Sourcepub fn register(
&mut self,
task_id: u64,
callable_bits: u64,
callable_kind: NativeKind,
)
pub fn register( &mut self, task_id: u64, callable_bits: u64, callable_kind: NativeKind, )
Register a callable for a given task_id.
Called by op_spawn_task when a new task is spawned. The caller
transfers one strong-count share for the kinded pair into the
scheduler; on take_callable (or Drop) the share transfers back
out (or is released).
Sourcepub fn take_callable(&mut self, task_id: u64) -> Option<(u64, NativeKind)>
pub fn take_callable(&mut self, task_id: u64) -> Option<(u64, NativeKind)>
Take (remove) the callable for task_id so it can be executed.
Returns None if the task was already consumed or never registered.
Ownership of the kinded pair transfers to the caller.
Sourcepub fn complete(
&mut self,
task_id: u64,
value_bits: u64,
value_kind: NativeKind,
)
pub fn complete( &mut self, task_id: u64, value_bits: u64, value_kind: NativeKind, )
Record a completed result for a task.
The caller transfers one strong-count share into the scheduler. If a completion was already recorded, the prior share is released.
Sourcepub fn get_result(&self, task_id: u64) -> Option<&TaskStatus>
pub fn get_result(&self, task_id: u64) -> Option<&TaskStatus>
Get the result for a task, if it has completed.
Sourcepub fn is_resolved(&self, task_id: u64) -> bool
pub fn is_resolved(&self, task_id: u64) -> bool
Check whether a task has a stored result (completed or cancelled).
Sourcepub fn register_external(
&mut self,
task_id: u64,
) -> Sender<Result<(u64, NativeKind), String>>
pub fn register_external( &mut self, task_id: u64, ) -> Sender<Result<(u64, NativeKind), String>>
Register an externally-completed task (e.g., remote call).
Returns a oneshot::Sender that the background task uses to deliver the
result (kinded pair). The scheduler marks the task as Pending and
stores the receiver.
Sourcepub fn try_resolve_external(
&mut self,
task_id: u64,
) -> Option<Result<(u64, NativeKind), VMError>>
pub fn try_resolve_external( &mut self, task_id: u64, ) -> Option<Result<(u64, NativeKind), VMError>>
Try to resolve an external task (non-blocking check).
Returns Some(Ok((bits, kind))) if the external task completed
successfully, Some(Err(..)) on error/cancellation, or None if
still pending.
On the cached-completion fast path, the cached share is cloned
(clone_with_kind) so both the scheduler entry and the returned
pair own independent shares — caller drops/uses freely.
Sourcepub fn has_external(&self, task_id: u64) -> bool
pub fn has_external(&self, task_id: u64) -> bool
Check whether a task has an external receiver (is externally-completed).
Sourcepub fn take_external_receiver(
&mut self,
task_id: u64,
) -> Option<Receiver<Result<(u64, NativeKind), String>>>
pub fn take_external_receiver( &mut self, task_id: u64, ) -> Option<Receiver<Result<(u64, NativeKind), String>>>
Take the external receiver for async awaiting.
Used by execute_with_async when it needs to truly .await an external
task’s completion.
Sourcepub fn resolve_task<F>(
&mut self,
task_id: u64,
executor_fn: F,
) -> Result<(u64, NativeKind), VMError>
pub fn resolve_task<F>( &mut self, task_id: u64, executor_fn: F, ) -> Result<(u64, NativeKind), VMError>
Resolve a single task by executing its callable on a fresh VM executor.
This is the synchronous (inline) strategy: the callable is executed immediately when awaited. Returns the result kinded pair, or an error.
The executor_fn callback receives the callable kinded pair and must
execute it, returning the result kinded pair. Ownership of the pair
transfers into the callback; the callback’s returned pair owns one
share which is then cached and a clone returned to the caller.
Sourcepub fn resolve_task_group<F>(
&mut self,
kind: u8,
task_ids: &[u64],
executor_fn: F,
) -> Result<(u64, NativeKind), VMError>
pub fn resolve_task_group<F>( &mut self, kind: u8, task_ids: &[u64], executor_fn: F, ) -> Result<(u64, NativeKind), VMError>
Resolve a task group according to the join strategy.
Join kinds (encoded in the high 2 bits of JoinInit’s packed operand): 0 = All — wait for all tasks, return array of results 1 = Race — return first completed result 2 = Any — return first successful result (skip errors) 3 = AllSettled — return array of {status, value/error} for every task
Since we execute synchronously, “race” and “any” still run all tasks sequentially but return early on the first applicable result.
Returned aggregate is a TaskGroup-shaped heap value (ArcNativeKind::Ptr(HeapKind::TaskGroup). (The
pre-bulldozer code returned a heap array of kinded results; without
a kinded VMArray helper post-§2.7.4, the TaskGroup carrier is the
minimum shape the await-time decoder can re-walk.)
Trait Implementations§
Source§impl Debug for TaskScheduler
impl Debug for TaskScheduler
Source§impl Default for TaskScheduler
impl Default for TaskScheduler
Source§impl Drop for TaskScheduler
impl Drop for TaskScheduler
Source§fn drop(&mut self)
fn drop(&mut self)
Release every heap-bearing share the scheduler still owns.
Required to honor the §2.7.7 retain-on-store contract: every value
inserted via register / complete carries a strong-count share;
if the scheduler is dropped before consumers retire those shares,
drop_with_kind releases them here.
Auto Trait Implementations§
impl Freeze for TaskScheduler
impl !RefUnwindSafe for TaskScheduler
impl Send for TaskScheduler
impl Sync for TaskScheduler
impl Unpin for TaskScheduler
impl UnsafeUnpin for TaskScheduler
impl !UnwindSafe for TaskScheduler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more