diff --git a/Cargo.lock b/Cargo.lock index 3df301eac..5668a918e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2807,7 +2807,6 @@ version = "0.0.17" dependencies = [ "clap", "libc", - "num_cpus", "uucore", ] diff --git a/src/uu/nproc/Cargo.toml b/src/uu/nproc/Cargo.toml index dc51d8a55..c2c224a40 100644 --- a/src/uu/nproc/Cargo.toml +++ b/src/uu/nproc/Cargo.toml @@ -16,7 +16,6 @@ path = "src/nproc.rs" [dependencies] libc = { workspace=true } -num_cpus = { workspace=true } clap = { workspace=true } uucore = { workspace=true, features=["fs"] } diff --git a/src/uu/nproc/src/nproc.rs b/src/uu/nproc/src/nproc.rs index 960957df6..fbfeebb23 100644 --- a/src/uu/nproc/src/nproc.rs +++ b/src/uu/nproc/src/nproc.rs @@ -8,7 +8,7 @@ // spell-checker:ignore (ToDO) NPROCESSORS nprocs numstr threadstr sysconf use clap::{crate_version, Arg, ArgAction, Command}; -use std::env; +use std::{env, thread}; use uucore::display::Quotable; use uucore::error::{UResult, USimpleError}; use uucore::format_usage; @@ -73,16 +73,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { // If OMP_NUM_THREADS=0, rejects the value let thread: Vec<&str> = threadstr.split_terminator(',').collect(); match &thread[..] { - [] => num_cpus::get(), + [] => available_parallelism(), [s, ..] => match s.parse() { - Ok(0) | Err(_) => num_cpus::get(), + Ok(0) | Err(_) => available_parallelism(), Ok(n) => n, }, } } // the variable 'OMP_NUM_THREADS' doesn't exist // fallback to the regular CPU detection - Err(_) => num_cpus::get(), + Err(_) => available_parallelism(), } }; @@ -127,7 +127,7 @@ fn num_cpus_all() -> usize { if nprocs == 1 { // In some situation, /proc and /sys are not mounted, and sysconf returns 1. // However, we want to guarantee that `nproc --all` >= `nproc`. - num_cpus::get() + available_parallelism() } else if nprocs > 0 { nprocs as usize } else { @@ -135,7 +135,7 @@ fn num_cpus_all() -> usize { } } -// Other platforms (e.g., windows), num_cpus::get() directly. +// Other platforms (e.g., windows), available_parallelism() directly. #[cfg(not(any( target_os = "linux", target_vendor = "apple", @@ -143,5 +143,14 @@ fn num_cpus_all() -> usize { target_os = "netbsd" )))] fn num_cpus_all() -> usize { - num_cpus::get() + available_parallelism() +} + +// In some cases, thread::available_parallelism() may return an Err +// In this case, we will return 1 (like GNU) +fn available_parallelism() -> usize { + match thread::available_parallelism() { + Ok(n) => n.get(), + Err(_) => 1, + } }