Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_tasks/src/lib.rs
9356 views
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.
10
pub 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
34
cfg::std! {
35
extern crate std;
36
}
37
38
extern crate alloc;
39
40
cfg::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.
56
pub trait ConditionalSendFuture: Future + ConditionalSend {}
57
58
impl<T: Future + ConditionalSend> ConditionalSendFuture for T {}
59
60
use 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.
63
pub type BoxedFuture<'a, T> = core::pin::Pin<Box<dyn ConditionalSendFuture<Output = T> + 'a>>;
64
65
// Modules
66
mod executor;
67
pub mod futures;
68
mod iter;
69
mod slice;
70
mod task;
71
mod usages;
72
73
cfg::async_executor! {
74
if {} else {
75
mod edge_executor;
76
}
77
}
78
79
// Exports
80
pub use iter::ParallelIterator;
81
pub use slice::{ParallelSlice, ParallelSliceMut};
82
pub use task::Task;
83
pub use usages::{AsyncComputeTaskPool, ComputeTaskPool, IoTaskPool};
84
85
pub use futures_lite;
86
pub use futures_lite::future::poll_once;
87
88
cfg::web! {
89
if {} else {
90
pub use usages::tick_global_task_pools_on_main_thread;
91
}
92
}
93
94
cfg::multi_threaded! {
95
if {
96
mod task_pool;
97
mod thread_executor;
98
99
pub use task_pool::{Scope, TaskPool, TaskPoolBuilder};
100
pub use thread_executor::{ThreadExecutor, ThreadExecutorTicker};
101
} else {
102
mod single_threaded_task_pool;
103
104
pub use single_threaded_task_pool::{Scope, TaskPool, TaskPoolBuilder, ThreadExecutor};
105
}
106
}
107
108
pub use bevy_platform::future::block_on;
109
110
/// The tasks prelude.
111
///
112
/// This includes the most common types in this crate, re-exported for your convenience.
113
pub mod prelude {
114
#[doc(hidden)]
115
pub use crate::{
116
block_on,
117
iter::ParallelIterator,
118
slice::{ParallelSlice, ParallelSliceMut},
119
usages::{AsyncComputeTaskPool, ComputeTaskPool, IoTaskPool},
120
};
121
}
122
123
/// Gets the logical CPU core count available to the current process.
124
///
125
/// This is identical to `std::thread::available_parallelism`, except
126
/// it will return a default value of 1 if it internally errors out.
127
///
128
/// This will always return at least 1.
129
pub fn available_parallelism() -> usize {
130
cfg::switch! {{
131
cfg::std => {
132
std::thread::available_parallelism()
133
.map(core::num::NonZero::<usize>::get)
134
.unwrap_or(1)
135
}
136
_ => {
137
1
138
}
139
}}
140
}
141
142