Skip to content

Commit 986912d

Browse files
committed
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.
1 parent 791c041 commit 986912d

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
153153
target_os = "hurd",
154154
target_os = "linux",
155155
target_os = "aix",
156+
target_os = "freebsd",
156157
target_vendor = "apple",
157158
target_os = "cygwin",
158159
) => {
@@ -163,9 +164,17 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
163164
#[cfg(any(target_os = "android", target_os = "linux"))]
164165
{
165166
quota = cgroups::quota().max(1);
166-
let mut set: libc::cpu_set_t = unsafe { mem::zeroed() };
167+
}
168+
169+
#[cfg(any(target_os = "android", target_os = "linux", target_os = "freebsd"))]
170+
{
171+
#[cfg(not(target_os = "freebsd"))]
172+
type Cpuset = libc::cpu_set_t;
173+
#[cfg(target_os = "freebsd")]
174+
type Cpuset = libc::cpuset_t;
175+
let mut set: Cpuset = unsafe { mem::zeroed() };
167176
unsafe {
168-
if libc::sched_getaffinity(0, size_of::<libc::cpu_set_t>(), &mut set) == 0 {
177+
if libc::sched_getaffinity(0, mem::size_of::<Cpuset>(), &mut set) == 0 {
169178
let count = libc::CPU_COUNT(&set) as usize;
170179
let count = count.min(quota);
171180

0 commit comments

Comments
 (0)