-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
Platform.cpp
71 lines (60 loc) · 1.53 KB
/
Platform.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "Platform.h"
#include <stdio.h>
#include <assert.h>
long getenvlong(const char *name, long minval, long defval, long maxval)
{
assert(minval <= defval && defval <= maxval);
const char *s = getenv(name);
if (!s)
return defval;
char *tail;
long l = strtol(s, &tail, 0);
if (*tail)
return defval;
if (l < minval) l = minval;
if (l > maxval) l = maxval;
return l;
}
#if defined(_WIN32)
#include <windows.h>
void SetAffinity ( int cpu )
{
SetProcessAffinityMask(GetCurrentProcess(),cpu);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
}
#if NCPU > 1
void SetThreadAffinity ( std::thread &t, int cpu )
{
SetThreadIdealProcessor((HANDLE)t.native_handle(), (DWORD)cpu);
}
#endif
#else
#include <sched.h>
void SetAffinity ( int /*cpu*/ )
{
#if !defined(__CYGWIN__) && !defined(__APPLE__) && !defined(__FreeBSD__)
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(2,&mask);
if( sched_setaffinity(0,sizeof(mask),&mask) == -1)
{
printf("WARNING: Could not set CPU affinity\n");
}
#endif
}
#if NCPU > 1
void SetThreadAffinity ( std::thread &t, int cpu )
{
#if !defined(__CYGWIN__) && !defined(__APPLE__) && !defined(__FreeBSD__)
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(cpu, &cpuset);
pthread_setaffinity_np(t.native_handle(), sizeof(cpu_set_t), &cpuset);
#elif defined(__APPLE__)
thread_affinity_policy_data_t policy = { cpu };
thread_policy_set(pthread_mach_thread_np(t.native_handle()), THREAD_AFFINITY_POLICY,
(thread_policy_t)&policy, 1);
#endif
}
#endif
#endif