Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 1 | //! Task abstraction for building executors. |
| 2 | //! |
Stjepan Glavina | a94d2f4 | 2020-01-25 00:14:33 +0100 | [diff] [blame] | 3 | //! # Spawning |
| 4 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 5 | //! To spawn a future onto an executor, we first need to allocate it on the heap and keep some |
| 6 | //! state alongside it. The state indicates whether the future is ready for polling, waiting to be |
| 7 | //! woken up, or completed. Such a future is called a *task*. |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 8 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 9 | //! All executors have some kind of queue that holds runnable tasks: |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 10 | //! |
| 11 | //! ``` |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 12 | //! let (sender, receiver) = crossbeam::channel::unbounded(); |
| 13 | //! # |
| 14 | //! # // A future that will get spawned. |
| 15 | //! # let future = async { 1 + 2 }; |
| 16 | //! # |
| 17 | //! # // A function that schedules the task when it gets woken up. |
| 18 | //! # let schedule = move |task| sender.send(task).unwrap(); |
| 19 | //! # |
| 20 | //! # // Construct a task. |
| 21 | //! # let (task, handle) = async_task::spawn(future, schedule, ()); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 22 | //! ``` |
| 23 | //! |
Stjepan Glavina | fcfa4ab | 2019-11-25 18:39:17 +0100 | [diff] [blame] | 24 | //! A task is constructed using either [`spawn`] or [`spawn_local`]: |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 25 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 26 | //! ``` |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 27 | //! # let (sender, receiver) = crossbeam::channel::unbounded(); |
| 28 | //! # |
| 29 | //! // A future that will be spawned. |
| 30 | //! let future = async { 1 + 2 }; |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 31 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 32 | //! // A function that schedules the task when it gets woken up. |
| 33 | //! let schedule = move |task| sender.send(task).unwrap(); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 34 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 35 | //! // Construct a task. |
| 36 | //! let (task, handle) = async_task::spawn(future, schedule, ()); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 37 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 38 | //! // Push the task into the queue by invoking its schedule function. |
| 39 | //! task.schedule(); |
| 40 | //! ``` |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 41 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 42 | //! The last argument to the [`spawn`] function is a *tag*, an arbitrary piece of data associated |
| 43 | //! with the task. In most executors, this is typically a task identifier or task-local storage. |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 44 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 45 | //! The function returns a runnable [`Task`] and a [`JoinHandle`] that can await the result. |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 46 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 47 | //! # Execution |
| 48 | //! |
| 49 | //! Task executors have some kind of main loop that drives tasks to completion. That means taking |
| 50 | //! runnable tasks out of the queue and running each one in order: |
| 51 | //! |
| 52 | //! ```no_run |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 53 | //! # let (sender, receiver) = crossbeam::channel::unbounded(); |
| 54 | //! # |
| 55 | //! # // A future that will get spawned. |
| 56 | //! # let future = async { 1 + 2 }; |
| 57 | //! # |
| 58 | //! # // A function that schedules the task when it gets woken up. |
| 59 | //! # let schedule = move |task| sender.send(task).unwrap(); |
| 60 | //! # |
| 61 | //! # // Construct a task. |
| 62 | //! # let (task, handle) = async_task::spawn(future, schedule, ()); |
| 63 | //! # |
| 64 | //! # // Push the task into the queue by invoking its schedule function. |
| 65 | //! # task.schedule(); |
| 66 | //! # |
| 67 | //! for task in receiver { |
| 68 | //! task.run(); |
| 69 | //! } |
| 70 | //! ``` |
| 71 | //! |
| 72 | //! When a task is run, its future gets polled. If polling does not complete the task, that means |
| 73 | //! it's waiting for another future and needs to go to sleep. When woken up, its schedule function |
| 74 | //! will be invoked, pushing it back into the queue so that it can be run again. |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 75 | //! |
| 76 | //! # Cancellation |
| 77 | //! |
Stjepan Glavina | 5c398cf | 2019-08-20 15:29:43 +0200 | [diff] [blame] | 78 | //! Both [`Task`] and [`JoinHandle`] have methods that cancel the task. When cancelled, the task's |
| 79 | //! future will not be polled again and will get dropped instead. |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 80 | //! |
| 81 | //! If cancelled by the [`Task`] instance, the task is destroyed immediately. If cancelled by the |
| 82 | //! [`JoinHandle`] instance, it will be scheduled one more time and the next attempt to run it will |
| 83 | //! simply destroy it. |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 84 | //! |
| 85 | //! # Performance |
| 86 | //! |
Stjepan Glavina | 5c398cf | 2019-08-20 15:29:43 +0200 | [diff] [blame] | 87 | //! Task construction incurs a single allocation that holds its state, the schedule function, and |
| 88 | //! the future or the result of the future if completed. |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 89 | //! |
Stjepan Glavina | a94d2f4 | 2020-01-25 00:14:33 +0100 | [diff] [blame] | 90 | //! The layout of a task is equivalent to 4 `usize`s followed by the schedule function, and then by |
| 91 | //! a union of the future and its output. |
| 92 | //! |
| 93 | //! # Waking |
| 94 | //! |
| 95 | //! The handy [`waker_fn`] constructor converts any function into a [`Waker`]. Every time it is |
| 96 | //! woken, the function gets called: |
| 97 | //! |
| 98 | //! ``` |
| 99 | //! let waker = async_task::waker_fn(|| println!("Wake!")); |
| 100 | //! |
| 101 | //! // Prints "Wake!" twice. |
| 102 | //! waker.wake_by_ref(); |
| 103 | //! waker.wake_by_ref(); |
| 104 | //! ``` |
| 105 | //! |
| 106 | //! This is useful for implementing single-future executors like [`block_on`]. |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 107 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 108 | //! [`spawn`]: fn.spawn.html |
Stjepan Glavina | fcfa4ab | 2019-11-25 18:39:17 +0100 | [diff] [blame] | 109 | //! [`spawn_local`]: fn.spawn_local.html |
Stjepan Glavina | a94d2f4 | 2020-01-25 00:14:33 +0100 | [diff] [blame] | 110 | //! [`waker_fn`]: fn.waker_fn.html |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 111 | //! [`Task`]: struct.Task.html |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 112 | //! [`JoinHandle`]: struct.JoinHandle.html |
Stjepan Glavina | a94d2f4 | 2020-01-25 00:14:33 +0100 | [diff] [blame] | 113 | //! [`Waker`]: https://doc.rust-lang.org/std/task/struct.Waker.html |
| 114 | //! [`block_on`]: https://github.com/async-rs/async-task/blob/master/examples/block.rs |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 115 | |
Stjepan Glavina | 921e8a0 | 2020-01-06 14:31:28 -0600 | [diff] [blame] | 116 | #![no_std] |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 117 | #![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)] |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 118 | #![doc(test(attr(deny(rust_2018_idioms, warnings))))] |
| 119 | #![doc(test(attr(allow(unused_extern_crates, unused_variables))))] |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 120 | |
Stjepan Glavina | 921e8a0 | 2020-01-06 14:31:28 -0600 | [diff] [blame] | 121 | extern crate alloc; |
| 122 | |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 123 | mod header; |
| 124 | mod join_handle; |
| 125 | mod raw; |
| 126 | mod state; |
| 127 | mod task; |
| 128 | mod utils; |
Stjepan Glavina | a94d2f4 | 2020-01-25 00:14:33 +0100 | [diff] [blame] | 129 | mod waker_fn; |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 130 | |
| 131 | pub use crate::join_handle::JoinHandle; |
Stjepan Glavina | b7a2496 | 2020-02-03 15:18:01 +0100 | [diff] [blame] | 132 | pub use crate::task::{spawn, Task}; |
Stjepan Glavina | a94d2f4 | 2020-01-25 00:14:33 +0100 | [diff] [blame] | 133 | pub use crate::waker_fn::waker_fn; |
Stjepan Glavina | b7a2496 | 2020-02-03 15:18:01 +0100 | [diff] [blame] | 134 | |
| 135 | #[cfg(any(unix, windows))] |
| 136 | pub use crate::task::spawn_local; |