Jason Macnak | f4a8bd2 | 2020-03-19 20:54:21 +0000 | [diff] [blame] | 1 | //! Built-in executors and related tools. |
| 2 | //! |
Haibo Huang | edb6aa2 | 2021-02-09 17:06:48 -0800 | [diff] [blame] | 3 | //! All asynchronous computation occurs within an executor, which is |
| 4 | //! capable of spawning futures as tasks. This module provides several |
| 5 | //! built-in executors, as well as tools for building your own. |
| 6 | //! |
| 7 | //! All items are only available when the `std` feature of this |
Jason Macnak | f4a8bd2 | 2020-03-19 20:54:21 +0000 | [diff] [blame] | 8 | //! library is activated, and it is activated by default. |
Haibo Huang | edb6aa2 | 2021-02-09 17:06:48 -0800 | [diff] [blame] | 9 | //! |
| 10 | //! # Using a thread pool (M:N task scheduling) |
| 11 | //! |
| 12 | //! Most of the time tasks should be executed on a [thread pool](ThreadPool). |
| 13 | //! A small set of worker threads can handle a very large set of spawned tasks |
| 14 | //! (which are much lighter weight than threads). Tasks spawned onto the pool |
| 15 | //! with the [`spawn_ok`](ThreadPool::spawn_ok) function will run ambiently on |
| 16 | //! the created threads. |
| 17 | //! |
| 18 | //! # Spawning additional tasks |
| 19 | //! |
| 20 | //! Tasks can be spawned onto a spawner by calling its [`spawn_obj`] method |
| 21 | //! directly. In the case of `!Send` futures, [`spawn_local_obj`] can be used |
| 22 | //! instead. |
| 23 | //! |
| 24 | //! # Single-threaded execution |
| 25 | //! |
| 26 | //! In addition to thread pools, it's possible to run a task (and the tasks |
| 27 | //! it spawns) entirely within a single thread via the [`LocalPool`] executor. |
| 28 | //! Aside from cutting down on synchronization costs, this executor also makes |
| 29 | //! it possible to spawn non-`Send` tasks, via [`spawn_local_obj`]. The |
| 30 | //! [`LocalPool`] is best suited for running I/O-bound tasks that do relatively |
| 31 | //! little work between I/O operations. |
| 32 | //! |
| 33 | //! There is also a convenience function [`block_on`] for simply running a |
| 34 | //! future to completion on the current thread. |
| 35 | //! |
| 36 | //! [`spawn_obj`]: https://docs.rs/futures/0.3/futures/task/trait.Spawn.html#tymethod.spawn_obj |
| 37 | //! [`spawn_local_obj`]: https://docs.rs/futures/0.3/futures/task/trait.LocalSpawn.html#tymethod.spawn_local_obj |
Jason Macnak | f4a8bd2 | 2020-03-19 20:54:21 +0000 | [diff] [blame] | 38 | |
| 39 | #![cfg_attr(not(feature = "std"), no_std)] |
Joel Galenson | 9b8ea34 | 2021-08-09 10:30:18 -0700 | [diff] [blame] | 40 | #![warn( |
| 41 | missing_debug_implementations, |
| 42 | missing_docs, |
| 43 | rust_2018_idioms, |
| 44 | single_use_lifetimes, |
| 45 | unreachable_pub |
| 46 | )] |
| 47 | #![doc(test( |
| 48 | no_crate_inject, |
| 49 | attr( |
| 50 | deny(warnings, rust_2018_idioms, single_use_lifetimes), |
| 51 | allow(dead_code, unused_assignments, unused_variables) |
| 52 | ) |
| 53 | ))] |
Chih-Hung Hsieh | 3bb9be1 | 2020-10-25 23:16:21 -0700 | [diff] [blame] | 54 | #![cfg_attr(docsrs, feature(doc_cfg))] |
Jason Macnak | f4a8bd2 | 2020-03-19 20:54:21 +0000 | [diff] [blame] | 55 | |
| 56 | #[cfg(feature = "std")] |
| 57 | mod local_pool; |
| 58 | #[cfg(feature = "std")] |
| 59 | pub use crate::local_pool::{block_on, block_on_stream, BlockingStream, LocalPool, LocalSpawner}; |
| 60 | |
| 61 | #[cfg(feature = "thread-pool")] |
Chih-Hung Hsieh | 3bb9be1 | 2020-10-25 23:16:21 -0700 | [diff] [blame] | 62 | #[cfg_attr(docsrs, doc(cfg(feature = "thread-pool")))] |
Jason Macnak | f4a8bd2 | 2020-03-19 20:54:21 +0000 | [diff] [blame] | 63 | #[cfg(feature = "std")] |
| 64 | mod thread_pool; |
| 65 | #[cfg(feature = "thread-pool")] |
Joel Galenson | 6c2d5a3 | 2021-05-19 15:17:28 -0700 | [diff] [blame] | 66 | #[cfg(feature = "std")] |
| 67 | mod unpark_mutex; |
| 68 | #[cfg(feature = "thread-pool")] |
Chih-Hung Hsieh | 3bb9be1 | 2020-10-25 23:16:21 -0700 | [diff] [blame] | 69 | #[cfg_attr(docsrs, doc(cfg(feature = "thread-pool")))] |
Jason Macnak | f4a8bd2 | 2020-03-19 20:54:21 +0000 | [diff] [blame] | 70 | #[cfg(feature = "std")] |
| 71 | pub use crate::thread_pool::{ThreadPool, ThreadPoolBuilder}; |
| 72 | |
| 73 | #[cfg(feature = "std")] |
| 74 | mod enter; |
| 75 | #[cfg(feature = "std")] |
| 76 | pub use crate::enter::{enter, Enter, EnterError}; |