Skip to content

Commit fe6514e

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 f2ba411 commit fe6514e

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

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

+11-22
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
350350
target_os = "hurd",
351351
target_os = "linux",
352352
target_os = "aix",
353+
target_os = "freebsd",
353354
target_vendor = "apple",
354355
))] {
355356
#[allow(unused_assignments)]
@@ -359,9 +360,17 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
359360
#[cfg(any(target_os = "android", target_os = "linux"))]
360361
{
361362
quota = cgroups::quota().max(1);
362-
let mut set: libc::cpu_set_t = unsafe { mem::zeroed() };
363+
}
364+
365+
#[cfg(any(target_os = "android", target_os = "linux", target_os = "freebsd"))]
366+
{
367+
#[cfg(not(target_os = "freebsd"))]
368+
type cpuset = libc::cpu_set_t;
369+
#[cfg(target_os = "freebsd")]
370+
type cpuset = libc::cpuset_t;
371+
let mut set: cpuset = unsafe { mem::zeroed() };
363372
unsafe {
364-
if libc::sched_getaffinity(0, mem::size_of::<libc::cpu_set_t>(), &mut set) == 0 {
373+
if libc::sched_getaffinity(0, mem::size_of::<cpuset>(), &mut set) == 0 {
365374
let count = libc::CPU_COUNT(&set) as usize;
366375
let count = count.min(quota);
367376

@@ -386,32 +395,12 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
386395
}
387396
}
388397
} else if #[cfg(any(
389-
target_os = "freebsd",
390398
target_os = "dragonfly",
391399
target_os = "openbsd",
392400
target_os = "netbsd",
393401
))] {
394402
use crate::ptr;
395403

396-
#[cfg(target_os = "freebsd")]
397-
{
398-
let mut set: libc::cpuset_t = unsafe { mem::zeroed() };
399-
unsafe {
400-
if libc::cpuset_getaffinity(
401-
libc::CPU_LEVEL_WHICH,
402-
libc::CPU_WHICH_PID,
403-
-1,
404-
mem::size_of::<libc::cpuset_t>(),
405-
&mut set,
406-
) == 0 {
407-
let count = libc::CPU_COUNT(&set) as usize;
408-
if count > 0 {
409-
return Ok(NonZero::new_unchecked(count));
410-
}
411-
}
412-
}
413-
}
414-
415404
#[cfg(target_os = "netbsd")]
416405
{
417406
unsafe {

0 commit comments

Comments
 (0)