Skip to content

Commit 8e12778

Browse files
committed
std::thread::available_parallelism merging linux/android/freebsd version
FreeBSD 13.1 had introduced a sched cpu affinity compatibility layer with Linux. 13.0 and even 13.1 being EOL, we can simplify here.
1 parent bf3c6c5 commit 8e12778

File tree

1 file changed

+16
-30
lines changed

1 file changed

+16
-30
lines changed

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

+16-30
Original file line numberDiff line numberDiff line change
@@ -320,19 +320,22 @@ pub fn available_parallelism() -> io::Result<NonZeroUsize> {
320320
target_os = "solaris",
321321
target_os = "illumos",
322322
target_os = "aix",
323+
target_os = "freebsd",
323324
))] {
324-
#[allow(unused_assignments)]
325-
#[allow(unused_mut)]
326-
let mut quota = usize::MAX;
327-
328-
#[cfg(any(target_os = "android", target_os = "linux"))]
325+
#[cfg(any(target_os = "android", target_os = "linux", target_os = "freebsd"))]
329326
{
330-
quota = cgroups::quota().max(1);
331-
let mut set: libc::cpu_set_t = unsafe { mem::zeroed() };
327+
#[cfg(not(target_os = "freebsd"))]
328+
type cpuset = libc::cpu_set_t;
329+
#[cfg(target_os = "freebsd")]
330+
type cpuset = libc::cpuset_t;
331+
let mut set: cpuset = unsafe { mem::zeroed() };
332332
unsafe {
333-
if libc::sched_getaffinity(0, mem::size_of::<libc::cpu_set_t>(), &mut set) == 0 {
333+
if libc::sched_getaffinity(0, mem::size_of::<cpuset>(), &mut set) == 0 {
334334
let count = libc::CPU_COUNT(&set) as usize;
335-
let count = count.min(quota);
335+
#[cfg(not(target_os = "freebsd"))]
336+
let count = count.min(cgroups::quota().max(1));
337+
#[cfg(target_os = "freebsd")]
338+
let count = count.min(usize::MAX);
336339

337340
// According to sched_getaffinity's API it should always be non-zero, but
338341
// some old MIPS kernels were buggy and zero-initialized the mask if
@@ -350,37 +353,20 @@ pub fn available_parallelism() -> io::Result<NonZeroUsize> {
350353
cpus => {
351354
let count = cpus as usize;
352355
// Cover the unusual situation where we were able to get the quota but not the affinity mask
353-
let count = count.min(quota);
356+
#[cfg(any(target_os = "linux", target_os = "android"))]
357+
let count = count.min(cgroups::quota().max(1));
358+
#[cfg(not(any(target_os = "linux", target_os = "android")))]
359+
let count = count.min(usize::MAX);
354360
Ok(unsafe { NonZeroUsize::new_unchecked(count) })
355361
}
356362
}
357363
} else if #[cfg(any(
358-
target_os = "freebsd",
359364
target_os = "dragonfly",
360365
target_os = "openbsd",
361366
target_os = "netbsd",
362367
))] {
363368
use crate::ptr;
364369

365-
#[cfg(target_os = "freebsd")]
366-
{
367-
let mut set: libc::cpuset_t = unsafe { mem::zeroed() };
368-
unsafe {
369-
if libc::cpuset_getaffinity(
370-
libc::CPU_LEVEL_WHICH,
371-
libc::CPU_WHICH_PID,
372-
-1,
373-
mem::size_of::<libc::cpuset_t>(),
374-
&mut set,
375-
) == 0 {
376-
let count = libc::CPU_COUNT(&set) as usize;
377-
if count > 0 {
378-
return Ok(NonZeroUsize::new_unchecked(count));
379-
}
380-
}
381-
}
382-
}
383-
384370
#[cfg(target_os = "netbsd")]
385371
{
386372
unsafe {

0 commit comments

Comments
 (0)