forked from vsg-dev/VulkanSceneGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Affinity.cpp
154 lines (123 loc) · 4.23 KB
/
Affinity.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* <editor-fold desc="MIT License">
Copyright(c) 2020 Robert Osfield
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</editor-fold> */
#include <vsg/threading/Affinity.h>
#ifdef _WIN32
# include <process.h>
# include <windows.h>
static void win32_setAffinity(HANDLE tid, const vsg::Affinity& affinity)
{
uint32_t numProcessors = std::thread::hardware_concurrency();
DWORD_PTR affinityMask = 0x0;
if (affinity)
{
for (auto cpu : affinity.cpus)
{
if (cpu < numProcessors)
{
affinityMask |= (0x1LL << cpu);
}
}
}
else
{
// set affinity to all CPU cores
for (uint32_t cpu = 0; cpu < numProcessors; ++cpu)
{
affinityMask |= (0x1LL << cpu);
}
}
/*DWORD_PTR res =*/SetThreadAffinityMask(tid, affinityMask);
}
void vsg::setAffinity(std::thread& thread, const Affinity& affinity)
{
win32_setAffinity((HANDLE)thread.native_handle(), affinity);
}
void vsg::setAffinity(const Affinity& affinity)
{
win32_setAffinity(GetCurrentThread(), affinity);
}
#elif defined(__APPLE__)
# include <mach/mach.h>
# include <pthread.h>
static void macos_setAffinity(pthread_t thread_native_handle, const vsg::Affinity& affinity)
{
uint32_t numProcessors = std::thread::hardware_concurrency();
integer_t cpuset = 0;
if (affinity)
{
for (auto cpu : affinity.cpus)
{
if (cpu < numProcessors)
{
cpuset |= (0x1 << cpu);
}
}
}
else
{
// set affinity to all CPU cores
for (uint32_t cpu = 0; cpu < numProcessors; ++cpu)
{
cpuset |= (0x1 << cpu);
}
}
auto mach_thread = pthread_mach_thread_np(thread_native_handle);
thread_affinity_policy_data_t policy = {cpuset};
thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy, 1);
}
void vsg::setAffinity(std::thread& thread, const Affinity& affinity)
{
macos_setAffinity(thread.native_handle(), affinity);
}
void vsg::setAffinity(const Affinity& affinity)
{
macos_setAffinity(pthread_self(), affinity);
}
#elif defined(__ANDROID__) || defined(__CYGWIN__)
void vsg::setAffinity(std::thread&, const Affinity&)
{
// Not currently implemented
}
void vsg::setAffinity(const Affinity&)
{
// Not currently implemented
}
#else // unices
static void pthread_setAffinity(pthread_t thread_native_handle, const vsg::Affinity& affinity)
{
uint32_t numProcessors = std::thread::hardware_concurrency();
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
if (affinity)
{
for (auto cpu : affinity.cpus)
{
if (cpu < numProcessors)
{
CPU_SET(cpu, &cpuset);
}
}
}
else
{
// set affinity to all CPU cores
for (uint32_t cpu = 0; cpu < numProcessors; ++cpu)
{
CPU_SET(cpu, &cpuset);
}
}
/*int rc =*/pthread_setaffinity_np(thread_native_handle, sizeof(cpu_set_t), &cpuset);
}
void vsg::setAffinity(std::thread& thread, const Affinity& affinity)
{
pthread_setAffinity(thread.native_handle(), affinity);
}
void vsg::setAffinity(const Affinity& affinity)
{
pthread_setAffinity(pthread_self(), affinity);
}
#endif