# task

Version: 0.5.0
Framework commit: d503df2befa756e681fcdc6250e9552dc7388bea
Canonical URL: /docs/0.5.0/api/ferroui-system/task/

**module** · [`ferroui-system`](/docs/0.5.0/api/ferroui-system/) · v0.5.0

```rust
module task
```

Futures run on the thread that spawns them: the executor of the workspace (named after
`std::task`; phase C4 of #423, docs/idiomatic-rust-async.md, section 3).

One task table and one poll loop per thread. A spawned future is a task of the thread that
spawns it, polled on that thread only, and its handle ([`JoinHandle`](/docs/0.5.0/api/ferroui-system/task/struct-joinhandle/)) is a future of its
outcome: its output, or a [`JoinError`](/docs/0.5.0/api/ferroui-system/task/enum-joinerror/) when it was aborted or panicked. Where the polls of a
woken task run is the task's schedule, fixed when it is spawned:

- [`spawn_local`](/docs/0.5.0/api/ferroui-system/task/function-spawn_local/) gives the task to the queue of its thread. A wake on that thread polls the
  task at once, inside the call that wakes it; a wake from another thread waits in the queue
  of the task's thread until that thread runs [`run_pending`](/docs/0.5.0/api/ferroui-system/task/function-run_pending/) (or [`block_on`](/docs/0.5.0/api/ferroui-system/task/function-block_on/) does), and
  is announced to the schedule of the thread when one is set ([`set_thread_schedule`](/docs/0.5.0/api/ferroui-system/task/function-set_thread_schedule/)).
- [`spawn_local_on`](/docs/0.5.0/api/ferroui-system/task/function-spawn_local_on/) gives the task a [`Schedule`](/docs/0.5.0/api/ferroui-system/task/trait-schedule/): every wake, from any thread, queues one
  poll there (at most one queued poll at a time), and the schedule runs it on the task's
  thread in order with its other work. The first poll runs inside the spawning call
  (`FirstPoll::Now`) or is queued as well (`FirstPoll::Scheduled`).
- [`spawn_on`](/docs/0.5.0/api/ferroui-system/task/function-spawn_on/) does the same from any thread: a `Send` closure makes the future on the
  schedule's thread, inside the first work it gives the schedule, and the future is polled
  there at once.

The schedule of a thread ([`set_thread_schedule`](/docs/0.5.0/api/ferroui-system/task/function-set_thread_schedule/); a dispatcher is the schedule of its thread)
is where the wakes from other threads of the tasks in the queue of the thread go, and the
callbacks that other threads send to it. The work of a schedule may make it the current
schedule of its thread while it runs ([`set_current_schedule`](/docs/0.5.0/api/ferroui-system/task/function-set_current_schedule/), [`current_schedule`](/docs/0.5.0/api/ferroui-system/task/function-current_schedule/); a
dispatcher job is the dispatcher at the priority of the job): [`spawn_local`](/docs/0.5.0/api/ferroui-system/task/function-spawn_local/) in that work
gives the task that schedule, whose wakes poll it at once while the same schedule is current
and queue a poll on it otherwise (the rule of `await` with a `SynchronizationContext`), and
`Schedule::schedule_local` posts work that is not `Send` to it.

A schedule is told of the tasks given to it (`Schedule::spawned`), with an [`AbortHandle`](/docs/0.5.0/api/ferroui-system/task/struct-aborthandle/),
so that it can end them (a dispatcher aborts its tasks when it shuts down). [`try_id`](/docs/0.5.0/api/ferroui-system/task/function-try_id/) is the
[`Id`](/docs/0.5.0/api/ferroui-system/task/struct-id/) of the task being polled, and [`poll_now`](/docs/0.5.0/api/ferroui-system/task/function-poll_now/) polls a task of the thread inside the calling
code (a dispatcher job that resumes it at another priority).

In both cases a task is never polled inside its own poll: a wake that comes while it is polled
polls it again once that poll has returned (the queue of the thread), or queues a poll (a
schedule), and a queued poll that runs while the task is polled (a nested loop that runs the
schedule's work) marks it, and the running poll schedules it again when it returns.

A panic of a future faults its task: the panic is caught, the future is dropped, and the
handle ends with `JoinError::Panicked`, waking whoever awaits it; the code that woke the
future goes on.

[`Shared`](/docs/0.5.0/api/ferroui-system/task/struct-shared/) (from `JoinHandle::shared`) is the outcome of a task for several observers, with
synchronous continuations (`Shared::on_finished`) that run before the awaiting futures are
woken. `Shared::channel` makes one that code ends by hand ([`SharedSender`](/docs/0.5.0/api/ferroui-system/task/struct-sharedsender/)): the
`TaskCompletionSource<T>` and `Task<T>` of the reactive libraries, whose asynchronous forms give a
`Shared<Result<T, ObservableError>>` (`Shared::flatten` is a future of that result,
[`Flatten`](/docs/0.5.0/api/ferroui-system/task/struct-flatten/)).

[`oneshot`](/docs/0.5.0/api/ferroui-system/task/oneshot/) is a value sent once to the code that awaits it (the port of
`TaskCompletionSource`), from any thread; a sender dropped without sending ends the receiver
with [`Canceled`](/docs/0.5.0/api/ferroui-system/task/oneshot/struct-canceled/).

[View source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.System/task/mod.rs#L1)

## Contents

- [`oneshot`](/docs/0.5.0/api/ferroui-system/task/oneshot/) — module
- [`Canceled`](/docs/0.5.0/api/ferroui-system/task/oneshot/struct-canceled/) — struct
- [`block_on`](/docs/0.5.0/api/ferroui-system/task/function-block_on/) — function
- [`current_schedule`](/docs/0.5.0/api/ferroui-system/task/function-current_schedule/) — function
- [`is_current_schedule`](/docs/0.5.0/api/ferroui-system/task/function-is_current_schedule/) — function
- [`poll_now`](/docs/0.5.0/api/ferroui-system/task/function-poll_now/) — function
- [`run_pending`](/docs/0.5.0/api/ferroui-system/task/function-run_pending/) — function
- [`set_current_schedule`](/docs/0.5.0/api/ferroui-system/task/function-set_current_schedule/) — function
- [`set_thread_schedule`](/docs/0.5.0/api/ferroui-system/task/function-set_thread_schedule/) — function
- [`spawn_local`](/docs/0.5.0/api/ferroui-system/task/function-spawn_local/) — function
- [`spawn_local_on`](/docs/0.5.0/api/ferroui-system/task/function-spawn_local_on/) — function
- [`spawn_local_on_result`](/docs/0.5.0/api/ferroui-system/task/function-spawn_local_on_result/) — function
- [`spawn_on`](/docs/0.5.0/api/ferroui-system/task/function-spawn_on/) — function
- [`thread_schedule`](/docs/0.5.0/api/ferroui-system/task/function-thread_schedule/) — function
- [`try_id`](/docs/0.5.0/api/ferroui-system/task/function-try_id/) — function
- [`AbortHandle`](/docs/0.5.0/api/ferroui-system/task/struct-aborthandle/) — struct
- [`Id`](/docs/0.5.0/api/ferroui-system/task/struct-id/) — struct
- [`JoinError`](/docs/0.5.0/api/ferroui-system/task/enum-joinerror/) — enum
- [`JoinHandle`](/docs/0.5.0/api/ferroui-system/task/struct-joinhandle/) — struct
- [`FirstPoll`](/docs/0.5.0/api/ferroui-system/task/enum-firstpoll/) — enum
- [`Schedule`](/docs/0.5.0/api/ferroui-system/task/trait-schedule/) — trait
- [`Flatten`](/docs/0.5.0/api/ferroui-system/task/struct-flatten/) — struct
- [`OnFinishedId`](/docs/0.5.0/api/ferroui-system/task/struct-onfinishedid/) — struct
- [`Shared`](/docs/0.5.0/api/ferroui-system/task/struct-shared/) — struct
- [`SharedSender`](/docs/0.5.0/api/ferroui-system/task/struct-sharedsender/) — struct

## Availability

Extracted for `aarch64-apple-darwin`. Cargo features are listed in the [crate index](/docs/0.5.0/api/).
