mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 12:37:49 +00:00
nproc: fix conditional compilation
This commit is contained in:
parent
5e95d3752e
commit
a30d732463
1 changed files with 26 additions and 14 deletions
|
@ -12,7 +12,7 @@
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
extern crate num_cpus;
|
extern crate num_cpus;
|
||||||
|
|
||||||
#[cfg(not(windows))]
|
#[cfg(unix)]
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
|
@ -89,19 +89,7 @@ Print the number of cores available to the current process.", NAME, VERSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut cores = if matches.opt_present("all") {
|
let mut cores = if matches.opt_present("all") {
|
||||||
if cfg!(target_os = "linux") || cfg!(target_os = "macos") || cfg!(target_os = "freebsd") || cfg!(target_os = "netbsd") {
|
num_cpus_all()
|
||||||
let nprocs = unsafe { libc::sysconf(_SC_NPROCESSORS_CONF) };
|
|
||||||
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()
|
|
||||||
} else {
|
|
||||||
if nprocs > 0 { nprocs as usize } else { 1 }
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Other platform(e.g., windows), num_cpus::get() directly.
|
|
||||||
num_cpus::get()
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
num_cpus::get()
|
num_cpus::get()
|
||||||
};
|
};
|
||||||
|
@ -114,3 +102,27 @@ Print the number of cores available to the current process.", NAME, VERSION);
|
||||||
println!("{}", cores);
|
println!("{}", cores);
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(target_os = "linux",
|
||||||
|
target_os = "macos",
|
||||||
|
target_os = "freebsd",
|
||||||
|
target_os = "netbsd"))]
|
||||||
|
fn num_cpus_all() -> usize {
|
||||||
|
let nprocs = unsafe { libc::sysconf(_SC_NPROCESSORS_CONF) };
|
||||||
|
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()
|
||||||
|
} else {
|
||||||
|
if nprocs > 0 { nprocs as usize } else { 1 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Other platform(e.g., windows), num_cpus::get() directly.
|
||||||
|
#[cfg(not(any(target_os = "linux",
|
||||||
|
target_os = "macos",
|
||||||
|
target_os = "freebsd",
|
||||||
|
target_os = "netbsd")))]
|
||||||
|
fn num_cpus_all() -> usize {
|
||||||
|
num_cpus::get()
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue