Skip to content

Commit 25b3751

Browse files
committed
std: available_parallelism using native netbsd api first
before falling back to existing code paths like FreeBSD does.
1 parent fd9bf59 commit 25b3751

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

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

+23
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,29 @@ pub fn available_parallelism() -> io::Result<NonZeroUsize> {
344344
}
345345
}
346346

347+
#[cfg(target_os = "netbsd")]
348+
{
349+
unsafe {
350+
let set = libc::_cpuset_create();
351+
if !set.is_null() {
352+
let mut count: usize = 0;
353+
if libc::pthread_getaffinity_np(libc::pthread_self(), libc::_cpuset_size(set), set) == 0 {
354+
for i in 0..u64::MAX {
355+
match libc::_cpuset_isset(i, set) {
356+
-1 => break,
357+
0 => continue,
358+
_ => count = count + 1,
359+
}
360+
}
361+
}
362+
libc::_cpuset_destroy(set);
363+
if let Some(count) = NonZeroUsize::new(count) {
364+
return Ok(count);
365+
}
366+
}
367+
}
368+
}
369+
347370
let mut cpus: libc::c_uint = 0;
348371
let mut cpus_size = crate::mem::size_of_val(&cpus);
349372

0 commit comments

Comments
 (0)