diff --git a/crates/bevy_core/src/task_pool_options.rs b/crates/bevy_core/src/task_pool_options.rs index af910b1138bce..43779a072c0a8 100644 --- a/crates/bevy_core/src/task_pool_options.rs +++ b/crates/bevy_core/src/task_pool_options.rs @@ -94,8 +94,8 @@ impl DefaultTaskPoolOptions { /// Inserts the default thread pools into the given resource map based on the configured values pub fn create_default_pools(&self) { - let total_threads = - bevy_tasks::logical_core_count().clamp(self.min_total_threads, self.max_total_threads); + let total_threads = bevy_tasks::available_parallelism() + .clamp(self.min_total_threads, self.max_total_threads); trace!("Assigning {} cores to default task pools", total_threads); let mut remaining_threads = total_threads; diff --git a/crates/bevy_tasks/Cargo.toml b/crates/bevy_tasks/Cargo.toml index 41866500492a5..91f1e8e5e334c 100644 --- a/crates/bevy_tasks/Cargo.toml +++ b/crates/bevy_tasks/Cargo.toml @@ -12,7 +12,6 @@ keywords = ["bevy"] futures-lite = "1.4.0" async-executor = "1.3.0" async-channel = "1.4.2" -num_cpus = "1" once_cell = "1.7" [target.'cfg(target_arch = "wasm32")'.dependencies] diff --git a/crates/bevy_tasks/src/lib.rs b/crates/bevy_tasks/src/lib.rs index 7345d775ee968..62c42a7717868 100644 --- a/crates/bevy_tasks/src/lib.rs +++ b/crates/bevy_tasks/src/lib.rs @@ -33,5 +33,16 @@ pub mod prelude { }; } -pub use num_cpus::get as logical_core_count; -pub use num_cpus::get_physical as physical_core_count; +use std::num::NonZeroUsize; + +/// Gets the logical CPU core count available to the current process. +/// +/// This is identical to [`std::thread::available_parallelism`], except +/// it will return a default value of 1 if it internally errors out. +/// +/// This will always return at least 1. +pub fn available_parallelism() -> usize { + std::thread::available_parallelism() + .map(NonZeroUsize::get) + .unwrap_or(1) +} diff --git a/crates/bevy_tasks/src/task_pool.rs b/crates/bevy_tasks/src/task_pool.rs index b52ce0b9f1445..c9c97322f56f7 100644 --- a/crates/bevy_tasks/src/task_pool.rs +++ b/crates/bevy_tasks/src/task_pool.rs @@ -95,7 +95,7 @@ impl TaskPool { let executor = Arc::new(async_executor::Executor::new()); - let num_threads = num_threads.unwrap_or_else(num_cpus::get); + let num_threads = num_threads.unwrap_or_else(crate::available_parallelism); let threads = (0..num_threads) .map(|i| {