Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 1 | //! Task abstraction for building executors. |
| 2 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 3 | //! To spawn a future onto an executor, we first need to allocate it on the heap and keep some |
| 4 | //! state alongside it. The state indicates whether the future is ready for polling, waiting to be |
| 5 | //! woken up, or completed. Such a future is called a *task*. |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 6 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 7 | //! This crate helps with task allocation and polling its future to completion. |
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 | //! # Spawning |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 10 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 11 | //! All executors have some kind of queue that holds runnable tasks: |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 12 | //! |
| 13 | //! ``` |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 14 | //! let (sender, receiver) = crossbeam::channel::unbounded(); |
| 15 | //! # |
| 16 | //! # // A future that will get spawned. |
| 17 | //! # let future = async { 1 + 2 }; |
| 18 | //! # |
| 19 | //! # // A function that schedules the task when it gets woken up. |
| 20 | //! # let schedule = move |task| sender.send(task).unwrap(); |
| 21 | //! # |
| 22 | //! # // Construct a task. |
| 23 | //! # let (task, handle) = async_task::spawn(future, schedule, ()); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 24 | //! ``` |
| 25 | //! |
Stjepan Glavina | fcfa4ab | 2019-11-25 18:39:17 +0100 | [diff] [blame] | 26 | //! A task is constructed using either [`spawn`] or [`spawn_local`]: |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 27 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 28 | //! ``` |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 29 | //! # let (sender, receiver) = crossbeam::channel::unbounded(); |
| 30 | //! # |
| 31 | //! // A future that will be spawned. |
| 32 | //! let future = async { 1 + 2 }; |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 33 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 34 | //! // A function that schedules the task when it gets woken up. |
| 35 | //! let schedule = move |task| sender.send(task).unwrap(); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 36 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 37 | //! // Construct a task. |
| 38 | //! let (task, handle) = async_task::spawn(future, schedule, ()); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 39 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 40 | //! // Push the task into the queue by invoking its schedule function. |
| 41 | //! task.schedule(); |
| 42 | //! ``` |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 43 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 44 | //! The last argument to the [`spawn`] function is a *tag*, an arbitrary piece of data associated |
| 45 | //! 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] | 46 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 47 | //! 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] | 48 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 49 | //! # Execution |
| 50 | //! |
| 51 | //! Task executors have some kind of main loop that drives tasks to completion. That means taking |
| 52 | //! runnable tasks out of the queue and running each one in order: |
| 53 | //! |
| 54 | //! ```no_run |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 55 | //! # let (sender, receiver) = crossbeam::channel::unbounded(); |
| 56 | //! # |
| 57 | //! # // A future that will get spawned. |
| 58 | //! # let future = async { 1 + 2 }; |
| 59 | //! # |
| 60 | //! # // A function that schedules the task when it gets woken up. |
| 61 | //! # let schedule = move |task| sender.send(task).unwrap(); |
| 62 | //! # |
| 63 | //! # // Construct a task. |
| 64 | //! # let (task, handle) = async_task::spawn(future, schedule, ()); |
| 65 | //! # |
| 66 | //! # // Push the task into the queue by invoking its schedule function. |
| 67 | //! # task.schedule(); |
| 68 | //! # |
| 69 | //! for task in receiver { |
| 70 | //! task.run(); |
| 71 | //! } |
| 72 | //! ``` |
| 73 | //! |
| 74 | //! When a task is run, its future gets polled. If polling does not complete the task, that means |
| 75 | //! it's waiting for another future and needs to go to sleep. When woken up, its schedule function |
| 76 | //! 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] | 77 | //! |
| 78 | //! # Cancellation |
| 79 | //! |
Stjepan Glavina | 5c398cf | 2019-08-20 15:29:43 +0200 | [diff] [blame] | 80 | //! Both [`Task`] and [`JoinHandle`] have methods that cancel the task. When cancelled, the task's |
| 81 | //! future will not be polled again and will get dropped instead. |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 82 | //! |
| 83 | //! If cancelled by the [`Task`] instance, the task is destroyed immediately. If cancelled by the |
| 84 | //! [`JoinHandle`] instance, it will be scheduled one more time and the next attempt to run it will |
| 85 | //! simply destroy it. |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 86 | //! |
| 87 | //! # Performance |
| 88 | //! |
Stjepan Glavina | 5c398cf | 2019-08-20 15:29:43 +0200 | [diff] [blame] | 89 | //! Task construction incurs a single allocation that holds its state, the schedule function, and |
| 90 | //! the future or the result of the future if completed. |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 91 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 92 | //! The layout of a task is equivalent to 4 words followed by the schedule function, and then by a |
| 93 | //! union of the future and its output. |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 94 | //! |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 95 | //! [`spawn`]: fn.spawn.html |
Stjepan Glavina | fcfa4ab | 2019-11-25 18:39:17 +0100 | [diff] [blame] | 96 | //! [`spawn_local`]: fn.spawn_local.html |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 97 | //! [`Task`]: struct.Task.html |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 98 | //! [`JoinHandle`]: struct.JoinHandle.html |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 99 | |
Stjepan Glavina | 921e8a0 | 2020-01-06 14:31:28 -0600 | [diff] [blame] | 100 | #![no_std] |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 101 | #![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)] |
Stjepan Glavina | 7a8962b | 2019-08-16 11:25:25 +0200 | [diff] [blame] | 102 | #![doc(test(attr(deny(rust_2018_idioms, warnings))))] |
| 103 | #![doc(test(attr(allow(unused_extern_crates, unused_variables))))] |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 104 | |
Stjepan Glavina | 921e8a0 | 2020-01-06 14:31:28 -0600 | [diff] [blame] | 105 | extern crate alloc; |
| 106 | |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 107 | mod header; |
| 108 | mod join_handle; |
| 109 | mod raw; |
| 110 | mod state; |
| 111 | mod task; |
| 112 | mod utils; |
| 113 | |
| 114 | pub use crate::join_handle::JoinHandle; |
Stjepan Glavina | fcfa4ab | 2019-11-25 18:39:17 +0100 | [diff] [blame] | 115 | pub use crate::task::{spawn, spawn_local, Task}; |