Skip to content

Commit

Permalink
Rollup merge of rust-lang#120589 - devnexen:cpuaff_fbsd_upd, r=m-ou-se
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
matthiaskrgr committed Feb 8, 2024
2 parents 9293bbc + 0bfcafd commit 8038608
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions library/std/src/sys/pal/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ pub fn available_parallelism() -> io::Result<NonZeroUsize> {
target_os = "solaris",
target_os = "illumos",
target_os = "aix",
target_os = "freebsd",
))] {
#[allow(unused_assignments)]
#[allow(unused_mut)]
Expand All @@ -328,9 +329,17 @@ pub fn available_parallelism() -> io::Result<NonZeroUsize> {
#[cfg(any(target_os = "android", target_os = "linux"))]
{
quota = cgroups::quota().max(1);
let mut set: libc::cpu_set_t = unsafe { mem::zeroed() };
}

#[cfg(any(target_os = "android", target_os = "linux", target_os = "freebsd"))]
{
#[cfg(not(target_os = "freebsd"))]
type cpuset = libc::cpu_set_t;
#[cfg(target_os = "freebsd")]
type cpuset = libc::cpuset_t;
let mut set: cpuset = unsafe { mem::zeroed() };
unsafe {
if libc::sched_getaffinity(0, mem::size_of::<libc::cpu_set_t>(), &mut set) == 0 {
if libc::sched_getaffinity(0, mem::size_of::<cpuset>(), &mut set) == 0 {
let count = libc::CPU_COUNT(&set) as usize;
let count = count.min(quota);

Expand All @@ -355,32 +364,12 @@ pub fn available_parallelism() -> io::Result<NonZeroUsize> {
}
}
} else if #[cfg(any(
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "netbsd",
))] {
use crate::ptr;

#[cfg(target_os = "freebsd")]
{
let mut set: libc::cpuset_t = unsafe { mem::zeroed() };
unsafe {
if libc::cpuset_getaffinity(
libc::CPU_LEVEL_WHICH,
libc::CPU_WHICH_PID,
-1,
mem::size_of::<libc::cpuset_t>(),
&mut set,
) == 0 {
let count = libc::CPU_COUNT(&set) as usize;
if count > 0 {
return Ok(NonZeroUsize::new_unchecked(count));
}
}
}
}

#[cfg(target_os = "netbsd")]
{
unsafe {
Expand Down

0 comments on commit 8038608

Please sign in to comment.