Skip to content

Commit

Permalink
Rollup merge of #110830 - Freaky:freebsd-cpuset, r=thomcc
Browse files Browse the repository at this point in the history
Add FreeBSD cpuset support to `std::thread::available_concurrency`

Use libc::cpuset_getaffinity to determine the CPUs available to the current process.

The existing sysconf and sysctl paths are left as fallback.
  • Loading branch information
JohnTitor authored May 6, 2023
2 parents 30220be + e5e640c commit 3d9a1de
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions library/std/src/sys/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,25 @@ pub fn available_parallelism() -> io::Result<NonZeroUsize> {
} else if #[cfg(any(target_os = "freebsd", target_os = "dragonfly", 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));
}
}
}
}

let mut cpus: libc::c_uint = 0;
let mut cpus_size = crate::mem::size_of_val(&cpus);

Expand Down

0 comments on commit 3d9a1de

Please sign in to comment.