Skip to main content

bevy_tasks/
lib.rs

1#![doc = include_str!("../README.md")]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3#![doc(
4    html_logo_url = "https://bevy.org/assets/icon.png",
5    html_favicon_url = "https://bevy.org/assets/icon.png"
6)]
7#![no_std]
8
9/// Configuration information for this crate.
10pub mod cfg {
11    pub(crate) use bevy_platform::cfg::*;
12
13    pub use bevy_platform::cfg::{alloc, std, web};
14
15    define_alias! {
16        #[cfg(feature = "async_executor")] => {
17            /// Indicates `async_executor` is used as the future execution backend.
18            async_executor
19        }
20
21        #[cfg(all(not(target_arch = "wasm32"), feature = "multi_threaded"))] => {
22            /// Indicates multithreading support.
23            multi_threaded
24        }
25
26        #[cfg(target_arch = "wasm32")] => {
27            /// Indicates the current target requires additional `Send` bounds.
28            conditional_send
29        }
30
31    }
32}
33
34cfg::std! {
35    extern crate std;
36}
37
38extern crate alloc;
39
40cfg::conditional_send! {
41    if {
42        /// Use [`ConditionalSend`] to mark an optional Send trait bound. Useful as on certain platforms (eg. Wasm),
43        /// futures aren't Send.
44        pub trait ConditionalSend {}
45        impl<T> ConditionalSend for T {}
46    } else {
47        /// Use [`ConditionalSend`] to mark an optional Send trait bound. Useful as on certain platforms (eg. Wasm),
48        /// futures aren't Send.
49        pub trait ConditionalSend: Send {}
50        impl<T: Send> ConditionalSend for T {}
51    }
52}
53
54/// Use [`ConditionalSendFuture`] for a future with an optional Send trait bound, as on certain platforms (eg. Wasm),
55/// futures aren't Send.
56pub trait ConditionalSendFuture: Future + ConditionalSend {}
57
58impl<T: Future + ConditionalSend> ConditionalSendFuture for T {}
59
60use alloc::boxed::Box;
61
62/// An owned and dynamically typed Future used when you can't statically type your result or need to add some indirection.
63pub type BoxedFuture<'a, T> = core::pin::Pin<Box<dyn ConditionalSendFuture<Output = T> + 'a>>;
64
65// Modules
66mod executor;
67pub mod futures;
68mod iter;
69mod slice;
70mod usages;
71
72cfg::async_executor! {
73    if {} else {
74        mod edge_executor;
75    }
76}
77
78// Exports
79pub use async_task::Task;
80pub use iter::ParallelIterator;
81pub use slice::{ParallelSlice, ParallelSliceMut};
82pub use usages::{AsyncComputeTaskPool, ComputeTaskPool, IoTaskPool};
83
84pub use futures_lite;
85pub use futures_lite::future::poll_once;
86
87cfg::web! {
88    if {} else {
89        pub use usages::tick_global_task_pools_on_main_thread;
90    }
91}
92
93cfg::multi_threaded! {
94    if {
95        mod task_pool;
96        mod thread_executor;
97
98        pub use task_pool::{Scope, TaskPool, TaskPoolBuilder};
99        pub use thread_executor::{ThreadExecutor, ThreadExecutorTicker};
100    } else {
101        mod single_threaded_task_pool;
102
103        pub use single_threaded_task_pool::{Scope, TaskPool, TaskPoolBuilder, ThreadExecutor};
104    }
105}
106
107pub use bevy_platform::future::block_on;
108
109/// The tasks prelude.
110///
111/// This includes the most common types in this crate, re-exported for your convenience.
112pub mod prelude {
113    #[doc(hidden)]
114    pub use crate::{
115        block_on,
116        iter::ParallelIterator,
117        slice::{ParallelSlice, ParallelSliceMut},
118        usages::{AsyncComputeTaskPool, ComputeTaskPool, IoTaskPool},
119    };
120}
121
122/// Gets the logical CPU core count available to the current process.
123///
124/// This is identical to `std::thread::available_parallelism`, except
125/// it will return a default value of 1 if it internally errors out.
126///
127/// This will always return at least 1.
128pub fn available_parallelism() -> usize {
129    cfg::switch! {{
130        cfg::std => {
131            std::thread::available_parallelism()
132                .map(core::num::NonZero::<usize>::get)
133                .unwrap_or(1)
134        }
135        _ => {
136            1
137        }
138    }}
139}