1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Get the number of online CPU cores
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
16 #if defined(HAVE_CPUCORES_SYSCONF)
19 #elif defined(HAVE_CPUCORES_SYSCTL)
20 # ifdef HAVE_SYS_PARAM_H
21 # include <sys/param.h>
23 # ifdef HAVE_SYS_SYSCTL_H
24 # include <sys/sysctl.h>
29 static inline uint32_t
34 #if defined(HAVE_CPUCORES_SYSCONF)
35 const long cpus = sysconf(_SC_NPROCESSORS_ONLN);
37 ret = (uint32_t)(cpus);
39 #elif defined(HAVE_CPUCORES_SYSCTL)
40 int name[2] = { CTL_HW, HW_NCPU };
42 size_t cpus_size = sizeof(cpus);
43 if (sysctl(name, 2, &cpus, &cpus_size, NULL, 0) != -1
44 && cpus_size == sizeof(cpus) && cpus > 0)
45 ret = (uint32_t)(cpus);