Skip to content

Commit 3633f8b

Browse files
committed
openbsd: available_parallelism: use the right API
use the standard sysconf(_SC_NPROCESSORS_ONLN) way to get the number of available processors (capable of running processes), and fallback to sysctl([CTL_HW, HW_NCPU]) (number of CPUs configured) only on error. it permits to differenciate CPUs online vs CPUs configured (and not necessary capable of running processes). while here, use the common code path for BSDs for doing that, and avoid code duplication. Problem initially reported to me by Jiri Navratil.
1 parent fe2cfd4 commit 3633f8b

File tree

1 file changed

+6
-26
lines changed

1 file changed

+6
-26
lines changed

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

+6-26
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,12 @@ pub fn available_parallelism() -> io::Result<NonZeroUsize> {
354354
Ok(unsafe { NonZeroUsize::new_unchecked(count) })
355355
}
356356
}
357-
} else if #[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd"))] {
357+
} else if #[cfg(any(
358+
target_os = "freebsd",
359+
target_os = "dragonfly",
360+
target_os = "openbsd",
361+
target_os = "netbsd",
362+
))] {
358363
use crate::ptr;
359364

360365
#[cfg(target_os = "freebsd")]
@@ -427,31 +432,6 @@ pub fn available_parallelism() -> io::Result<NonZeroUsize> {
427432
return Err(io::const_io_error!(io::ErrorKind::NotFound, "The number of hardware threads is not known for the target platform"));
428433
}
429434
}
430-
Ok(unsafe { NonZeroUsize::new_unchecked(cpus as usize) })
431-
} else if #[cfg(target_os = "openbsd")] {
432-
use crate::ptr;
433-
434-
let mut cpus: libc::c_uint = 0;
435-
let mut cpus_size = crate::mem::size_of_val(&cpus);
436-
let mut mib = [libc::CTL_HW, libc::HW_NCPU, 0, 0];
437-
438-
let res = unsafe {
439-
libc::sysctl(
440-
mib.as_mut_ptr(),
441-
2,
442-
&mut cpus as *mut _ as *mut _,
443-
&mut cpus_size as *mut _ as *mut _,
444-
ptr::null_mut(),
445-
0,
446-
)
447-
};
448-
449-
// Handle errors if any.
450-
if res == -1 {
451-
return Err(io::Error::last_os_error());
452-
} else if cpus == 0 {
453-
return Err(io::const_io_error!(io::ErrorKind::NotFound, "The number of hardware threads is not known for the target platform"));
454-
}
455435

456436
Ok(unsafe { NonZeroUsize::new_unchecked(cpus as usize) })
457437
} else if #[cfg(target_os = "nto")] {

0 commit comments

Comments
 (0)