Skip to content

Commit a25b5a3

Browse files
mackylegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9529080 commit a25b5a3

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

thread-utils.c

+17-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,23 @@ int online_cpus(void)
3535

3636
if (!pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0))
3737
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) */
3955

4056
#ifdef _SC_NPROCESSORS_ONLN
4157
if ((ncpus = (long)sysconf(_SC_NPROCESSORS_ONLN)) > 0)

0 commit comments

Comments
 (0)