diff options
| author | Yuki Okushi <jtitor@2k36.org> | 2023-05-06 09:09:32 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-06 09:09:32 +0900 |
| commit | 3d9a1de690c794fc72102d2de2324beeefe81322 (patch) | |
| tree | 3cebc537a25d8a021dbce9c92c1be4b41bff1ddd | |
| parent | 30220be9297e0deac837dca02ab091ecf54ef80f (diff) | |
| parent | e5e640cacef51e401286f93388002f41e8ef3328 (diff) | |
| download | rust-3d9a1de690c794fc72102d2de2324beeefe81322.tar.gz rust-3d9a1de690c794fc72102d2de2324beeefe81322.zip | |
Rollup merge of #110830 - Freaky:freebsd-cpuset, r=thomcc
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.
| -rw-r--r-- | library/std/src/sys/unix/thread.rs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/library/std/src/sys/unix/thread.rs b/library/std/src/sys/unix/thread.rs index 15070b1f6a7..7307d9b2c86 100644 --- a/library/std/src/sys/unix/thread.rs +++ b/library/std/src/sys/unix/thread.rs @@ -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); |
