diff options
Diffstat (limited to 'lib/nproc.c')
-rw-r--r-- | lib/nproc.c | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/lib/nproc.c b/lib/nproc.c index e3ddb9288..378d3ff3f 100644 --- a/lib/nproc.c +++ b/lib/nproc.c @@ -1,6 +1,6 @@ /* Detect the number of processors. - Copyright (C) 2009-2021 Free Software Foundation, Inc. + Copyright (C) 2009-2022 Free Software Foundation, Inc. This file is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as @@ -307,15 +307,23 @@ num_processors_ignoring_omp (enum nproc_query query) NPROC_CURRENT and NPROC_ALL. */ #if HAVE_SYSCTL && ! defined __GLIBC__ && defined HW_NCPU - { /* This works on Mac OS X, FreeBSD, NetBSD, OpenBSD. */ + { /* This works on macOS, FreeBSD, NetBSD, OpenBSD. + macOS 10.14 does not allow mib to be const. */ 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 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 |