2009-02-13 23:45:29 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file cpucores.h
|
|
|
|
/// \brief Get the number of online CPU cores
|
|
|
|
//
|
2009-04-13 10:27:40 +02:00
|
|
|
// Author: Lasse Collin
|
2009-02-13 23:45:29 +01:00
|
|
|
//
|
2009-04-13 10:27:40 +02:00
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
2009-02-13 23:45:29 +01:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef CPUCORES_H
|
|
|
|
#define CPUCORES_H
|
|
|
|
|
2009-03-18 15:51:41 +01:00
|
|
|
#if defined(HAVE_CPUCORES_SYSCONF)
|
2009-02-13 23:45:29 +01:00
|
|
|
# include <unistd.h>
|
|
|
|
|
2009-03-18 15:51:41 +01:00
|
|
|
#elif defined(HAVE_CPUCORES_SYSCTL)
|
2009-02-13 23:45:29 +01:00
|
|
|
# ifdef HAVE_SYS_PARAM_H
|
|
|
|
# include <sys/param.h>
|
|
|
|
# endif
|
|
|
|
# ifdef HAVE_SYS_SYSCTL_H
|
|
|
|
# include <sys/sysctl.h>
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static inline uint32_t
|
|
|
|
cpucores(void)
|
|
|
|
{
|
|
|
|
uint32_t ret = 0;
|
|
|
|
|
|
|
|
#if defined(HAVE_CPUCORES_SYSCONF)
|
|
|
|
const long cpus = sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
if (cpus > 0)
|
|
|
|
ret = (uint32_t)(cpus);
|
|
|
|
|
|
|
|
#elif defined(HAVE_CPUCORES_SYSCTL)
|
|
|
|
int name[2] = { CTL_HW, HW_NCPU };
|
|
|
|
int cpus;
|
|
|
|
size_t cpus_size = sizeof(cpus);
|
|
|
|
if (!sysctl(name, &cpus, &cpus_size, NULL, NULL)
|
|
|
|
&& cpus_size == sizeof(cpus) && cpus > 0)
|
|
|
|
ret = (uint32_t)(cpus);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|