1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Get the number of online CPU cores
6 // This code has been put into the public domain.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 ///////////////////////////////////////////////////////////////////////////////
17 #if defined(HAVE_NCPU_SYSCONF)
20 #elif defined(HAVE_NCPU_SYSCTL)
21 # ifdef HAVE_SYS_PARAM_H
22 # include <sys/param.h>
24 # ifdef HAVE_SYS_SYSCTL_H
25 # include <sys/sysctl.h>
30 static inline uint32_t
35 #if defined(HAVE_CPUCORES_SYSCONF)
36 const long cpus = sysconf(_SC_NPROCESSORS_ONLN);
38 ret = (uint32_t)(cpus);
40 #elif defined(HAVE_CPUCORES_SYSCTL)
41 int name[2] = { CTL_HW, HW_NCPU };
43 size_t cpus_size = sizeof(cpus);
44 if (!sysctl(name, &cpus, &cpus_size, NULL, NULL)
45 && cpus_size == sizeof(cpus) && cpus > 0)
46 ret = (uint32_t)(cpus);