From aa04667c6efc9fb21ca570aa26b83dd310f580f4 Mon Sep 17 00:00:00 2001 From: Havard Eidnes Date: Thu, 12 Oct 2023 14:12:55 +0000 Subject: [PATCH] std library thread.rs: undo attempt at NetBSD-specific "find parallelism". First off, I have it on good authority this code is Just Wrong: by default a given thread does not have affinity to any specific set of CPUs. This particular change came with this pull request: https://github.com/rust-lang/rust/pull/112226 However, even worse, this code causes a segmentation fault for certain NetBSD target architectures in the "bootstrap" program when building rust natively on those platforms. So far armv7/9.0, powerpc/10.0_BETA, and i386/9.3 all crash with a segmentation fault. However, for some strange reason, this isn't consistent across the board: riscv64/current, amd64/10.0_BETA, aarch64/9.0 and sparc64/10.0_BETA all pass this hurdle. A trivial C reimplementation also doesn't crash on any of these systems, ref. the thread which starts at https://mail-index.netbsd.org/current-users/2023/10/10/msg044510.html but also always prints 0. However, if we get a SEGV running this code, the entire build fails, of course. So ... while I do not have a full explanation for the SEGVs, this undoes the addition from pull request 112226, and restores the ability to build rust natively on the above flagged-as-problematical platforms. --- library/std/src/sys/unix/thread.rs | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/library/std/src/sys/unix/thread.rs b/library/std/src/sys/unix/thread.rs index e667f34aece05..151cd812a59a6 100644 --- a/library/std/src/sys/unix/thread.rs +++ b/library/std/src/sys/unix/thread.rs @@ -375,29 +375,6 @@ pub fn available_parallelism() -> io::Result { } } - #[cfg(target_os = "netbsd")] - { - unsafe { - let set = libc::_cpuset_create(); - if !set.is_null() { - let mut count: usize = 0; - if libc::pthread_getaffinity_np(libc::pthread_self(), libc::_cpuset_size(set), set) == 0 { - for i in 0..u64::MAX { - match libc::_cpuset_isset(i, set) { - -1 => break, - 0 => continue, - _ => count = count + 1, - } - } - } - libc::_cpuset_destroy(set); - if let Some(count) = NonZeroUsize::new(count) { - return Ok(count); - } - } - } - } - let mut cpus: libc::c_uint = 0; let mut cpus_size = crate::mem::size_of_val(&cpus);