diff options
| author | bors <bors@rust-lang.org> | 2023-09-22 10:34:45 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-09-22 10:34:45 +0000 |
| commit | 03c199af8e0f10c4fe4ead8e97e65286bef86e7d (patch) | |
| tree | 9e771d0cbd662db18ef1cc7aaea1316a2e47307b /library/std/src/sys/unix/thread.rs | |
| parent | 5a4e47ebedb4132168c1b22262f21f0d3a2a96df (diff) | |
| parent | 1a18ec0dcf88f35b4682e7f630cbd3b167b56e92 (diff) | |
| download | rust-03c199af8e0f10c4fe4ead8e97e65286bef86e7d.tar.gz rust-03c199af8e0f10c4fe4ead8e97e65286bef86e7d.zip | |
Auto merge of #116054 - matthiaskrgr:rollup-3pusno6, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - #114379 (Command: also print removed env vars) - #116034 (add UI test for delimiter errors) - #116036 (tests/ui: Split large_moves.rs and move to lint/large_assignments) - #116038 (Fall back to _SC_NPROCESSORS_ONLN if sched_getaffinity returns an empty mask) - #116039 (Account for nested `impl Trait` in TAIT) - #116041 (Add note to `is_known_rigid`) - #116049 (give FutureIncompatibilityReason variants more explicit names) r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'library/std/src/sys/unix/thread.rs')
| -rw-r--r-- | library/std/src/sys/unix/thread.rs | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/library/std/src/sys/unix/thread.rs b/library/std/src/sys/unix/thread.rs index 568630daf38..311ed95022f 100644 --- a/library/std/src/sys/unix/thread.rs +++ b/library/std/src/sys/unix/thread.rs @@ -318,25 +318,38 @@ pub fn available_parallelism() -> io::Result<NonZeroUsize> { target_os = "solaris", target_os = "illumos", ))] { + #[allow(unused_assignments)] + #[allow(unused_mut)] + let mut quota = usize::MAX; + #[cfg(any(target_os = "android", target_os = "linux"))] { - let quota = cgroups::quota().max(1); + quota = cgroups::quota().max(1); let mut set: libc::cpu_set_t = unsafe { mem::zeroed() }; unsafe { if libc::sched_getaffinity(0, mem::size_of::<libc::cpu_set_t>(), &mut set) == 0 { let count = libc::CPU_COUNT(&set) as usize; let count = count.min(quota); - // reported to occur on MIPS kernels older than our minimum supported kernel version for those targets - let count = NonZeroUsize::new(count) - .expect("CPU count must be > 0. This may be a bug in sched_getaffinity(); try upgrading the kernel."); - return Ok(count); + + // According to sched_getaffinity's API it should always be non-zero, but + // some old MIPS kernels were buggy and zero-initialized the mask if + // none was explicitly set. + // In that case we use the sysconf fallback. + if let Some(count) = NonZeroUsize::new(count) { + return Ok(count) + } } } } match unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) } { -1 => Err(io::Error::last_os_error()), 0 => Err(io::const_io_error!(io::ErrorKind::NotFound, "The number of hardware threads is not known for the target platform")), - cpus => Ok(unsafe { NonZeroUsize::new_unchecked(cpus as usize) }), + cpus => { + let count = cpus as usize; + // Cover the unusual situation where we were able to get the quota but not the affinity mask + let count = count.min(quota); + Ok(unsafe { NonZeroUsize::new_unchecked(count) }) + } } } else if #[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd"))] { use crate::ptr; |
