docs

您正在閱讀 v0.1.0 的文件。Nabi 0.1.0 已停止支援。 最新版本:v0.1.3 →

core::task

本頁內容

API 參考只有英文版,由標準函式庫原始碼中的英文文件註解產生。

tasks on the work-stealing executor and block_on; Duration / Instant are core::time.

One executor per process, started on first use: N workers on threads that live as long as the process (N = online CPUs or NABIR_WORKERS), the blocking lane and the I/O driver. block_on and par::* attach the calling thread as a guest until their future and its spawn_local tasks are done. Without threads (wasm32-wasip2, the browser) the guest runs everything itself, waiting in nabi_io_wait.

Cancellation: Task::cancel sets a flag and wakes the task; each await checks it on entry and after each resume and leaves the body like a return, running finally / using. A task cancelled before its first poll is never polled.

NABIR_THREAD_PER_CORE=1 keeps every task on one worker's local queue, so nothing is stolen. NABIR_DYNAMIC_WORKERS=1 wakes only active workers and activates one more when all are busy.

Re-exports

core::task::parking::{readable, registrations, wake_io, writable}Source

pub use core::task::parking::{readable, registrations, wake_io, writable}
  • readable
  • wake_io
  • writable

Structs

SelectFutureSource

pub struct SelectFuture<F>

The future select returns: (index, output) of the first future to complete.

Trait implementations 2

CancellationErrorSource

pub struct CancellationError

A task was cancelled.

  • pub const fn message(self) -> str§

    Source

    What went wrong, for display.

Trait implementations 3
  • impl Display for CancellationError
  • impl Debug for CancellationError
  • impl Error for CancellationError

JoinSource

pub struct Join<T>

Task::join.

  • pub fn poll(ref self, ref ctx: Context) throws (CancellationError) -> Poll<T>§

    Source

    One step of join: Ready with the result once the task finished, Pending before. Throws CancellationError for a cancelled task.

Trait implementations 1
  • impl<T> Debug for Join<T>

TaskIdSource

pub struct TaskId

Identifies a task for as long as the process runs (ids are never reused): Task::id, task::id().

  • pub const fn as_u64(self) -> u64§

    Source

    The id as a number, unique for the process's run.

Trait implementations 5

TimeoutErrorSource

pub struct TimeoutError

task::timeout ran out.

  • pub const fn message(self) -> str§

    Source

    What went wrong, for display.

Trait implementations 3
  • impl Display for TimeoutError
  • impl Debug for TimeoutError
  • impl Error for TimeoutError

TaskSource

pub struct Task<T>

A spawned task; await it for its result.

  • pub fn is_finished(self) -> bool§

    Source

    Whether the task has finished, with a result or cancelled.

  • pub const fn id(self) -> TaskId§

    Source

    This task's id (what task::id() returns inside it).

  • pub fn cancel(self)§

    Source

    Ask the task to stop: it is woken, its current await leaves the body (running the finally / using cleanups on the way out) and it finishes without a result; one not yet polled never runs. Awaiting it then panics; use try await t.join() to see CancellationError. A task that reaches no cancellation point after the cancel finishes normally, with its result.

  • pub fn is_cancelled(self) -> bool§

    Source

    Whether cancel was called on the task.

  • pub const fn join(self) -> Join<T>§

    Source

    Await the result, throwing CancellationError for a cancelled task.

Trait implementations 3

Functions

joinSource

pub async fn join<I: IntoIterator<Item = Task<T>>, T>(tasks: I) -> List<T>

Every task's result, in order.

raceSource

pub async fn race<I: IntoIterator<Item = Task<T>>, T>(tasks: I) -> T

The result of whichever task finishes first; ties go to the earlier one. The others are cancelled (by the time race returns they may still run their cleanups), and so are all of them when the awaiting task is. Panics on no tasks.

selectSource

pub const fn select<I: IntoIterator<Item = F>, F: Future>(iter: I) -> SelectFuture<F>

The first of futs to complete, with its index: let (i, v) = await task::select([a, b]). Unlike race the futures are not spawned: they run inside the awaiting task, polled in list order (ties go to the lower index). The futures share one type: async { .. } blocks with the same output do (wrap differing outputs, async { Msg::A(await a) }). Once one completes, every other one that has started is polled once more as cancelled (its await leaves through finally / using, as for Task::cancel) and dropped; one whose cleanups await goes on as a task of its own until they end, and select does not wait for it. Panics on no futures.

spawn_localSource

pub fn spawn_local<F: Future>(fut: F) -> Task<F::Output>

Run fut as a new task on this worker only (it need not be Send). From a task a block_on thread happens to poll, that is one pool worker, and the task starts once that poll ends.

current_idSource

pub fn current_id() -> TaskId | none

The running task's id, or none outside a task (sync code, a spawn_blocking thread).

idSource

pub fn id() -> TaskId

The running task's id; panics outside a task (current_id).

registrationsSource

pub fn registrations() -> usize

Parked wakers (all keys) plus pending timers of unfinished tasks: a diagnostic (a cancelled task leaves none behind).

sleepSource

pub async fn sleep(dur: Duration)

Completes once dur has passed since its first poll.

sleep_untilSource

pub async fn sleep_until(when: Instant)

Completes at when.

timeoutSource

pub async fn timeout<T>(dur: Duration, task: Task<T>) throws (TimeoutError) -> T

task's result, or TimeoutError once dur has passed since the first poll: try await task::timeout(d, t). On expiry the task is cancelled (it may still run its cleanups when timeout throws), and so it is when the awaiting task is.

yield_nowSource

pub async fn yield_now()

Let the other ready tasks run once.

block_onSource

pub fn block_on<F: Future>(fut: F) -> F::Output

Run fut to completion from synchronous code. It returns once the spawn_local tasks pinned to this thread are done too.

try_block_onSource

pub fn try_block_on<T, E>(fut: TryAsyncFuture<T, E>) throws (E) -> T

block_on for a throwing async body: its error is thrown here, with the site of the throw that raised it (on whichever thread).

worker_countSource

pub fn worker_count() -> usize

Workers of the executor this thread runs tasks on (1 outside one, and without threads, where the one thread runs every task).

cpu_countSource

pub fn cpu_count() -> usize

Online CPUs (the default pool size).

spawnSource

pub fn spawn<F: Future + Send>(fut: F) -> Task<F::Output> where F::Output: Send

Run fut as a new task on any worker, concurrently with the spawner. Needs a running executor (inside async fn main / block_on). The task gets its own copy of every collection fut holds, what its coroutine captured included: the spawner keeps the originals.

spawn_blockingSource

pub fn spawn_blocking<T: Send>(f: fn() -> T + Send) -> Task<T>

Run f on the executor's blocking lane as a task (tokio's spawn_blocking). For syscalls with no readiness to wait on, like regular files under kqueue / epoll. f is queued at once. A cancel before the lane takes it means it never runs (join throws CancellationError); once it runs it finishes with its result, as it has no cancellation point.

spawn_blocking_with_argSource

pub fn spawn_blocking_with_arg<V: Send, T: Send>(arg: V, f: fn(V) -> T + Send) -> Task<T>

spawn_blocking that hands arg to f on the lane. f's plain captures stay shared with the caller, so they must be Send + Sync; arg only needs Send: it is handed over like a channel send (the task gets a copy of every collection it reaches, and the caller can no longer use it).

Implementations

impl Pool

  • pub const fn workers(self) -> usize§

    Source

    Number of pool threads.

Trait implementations 2
  • unsafe impl Send for Pool
  • unsafe impl Sync for Pool

impl Timeout

  • pub fn poll(ref self, ref ctx: Context) throws (TimeoutError) -> Poll<T>§

    Source

    One step of timeout: Ready with the result once the task finished, Pending before the deadline. Throws TimeoutError once it passed.