bevy_tasks/
lib.rs

1#![doc = include_str!("../README.md")]
2#![cfg_attr(docsrs, feature(doc_auto_cfg))]
3#![doc(
4    html_logo_url = "https://bevyengine.org/assets/icon.png",
5    html_favicon_url = "https://bevyengine.org/assets/icon.png"
6)]
7
8extern crate alloc;
9
10mod slice;
11pub use slice::{ParallelSlice, ParallelSliceMut};
12
13#[cfg_attr(target_arch = "wasm32", path = "wasm_task.rs")]
14mod task;
15
16pub use task::Task;
17
18#[cfg(all(not(target_arch = "wasm32"), feature = "multi_threaded"))]
19mod task_pool;
20
21#[cfg(all(not(target_arch = "wasm32"), feature = "multi_threaded"))]
22pub use task_pool::{Scope, TaskPool, TaskPoolBuilder};
23
24#[cfg(any(target_arch = "wasm32", not(feature = "multi_threaded")))]
25mod single_threaded_task_pool;
26
27#[cfg(any(target_arch = "wasm32", not(feature = "multi_threaded")))]
28pub use single_threaded_task_pool::{Scope, TaskPool, TaskPoolBuilder, ThreadExecutor};
29
30mod usages;
31#[cfg(not(target_arch = "wasm32"))]
32pub use usages::tick_global_task_pools_on_main_thread;
33pub use usages::{AsyncComputeTaskPool, ComputeTaskPool, IoTaskPool};
34
35#[cfg(all(not(target_arch = "wasm32"), feature = "multi_threaded"))]
36mod thread_executor;
37#[cfg(all(not(target_arch = "wasm32"), feature = "multi_threaded"))]
38pub use thread_executor::{ThreadExecutor, ThreadExecutorTicker};
39
40#[cfg(feature = "async-io")]
41pub use async_io::block_on;
42#[cfg(not(feature = "async-io"))]
43pub use futures_lite::future::block_on;
44pub use futures_lite::future::poll_once;
45
46mod iter;
47pub use iter::ParallelIterator;
48
49pub use futures_lite;
50
51/// The tasks prelude.
52///
53/// This includes the most common types in this crate, re-exported for your convenience.
54pub mod prelude {
55    #[doc(hidden)]
56    pub use crate::{
57        block_on,
58        iter::ParallelIterator,
59        slice::{ParallelSlice, ParallelSliceMut},
60        usages::{AsyncComputeTaskPool, ComputeTaskPool, IoTaskPool},
61    };
62}
63
64use core::num::NonZero;
65
66/// Gets the logical CPU core count available to the current process.
67///
68/// This is identical to [`std::thread::available_parallelism`], except
69/// it will return a default value of 1 if it internally errors out.
70///
71/// This will always return at least 1.
72pub fn available_parallelism() -> usize {
73    std::thread::available_parallelism()
74        .map(NonZero::<usize>::get)
75        .unwrap_or(1)
76}