Skip to content

Commit 2e9c239

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 dfa88b3 commit 2e9c239

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
@@ -320,6 +320,7 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
320320
target_os = "solaris",
321321
target_os = "illumos",
322322
target_os = "aix",
323+
target_os = "freebsd",
323324
))] {
324325
#[allow(unused_assignments)]
325326
#[allow(unused_mut)]
@@ -328,9 +329,17 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
328329
#[cfg(any(target_os = "android", target_os = "linux"))]
329330
{
330331
quota = cgroups::quota().max(1);
331-
let mut set: libc::cpu_set_t = unsafe { mem::zeroed() };
332+
}
333+
334+
#[cfg(any(target_os = "android", target_os = "linux", target_os = "freebsd"))]
335+
{
336+
#[cfg(not(target_os = "freebsd"))]
337+
type cpuset = libc::cpu_set_t;
338+
#[cfg(target_os = "freebsd")]
339+
type cpuset = libc::cpuset_t;
340+
let mut set: cpuset = unsafe { mem::zeroed() };
332341
unsafe {
333-
if libc::sched_getaffinity(0, mem::size_of::<libc::cpu_set_t>(), &mut set) == 0 {
342+
if libc::sched_getaffinity(0, mem::size_of::<cpuset>(), &mut set) == 0 {
334343
let count = libc::CPU_COUNT(&set) as usize;
335344
let count = count.min(quota);
336345

@@ -355,32 +364,12 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
355364
}
356365
}
357366
} else if #[cfg(any(
358-
target_os = "freebsd",
359367
target_os = "dragonfly",
360368
target_os = "openbsd",
361369
target_os = "netbsd",
362370
))] {
363371
use crate::ptr;
364372

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(NonZero::new_unchecked(count));
379-
}
380-
}
381-
}
382-
}
383-
384373
#[cfg(target_os = "netbsd")]
385374
{
386375
unsafe {

0 commit comments

Comments
 (0)