Skip to content

Commit 6f9f448

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 b2728d5 commit 6f9f448

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
@@ -353,6 +353,7 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
353353
target_os = "hurd",
354354
target_os = "linux",
355355
target_os = "aix",
356+
target_os = "freebsd",
356357
target_vendor = "apple",
357358
))] {
358359
#[allow(unused_assignments)]
@@ -362,9 +363,17 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
362363
#[cfg(any(target_os = "android", target_os = "linux"))]
363364
{
364365
quota = cgroups::quota().max(1);
365-
let mut set: libc::cpu_set_t = unsafe { mem::zeroed() };
366+
}
367+
368+
#[cfg(any(target_os = "android", target_os = "linux", target_os = "freebsd"))]
369+
{
370+
#[cfg(not(target_os = "freebsd"))]
371+
type cpuset = libc::cpu_set_t;
372+
#[cfg(target_os = "freebsd")]
373+
type cpuset = libc::cpuset_t;
374+
let mut set: cpuset = unsafe { mem::zeroed() };
366375
unsafe {
367-
if libc::sched_getaffinity(0, mem::size_of::<libc::cpu_set_t>(), &mut set) == 0 {
376+
if libc::sched_getaffinity(0, mem::size_of::<cpuset>(), &mut set) == 0 {
368377
let count = libc::CPU_COUNT(&set) as usize;
369378
let count = count.min(quota);
370379

@@ -389,32 +398,12 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
389398
}
390399
}
391400
} else if #[cfg(any(
392-
target_os = "freebsd",
393401
target_os = "dragonfly",
394402
target_os = "openbsd",
395403
target_os = "netbsd",
396404
))] {
397405
use crate::ptr;
398406

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

0 commit comments

Comments
 (0)