Skip to content

Commit e5e640c

Browse files
committed
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.
1 parent 20d90b1 commit e5e640c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

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

+19
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,25 @@ pub fn available_parallelism() -> io::Result<NonZeroUsize> {
326326
} else if #[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd"))] {
327327
use crate::ptr;
328328

329+
#[cfg(target_os = "freebsd")]
330+
{
331+
let mut set: libc::cpuset_t = unsafe { mem::zeroed() };
332+
unsafe {
333+
if libc::cpuset_getaffinity(
334+
libc::CPU_LEVEL_WHICH,
335+
libc::CPU_WHICH_PID,
336+
-1,
337+
mem::size_of::<libc::cpuset_t>(),
338+
&mut set,
339+
) == 0 {
340+
let count = libc::CPU_COUNT(&set) as usize;
341+
if count > 0 {
342+
return Ok(NonZeroUsize::new_unchecked(count));
343+
}
344+
}
345+
}
346+
}
347+
329348
let mut cpus: libc::c_uint = 0;
330349
let mut cpus_size = crate::mem::size_of_val(&cpus);
331350

0 commit comments

Comments
 (0)