mirror of
https://github.com/uutils/coreutils
synced 2024-12-13 06:42:42 +00:00
nproc: replace num_cpus crate with thread::available_parallelism (#4352)
* nproc: replace num_cpus crate with std:🧵:available_parallelism
* nproc: unwrap Result for Windows
* nproc: if thread::available_parallelism results in err return 1
* nproc: wrap the call to available_parallelism into a function
* nproc: remove comment in the wrong place
* nproc: fix style violation
* nproc: fix comment, refers to the new function
This commit is contained in:
parent
e8af2a1e67
commit
04b6d806a2
3 changed files with 16 additions and 9 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -2807,7 +2807,6 @@ version = "0.0.17"
|
|||
dependencies = [
|
||||
"clap",
|
||||
"libc",
|
||||
"num_cpus",
|
||||
"uucore",
|
||||
]
|
||||
|
||||
|
|
|
@ -16,7 +16,6 @@ path = "src/nproc.rs"
|
|||
|
||||
[dependencies]
|
||||
libc = { workspace=true }
|
||||
num_cpus = { workspace=true }
|
||||
clap = { workspace=true }
|
||||
uucore = { workspace=true, features=["fs"] }
|
||||
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue