Skip to content

Commit 4689595

Browse files
committed
HW_AVAILCPU is unavailable under openbsd
define `num_cpus()` function for openbsd that use `HW_NCPU` for grabbing the current number of cpus that could be used.
1 parent cb3999c commit 4689595

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/libtest/lib.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,6 @@ fn get_concurrency() -> usize {
919919
#[cfg(any(target_os = "freebsd",
920920
target_os = "dragonfly",
921921
target_os = "bitrig",
922-
target_os = "openbsd",
923922
target_os = "netbsd"))]
924923
fn num_cpus() -> usize {
925924
let mut cpus: libc::c_uint = 0;
@@ -946,6 +945,24 @@ fn get_concurrency() -> usize {
946945
}
947946
cpus as usize
948947
}
948+
949+
#[cfg(target_os = "openbsd")]
950+
fn num_cpus() -> usize {
951+
let mut cpus: libc::c_uint = 0;
952+
let mut cpus_size = std::mem::size_of_val(&cpus);
953+
let mut mib = [libc::CTL_HW, libc::HW_NCPU, 0, 0];
954+
955+
unsafe {
956+
libc::sysctl(mib.as_mut_ptr(), 2,
957+
&mut cpus as *mut _ as *mut _,
958+
&mut cpus_size as *mut _ as *mut _,
959+
0 as *mut _, 0);
960+
}
961+
if cpus < 1 {
962+
cpus = 1;
963+
}
964+
cpus as usize
965+
}
949966
}
950967

951968
pub fn filter_tests(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> Vec<TestDescAndFn> {

0 commit comments

Comments
 (0)