pub struct Executor { /* private fields */ }Expand description
Single-threaded async executor.
Manages task lifecycle: spawn, poll, complete, free. Tasks are
allocated via Box (default) or slab (via spawn_slab). Each
task’s header contains a free_fn that knows how to deallocate
its own storage — the executor doesn’t know or care which
allocator was used.
§UnsafeCell on incoming and deferred_free
These fields are wrapped in UnsafeCell to prevent a provenance
aliasing violation. During poll(), raw pointers to these Vecs are
stored in TLS for wakers to push into. Later in the same poll(),
complete_task(&mut self) takes &mut self — which under Rust’s
aliasing rules asserts exclusive access to ALL fields. Without
UnsafeCell, this invalidates the TLS pointers because two &mut
paths to the same memory exist. UnsafeCell opts these fields out
of &mut’s exclusivity guarantee, telling the compiler they may be
accessed through other paths (the TLS raw pointers).
This is NOT a performance concern — UnsafeCell is zero-sized and
get() compiles to a no-op pointer cast. The only effect is that
the compiler won’t optimize based on exclusive access to these fields.
Implementations§
Source§impl Executor
impl Executor
Sourcepub fn spawn_boxed<F>(&mut self, future: F) -> JoinHandle<F::Output> ⓘ
pub fn spawn_boxed<F>(&mut self, future: F) -> JoinHandle<F::Output> ⓘ
Spawn an async task via Box allocation. Returns a JoinHandle
that can be awaited for the task’s output.
Sourcepub fn task_count(&self) -> usize
pub fn task_count(&self) -> usize
Number of live tasks.
Sourcepub fn set_tasks_per_cycle(&mut self, limit: usize)
pub fn set_tasks_per_cycle(&mut self, limit: usize)
Set the maximum tasks to poll per cycle.