From 7fc3219bcc2bf448ef26cf30a2e5770fdda3f2b4 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 10 Oct 2021 10:43:47 -0700 Subject: [PATCH] nproc: port better to OpenBSD Problem reported by Omar Polo in: https://lists.gnu.org/r/emacs-devel/2021-10/msg00692.html * lib/nproc.c (num_processors_ignoring_omp): Prefer HW_NCPUONLINE to HW_NCPU, for OpenBSD. Also, make mib const. --- ChangeLog | 8 ++++++++ lib/nproc.c | 19 +++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 232d2dec3..da5b570ee 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2021-10-10 Paul Eggert + + nproc: port better to OpenBSD + Problem reported by Omar Polo in: + https://lists.gnu.org/r/emacs-devel/2021-10/msg00692.html + * lib/nproc.c (num_processors_ignoring_omp): Prefer HW_NCPUONLINE + to HW_NCPU, for OpenBSD. Also, make mib const. + 2021-10-02 Paul Eggert timer-time: port better to OpenBSD 6.9 diff --git a/lib/nproc.c b/lib/nproc.c index e3ddb9288..a9e369dd3 100644 --- a/lib/nproc.c +++ b/lib/nproc.c @@ -310,12 +310,19 @@ num_processors_ignoring_omp (enum nproc_query query) { /* This works on Mac OS X, FreeBSD, NetBSD, OpenBSD. */ int nprocs; size_t len = sizeof (nprocs); - static int mib[2] = { CTL_HW, HW_NCPU }; - - if (sysctl (mib, ARRAY_SIZE (mib), &nprocs, &len, NULL, 0) == 0 - && len == sizeof (nprocs) - && 0 < nprocs) - return nprocs; + static int const mib[][2] = { +# ifdef HW_NCPUONLINE + { CTL_HW, HW_NCPUONLINE }, +# endif + { CTL_HW, HW_NCPU } + }; + for (int i = 0; i < ARRAY_SIZE (mib); i++) + { + if (sysctl (mib[i], ARRAY_SIZE (mib[i]), &nprocs, &len, NULL, 0) == 0 + && len == sizeof (nprocs) + && 0 < nprocs) + return nprocs; + } } #endif -- 2.30.2