Skip to content

Commit 0a4206a

Browse files
authored
Rollup merge of #120589 - devnexen:cpuaff_fbsd_upd, r=clarfonthey
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 8f3a9a1 + 9f208cb commit 0a4206a

1 file changed

Lines changed: 12 additions & 28 deletions

File tree

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

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
162162
target_os = "hurd",
163163
target_os = "linux",
164164
target_os = "aix",
165+
target_os = "freebsd",
165166
target_vendor = "apple",
166167
target_os = "cygwin",
167168
target_os = "redox",
@@ -174,9 +175,17 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
174175
#[cfg(any(target_os = "android", target_os = "linux"))]
175176
{
176177
quota = cgroups::quota().max(1);
177-
let mut set: libc::cpu_set_t = unsafe { mem::zeroed() };
178+
}
179+
180+
#[cfg(any(target_os = "android", target_os = "linux", target_os = "freebsd"))]
181+
{
182+
#[cfg(not(target_os = "freebsd"))]
183+
type Cpuset = libc::cpu_set_t;
184+
#[cfg(target_os = "freebsd")]
185+
type Cpuset = libc::cpuset_t;
186+
let mut set: Cpuset = unsafe { mem::zeroed() };
178187
unsafe {
179-
if libc::sched_getaffinity(0, size_of::<libc::cpu_set_t>(), &mut set) == 0 {
188+
if libc::sched_getaffinity(0, size_of::<Cpuset>(), &mut set) == 0 {
180189
let count = libc::CPU_COUNT(&set) as usize;
181190
let count = count.min(quota);
182191

@@ -201,34 +210,9 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
201210
}
202211
}
203212
}
204-
any(
205-
target_os = "freebsd",
206-
target_os = "dragonfly",
207-
target_os = "openbsd",
208-
target_os = "netbsd",
209-
) => {
213+
any(target_os = "dragonfly", target_os = "openbsd", target_os = "netbsd") => {
210214
use crate::ptr;
211215

212-
#[cfg(target_os = "freebsd")]
213-
{
214-
let mut set: libc::cpuset_t = unsafe { mem::zeroed() };
215-
unsafe {
216-
if libc::cpuset_getaffinity(
217-
libc::CPU_LEVEL_WHICH,
218-
libc::CPU_WHICH_PID,
219-
-1,
220-
size_of::<libc::cpuset_t>(),
221-
&mut set,
222-
) == 0
223-
{
224-
let count = libc::CPU_COUNT(&set) as usize;
225-
if count > 0 {
226-
return Ok(NonZero::new_unchecked(count));
227-
}
228-
}
229-
}
230-
}
231-
232216
#[cfg(target_os = "netbsd")]
233217
{
234218
unsafe {

0 commit comments

Comments
 (0)