Skip to content

Commit e3f3d59

Browse files
committed
Auto merge of #120589 - devnexen:cpuaff_fbsd_upd, r=m-ou-se
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.
2 parents 66428d9 + 3ccabb4 commit e3f3d59

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

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

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

@@ -190,32 +199,12 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
190199
}
191200
}
192201
any(
193-
target_os = "freebsd",
194202
target_os = "dragonfly",
195203
target_os = "openbsd",
196204
target_os = "netbsd",
197205
) => {
198206
use crate::ptr;
199207

200-
#[cfg(target_os = "freebsd")]
201-
{
202-
let mut set: libc::cpuset_t = unsafe { mem::zeroed() };
203-
unsafe {
204-
if libc::cpuset_getaffinity(
205-
libc::CPU_LEVEL_WHICH,
206-
libc::CPU_WHICH_PID,
207-
-1,
208-
size_of::<libc::cpuset_t>(),
209-
&mut set,
210-
) == 0 {
211-
let count = libc::CPU_COUNT(&set) as usize;
212-
if count > 0 {
213-
return Ok(NonZero::new_unchecked(count));
214-
}
215-
}
216-
}
217-
}
218-
219208
#[cfg(target_os = "netbsd")]
220209
{
221210
unsafe {

0 commit comments

Comments
 (0)