Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 12 additions & 26 deletions src/uu/nproc/src/nproc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,41 +106,27 @@ pub fn uu_app() -> Command {
)
}

#[cfg(any(
target_os = "linux",
target_vendor = "apple",
target_os = "freebsd",
target_os = "netbsd"
))]
#[cfg(unix)]
fn num_cpus_all() -> usize {
let nprocs = unsafe { libc::sysconf(libc::_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`.
available_parallelism()
} else if nprocs > 0 {
nprocs as usize
} else {
1
}
// In some situation, /proc and /sys are not mounted, and sysconf returns 1.
// However, we want to guarantee that `nproc --all` >= `nproc`.
unsafe { libc::sysconf(libc::_SC_NPROCESSORS_CONF) }
.try_into()
.ok()
.filter(|&n: &isize| n > 1)
.map_or_else(available_parallelism, |n| n as usize)
}

// Other platforms (e.g., windows), available_parallelism() directly.
#[cfg(not(any(
target_os = "linux",
target_vendor = "apple",
target_os = "freebsd",
target_os = "netbsd"
)))]
#[cfg(not(unix))]
fn num_cpus_all() -> usize {
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,
}
thread::available_parallelism()
.map(std::num::NonZeroUsize::get)
.unwrap_or(1)
}
Loading