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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
/* -*- c++ -*- */
/*
* Copyright 2012 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* GNU Radio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <gruel/thread.h>
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
#include <Windows.h>
namespace gruel {
gr_thread_t
get_current_thread_id()
{
return GetCurrentThread();
}
void
thread_bind_to_processor(unsigned int n)
{
std::vector<unsigned int> mask(1, n);
thread_bind_to_processor(get_current_thread_id(), mask);
}
void
thread_bind_to_processor(const std::vector<unsigned int> &mask)
{
thread_bind_to_processor(get_current_thread_id(), mask);
}
void
thread_bind_to_processor(gr_thread_t thread, unsigned int n)
{
std::vector<unsigned int> mask(1, n);
thread_bind_to_processor(thread, mask);
}
void
thread_bind_to_processor(gr_thread_t thread, const std::vector<unsigned int> &mask)
{
//DWORD_PTR mask = (1 << n);
DWORD_PTR dword_mask = 0;
std::vector<unsigned int> _mask = mask;
std::vector<unsigned int>::iterator itr;
for(itr = _mask.begin(); itr != _mask.end(); itr++)
dword_mask |= (1 << (*itr));
DWORD_PTR ret = SetThreadAffinityMask(thread, dword_mask);
if(ret == 0) {
std::stringstream s;
s << "thread_bind_to_processor failed with error: " << GetLastError() << std::endl;
throw std::runtime_error(s.str());
}
}
void
thread_unbind()
{
thread_unbind(get_current_thread_id());
}
void
thread_unbind(gr_thread_t thread)
{
DWORD_PTR dword_mask = sizeof(DWORD_PTR) - 1;
DWORD_PTR ret = SetThreadAffinityMask(thread, dword_mask);
if(ret == 0) {
std::stringstream s;
s << "thread_unbind failed with error: " << GetLastError() << std::endl;
throw std::runtime_error(s.str());
}
}
} /* namespace gruel */
#elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
namespace gruel {
gr_thread_t
get_current_thread_id()
{
// Not implemented on OSX
}
void
thread_bind_to_processor(unsigned int n)
{
// Not implemented on OSX
}
void
thread_bind_to_processor(gr_thread_t thread, unsigned int n)
{
// Not implemented on OSX
}
void
thread_bind_to_processor(const std::vector<unsigned int> &mask)
{
// Not implemented on OSX
}
void
thread_bind_to_processor(gr_thread_t thread, const std::vector<unsigned int> &mask)
{
// Not implemented on OSX
}
void
thread_unbind()
{
// Not implemented on OSX
}
void
thread_unbind(gr_thread_t thread)
{
// Not implemented on OSX
}
} /* namespace gruel */
#else
#include <sstream>
#include <stdexcept>
#include <pthread.h>
namespace gruel {
gr_thread_t
get_current_thread_id()
{
return pthread_self();
}
void
thread_bind_to_processor(unsigned int n)
{
std::vector<unsigned int> mask(1, n);
thread_bind_to_processor(get_current_thread_id(), mask);
}
void
thread_bind_to_processor(const std::vector<unsigned int> &mask)
{
thread_bind_to_processor(get_current_thread_id(), mask);
}
void
thread_bind_to_processor(gr_thread_t thread, unsigned int n)
{
std::vector<unsigned int> mask(1, n);
thread_bind_to_processor(thread, mask);
}
void
thread_bind_to_processor(gr_thread_t thread, const std::vector<unsigned int> &mask)
{
cpu_set_t set;
size_t len = sizeof(cpu_set_t);
std::vector<unsigned int> _mask = mask;
std::vector<unsigned int>::iterator itr;
CPU_ZERO(&set);
for(itr = _mask.begin(); itr != _mask.end(); itr++)
CPU_SET(*itr, &set);
int ret = pthread_setaffinity_np(thread, len, &set);
if(ret != 0) {
std::stringstream s;
s << "thread_bind_to_processor failed with error: " << ret << std::endl;
throw std::runtime_error(s.str());
}
}
void
thread_unbind()
{
thread_unbind(get_current_thread_id());
}
void
thread_unbind(gr_thread_t thread)
{
cpu_set_t set;
size_t len = sizeof(cpu_set_t);
CPU_ZERO(&set);
long ncpus = sysconf(_SC_NPROCESSORS_ONLN);
for(long n = 0; n < ncpus; n++) {
CPU_SET(n, &set);
}
int ret = pthread_setaffinity_np(thread, len, &set);
if(ret != 0) {
std::stringstream s;
s << "thread_unbind failed with error: " << ret << std::endl;
throw std::runtime_error(s.str());
}
}
} /* namespace gruel */
#endif
|