-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcores.cpp
44 lines (37 loc) · 979 Bytes
/
cores.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "cores.h"
#if defined(linux) || defined(__linux) || defined(__linux__) || \
defined(sun) || defined(__sun) || \
defined(__APPLE__)
#include <unistd.h>
int core_count()
{
static int count = 0;
if (count == 0)
count = sysconf(_SC_NPROCESSORS_ONLN);
// What if that says we don't have a processor?
// Will that ever happen?
if (count == 0)
return DEFAULT_CORES;
return count;
}
#elif defined(__WIN32) || defined(__WIN32__)
// TODO: don't include hyperthreading
#include <windows.h>
int core_count()
{
static int count = 0;
if (count == 0)
{
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
count = sysinfo.dwNumberOfProcessors;
}
if (count == 0)
return DEFAULT_CORES;
return count;
}
//#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
// TODO: figure out how to get # of cores on BSD
#else
int core_count() { return DEFAULT_CORES; }
#endif