Skip to content

Commit 45a3bf4

Browse files
committed
take into account the num of processes by ulimit
1 parent 1b0bc59 commit 45a3bf4

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

library/std/src/sys/pal/unix/thread.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,13 +440,24 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
440440
}
441441
}
442442
}
443+
444+
let mut ulimit = u64::MAX;
445+
#[cfg(any(target_os = "android", target_os = "linux"))]
446+
{
447+
let mut r: libc::rlimit = unsafe { mem::zeroed() };
448+
unsafe {
449+
if libc::getrlimit(libc::RLIMIT_NPROC, &mut r) == 0 {
450+
ulimit = r.rlim_max
451+
}
452+
}
453+
}
443454
match unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) } {
444455
-1 => Err(io::Error::last_os_error()),
445456
0 => Err(io::Error::UNKNOWN_THREAD_COUNT),
446457
cpus => {
447458
let count = cpus as usize;
448459
// Cover the unusual situation where we were able to get the quota but not the affinity mask
449-
let count = count.min(quota);
460+
let count = count.min(quota.min(ulimit.try_into().unwrap_or(usize::MAX)));
450461
Ok(unsafe { NonZero::new_unchecked(count) })
451462
}
452463
}

0 commit comments

Comments
 (0)