1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file tuklib_cpucores.c
4 /// \brief Get the number of CPU cores online
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 ///////////////////////////////////////////////////////////////////////////////
13 #include "tuklib_cpucores.h"
15 #if defined(TUKLIB_CPUCORES_SYSCTL)
16 # ifdef HAVE_SYS_PARAM_H
17 # include <sys/param.h>
19 # include <sys/sysctl.h>
21 #elif defined(TUKLIB_CPUCORES_SYSCONF)
31 #if defined(TUKLIB_CPUCORES_SYSCTL)
32 int name[2] = { CTL_HW, HW_NCPU };
34 size_t cpus_size = sizeof(cpus);
35 if (sysctl(name, 2, &cpus, &cpus_size, NULL, 0) != -1
36 && cpus_size == sizeof(cpus) && cpus > 0)
39 #elif defined(TUKLIB_CPUCORES_SYSCONF)
40 const long cpus = sysconf(_SC_NPROCESSORS_ONLN);