thread-utils.c: detect CPU count on older BSD-like systems

Not all systems support using sysconf to detect the number of available CPU cores. Older BSD and BSD-derived systems only provide the information via the sysctl function. If HAVE_BSD_SYSCTL is defined attempt to retrieve the number of available CPU cores using the sysctl function. If HAVE_BSD_SYSCTL is not defined or the sysctl function fails, we still attempt to get the information via sysconf. Signed-off-by: Kyle J. McKay <mackyle@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kyle J. McKay committed Mar 7, 2015 at 23:14 UTC a25b5a32c76630f2433b860fef7bc28a9380a8f6
1 file changed +17 -1
thread-utils.c
+17 -1
@@ -35,7 +35,23 @@ int online_cpus(void)
35
36 if (!pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0))
37 return (int)psd.psd_proc_cnt;
38 -#endif
38 +#elif defined(HAVE_BSD_SYSCTL) && defined(HW_NCPU)
39 + int mib[2];
40 + size_t len;
41 + int cpucount;
42 +
43 + mib[0] = CTL_HW;
44 +# ifdef HW_AVAILCPU
45 + mib[1] = HW_AVAILCPU;
46 + len = sizeof(cpucount);
47 + if (!sysctl(mib, 2, &cpucount, &len, NULL, 0))
48 + return cpucount;
49 +# endif /* HW_AVAILCPU */
50 + mib[1] = HW_NCPU;
51 + len = sizeof(cpucount);
52 + if (!sysctl(mib, 2, &cpucount, &len, NULL, 0))
53 + return cpucount;
54 +#endif /* defined(HAVE_BSD_SYSCTL) && defined(HW_NCPU) */
55
56 #ifdef _SC_NPROCESSORS_ONLN
57 if ((ncpus = (long)sysconf(_SC_NPROCESSORS_ONLN)) > 0)