Bug: 180427432

Clone this repo:
  1. e931a5f Make async-task available to product and vendor am: e7cd2b3885 by Matthew Maurer · 3 months ago main master android-u-beta-1-gpl
  2. e7cd2b3 Make async-task available to product and vendor by Matthew Maurer · 3 months ago
  3. 88cac09 Update TEST_MAPPING am: 4f3e765eb4 by Jeff Vander Stoep · 4 months ago
  4. 4f3e765 Update TEST_MAPPING by Jeff Vander Stoep · 4 months ago
  5. 8304cf8 Update TEST_MAPPING am: 72f0890b13 by David Brazdil · 4 months ago


Build License Cargo Documentation

Task abstraction for building executors.

To spawn a future onto an executor, we first need to allocate it on the heap and keep some state attached to it. The state indicates whether the future is ready for polling, waiting to be woken up, or completed. Such a stateful future is called a task.

All executors have a queue that holds scheduled tasks:

let (sender, receiver) = flume::unbounded();

A task is created using either spawn(), spawn_local(), or spawn_unchecked() which return a Runnable and a Task:

// A future that will be spawned.
let future = async { 1 + 2 };

// A function that schedules the task when it gets woken up.
let schedule = move |runnable| sender.send(runnable).unwrap();

// Construct a task.
let (runnable, task) = async_task::spawn(future, schedule);

// Push the task into the queue by invoking its schedule function.

The Runnable is used to poll the task's future, and the Task is used to await its output.

Finally, we need a loop that takes scheduled tasks from the queue and runs them:

for runnable in receiver {

Method run() polls the task's future once. Then, the Runnable vanishes and only reappears when its Waker wakes the task, thus scheduling it to be run again.


Licensed under either of

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.