Expand description
The pluggable executor async exports run on, and the launcher driver that keeps the completion promise.
An exported async fn lowers to a launcher that returns immediately and a
completion callback that fires when the future resolves. Something has to
drive the future in between:
- By default, a fixed pool of worker threads (one per available core, at
least two) started lazily on the first launch. Workers share one run
queue, and a future’s waker puts it back on the queue, so a launch
never costs a thread. The pool has no reactor, and its workers are few:
a future that blocks a worker for a long time (synchronous I/O, a long
computation) holds up every other call, so such work belongs on a
runtime of its own (the
tokiofeature, orset_spawner). - With the
tokiofeature, a Tokio runtime: the current one when the launcher is called from inside a runtime, otherwise a multi-thread runtime the library creates on first use. - On
wasm32, which has no threads, the future is polled inline before the launcher returns. A future still pending once nothing wakes it completes withGENERIC_ERROR_CODE(“async function suspended with no executor on wasm32”) instead of spinning.
A producer installs its own Spawner once at startup with
set_spawner, which overrides every default:
// e.g. `|fut| { tokio_handle.spawn(fut); }`
let _ = weaveffi::abi::set_spawner(|fut| spawn_on_my_runtime(fut));Generated launchers call launch_async, which runs the launcher’s own
work (lifting the inputs) under catch_unwind and hands the producer’s
future to run_async. Every future handed to a spawner is
Send + 'static and already wrapped so a panic inside it is caught and
reported through the completion callback; a spawner never observes an
unwinding future. A spawner may also drop a future without finishing it
(a runtime shutting down, say): the completion still fires, with
CANCELLED_ERROR_CODE. A spawner that
panics, or a default executor that can’t start, completes the call with
GENERIC_ERROR_CODE.
Structs§
- Catch
Unwind - A future adapter that catches a panic from the inner future’s
polland resolves toErr(payload)instead of unwinding into the executor. - Spawner
Already Set - The error returned when
set_spawneris called a second time.
Enums§
- Spawn
Error - Why the executor didn’t take a future.
Traits§
- Spawner
- An executor hook that drives the futures produced by async exports.
Functions§
- block_
on - Drive a future to completion on the current thread, blocking until it resolves.
- default_
pool_ size - The number of worker threads the default executor runs: one per
available core, at least two. (With the
tokiofeature or onwasm32the default executor is something else, and this is0.) - launch_
async - The body of every generated async launcher.
- run_
async - Run an async export to completion and deliver its result exactly once.
- set_
spawner - Install the process-wide spawner async exports run on.
- spawn
- Run
futon the installed spawner, or on the default executor.