From 08dc685afc94f0e39c662bed5d43ab437b6c157c Mon Sep 17 00:00:00 2001 From: Ryan Zoeller Date: Sat, 20 Aug 2022 16:36:05 -0500 Subject: [PATCH] Add sched_getaffinity and sched_setaffinity on FreeBSD --- CHANGELOG.md | 3 +++ src/sched.rs | 16 ++++++++++++---- test/test.rs | 1 + 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56ab09fff2..764222c59b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ This project adheres to [Semantic Versioning](https://semver.org/). ## [Unreleased] - ReleaseDate ### Added +- Added `sched_getaffinity` and `sched_setaffinity` on FreeBSD. + ([#1804](https://github.com/nix-rust/nix/pull/1804)) + ### Changed - The MSRV is now 1.56.1 diff --git a/src/sched.rs b/src/sched.rs index e9a326e261..943ed6ecb0 100644 --- a/src/sched.rs +++ b/src/sched.rs @@ -142,10 +142,10 @@ mod sched_linux_like { } } -#[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "linux"))] +#[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "freebsd", target_os = "linux"))] pub use self::sched_affinity::*; -#[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "linux"))] +#[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "freebsd", target_os = "linux"))] mod sched_affinity { use crate::errno::Errno; use std::mem; @@ -157,10 +157,13 @@ mod sched_affinity { /// sched_getaffinity for example. /// /// This is a wrapper around `libc::cpu_set_t`. - #[repr(C)] + #[repr(transparent)] #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pub struct CpuSet { + #[cfg(not(target_os = "freebsd"))] cpu_set: libc::cpu_set_t, + #[cfg(target_os = "freebsd")] + cpu_set: libc::cpuset_t, } impl CpuSet { @@ -205,7 +208,12 @@ mod sched_affinity { /// Return the maximum number of CPU in CpuSet pub const fn count() -> usize { - 8 * mem::size_of::() + #[cfg(not(target_os = "freebsd"))] + let bytes = mem::size_of::(); + #[cfg(target_os = "freebsd")] + let bytes = mem::size_of::(); + + 8 * bytes } } diff --git a/test/test.rs b/test/test.rs index f725ef97a0..06380379d8 100644 --- a/test/test.rs +++ b/test/test.rs @@ -36,6 +36,7 @@ mod test_resource; #[cfg(any( target_os = "android", target_os = "dragonfly", + target_os = "freebsd", target_os = "linux" ))] mod test_sched;