1 /* -*- c++ -*- */
2 /*
3  * Copyright 2012-2014 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 #include <gnuradio/thread/thread.h>
27 #include <boost/format.hpp>
28 
29 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
30 
31 #include <windows.h>
32 
33 namespace gr {
34 namespace thread {
35 
get_current_thread_id()36 gr_thread_t get_current_thread_id() { return GetCurrentThread(); }
37 
thread_bind_to_processor(int n)38 void thread_bind_to_processor(int n)
39 {
40     std::vector<int> mask(1, n);
41     thread_bind_to_processor(get_current_thread_id(), mask);
42 }
43 
thread_bind_to_processor(const std::vector<int> & mask)44 void thread_bind_to_processor(const std::vector<int>& mask)
45 {
46     thread_bind_to_processor(get_current_thread_id(), mask);
47 }
48 
thread_bind_to_processor(gr_thread_t thread,int n)49 void thread_bind_to_processor(gr_thread_t thread, int n)
50 {
51     std::vector<int> mask(1, n);
52     thread_bind_to_processor(thread, mask);
53 }
54 
thread_bind_to_processor(gr_thread_t thread,const std::vector<int> & mask)55 void thread_bind_to_processor(gr_thread_t thread, const std::vector<int>& mask)
56 {
57     // DWORD_PTR mask = (1 << n);
58     DWORD_PTR dword_mask = 0;
59 
60     std::vector<int> _mask = mask;
61     std::vector<int>::iterator itr;
62     for (itr = _mask.begin(); itr != _mask.end(); itr++)
63         dword_mask |= (1 << (*itr));
64 
65     DWORD_PTR ret = SetThreadAffinityMask(thread, dword_mask);
66     if (ret == 0) {
67         std::stringstream s;
68         s << "thread_bind_to_processor failed with error: " << GetLastError()
69           << std::endl;
70         throw std::runtime_error(s.str());
71     }
72 }
73 
thread_unbind()74 void thread_unbind() { thread_unbind(get_current_thread_id()); }
75 
thread_unbind(gr_thread_t thread)76 void thread_unbind(gr_thread_t thread)
77 {
78     DWORD_PTR dword_mask = sizeof(DWORD_PTR) - 1;
79     DWORD_PTR ret = SetThreadAffinityMask(thread, dword_mask);
80     if (ret == 0) {
81         std::stringstream s;
82         s << "thread_unbind failed with error: " << GetLastError() << std::endl;
83         throw std::runtime_error(s.str());
84     }
85 }
86 
thread_priority(gr_thread_t thread)87 int thread_priority(gr_thread_t thread)
88 {
89     // Not implemented on Windows
90     return -1;
91 }
92 
set_thread_priority(gr_thread_t thread,int priority)93 int set_thread_priority(gr_thread_t thread, int priority)
94 {
95     // Not implemented on Windows
96     return -1;
97 }
98 #ifndef __MINGW32__
99 #pragma pack(push, 8)
100 typedef struct tagTHREADNAME_INFO {
101     DWORD dwType;     // Must be 0x1000
102     LPCSTR szName;    // Pointer to name (in user addr space)
103     DWORD dwThreadID; // Thread ID (-1 = caller thread)
104     DWORD dwFlags;    // Reserved for future use, must be zero
105 } THREADNAME_INFO;
106 #pragma pack(pop)
_set_thread_name(gr_thread_t thread,const char * name,DWORD dwThreadId)107 static void _set_thread_name(gr_thread_t thread, const char* name, DWORD dwThreadId)
108 {
109     const DWORD SET_THREAD_NAME_EXCEPTION = 0x406D1388;
110 
111     THREADNAME_INFO info;
112     info.dwType = 0x1000;
113     info.szName = name;
114     info.dwThreadID = dwThreadId;
115     info.dwFlags = 0;
116 
117     __try {
118         RaiseException(SET_THREAD_NAME_EXCEPTION,
119                        0,
120                        sizeof(info) / sizeof(ULONG_PTR),
121                        (ULONG_PTR*)&info);
122     } __except (EXCEPTION_EXECUTE_HANDLER) {
123     }
124 }
125 
set_thread_name(gr_thread_t thread,std::string name)126 void set_thread_name(gr_thread_t thread, std::string name)
127 {
128     DWORD dwThreadId = GetThreadId(thread);
129     if (dwThreadId == 0)
130         return;
131 
132     if (name.empty())
133         name = boost::str(boost::format("thread %lu") % dwThreadId);
134 
135     _set_thread_name(thread, name.c_str(), dwThreadId);
136 }
137 #else
set_thread_name(gr_thread_t thread,std::string name)138 void set_thread_name(gr_thread_t thread, std::string name)
139 {
140     /* Not implemented on mingw-w64 */
141 }
142 #endif /* !__MINGW32__ */
143 
144 } /* namespace thread */
145 } /* namespace gr */
146 
147 
148 #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__) ||     \
149     defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__GNU__) || \
150     defined(__NetBSD__) || defined(__DragonFly__)
151 
152 namespace gr {
153 namespace thread {
154 
get_current_thread_id()155 gr_thread_t get_current_thread_id()
156 {
157     // Not implemented on OSX
158     return NULL;
159 }
160 
thread_bind_to_processor(int n)161 void thread_bind_to_processor(int n)
162 {
163     // Not implemented on OSX
164 }
165 
thread_bind_to_processor(gr_thread_t thread,int n)166 void thread_bind_to_processor(gr_thread_t thread, int n)
167 {
168     // Not implemented on OSX
169 }
170 
thread_bind_to_processor(const std::vector<int> & mask)171 void thread_bind_to_processor(const std::vector<int>& mask)
172 {
173     // Not implemented on OSX
174 }
175 
thread_bind_to_processor(gr_thread_t thread,const std::vector<int> & mask)176 void thread_bind_to_processor(gr_thread_t thread, const std::vector<int>& mask)
177 {
178     // Not implemented on OSX
179 }
180 
thread_unbind()181 void thread_unbind()
182 {
183     // Not implemented on OSX
184 }
185 
thread_unbind(gr_thread_t thread)186 void thread_unbind(gr_thread_t thread)
187 {
188     // Not implemented on OSX
189 }
190 
thread_priority(gr_thread_t thread)191 int thread_priority(gr_thread_t thread)
192 {
193     sched_param param;
194     int priority;
195     int policy;
196     int ret;
197     ret = pthread_getschedparam(thread, &policy, &param);
198     priority = param.sched_priority;
199     return (ret == 0) ? priority : ret;
200 }
201 
set_thread_priority(gr_thread_t thread,int priority)202 int set_thread_priority(gr_thread_t thread, int priority)
203 {
204     int policy;
205     struct sched_param param;
206     pthread_getschedparam(thread, &policy, &param);
207     param.sched_priority = priority;
208     return pthread_setschedparam(thread, policy, &param);
209 }
210 
set_thread_name(gr_thread_t thread,std::string name)211 void set_thread_name(gr_thread_t thread, std::string name)
212 {
213     // Not implemented on OSX
214 }
215 
216 } /* namespace thread */
217 } /* namespace gr */
218 
219 #else
220 
221 #include <pthread.h>
222 #include <sys/prctl.h>
223 #include <sstream>
224 #include <stdexcept>
225 
226 namespace gr {
227 namespace thread {
228 
get_current_thread_id()229 gr_thread_t get_current_thread_id() { return pthread_self(); }
230 
thread_bind_to_processor(int n)231 void thread_bind_to_processor(int n)
232 {
233     std::vector<int> mask(1, n);
234     thread_bind_to_processor(get_current_thread_id(), mask);
235 }
236 
thread_bind_to_processor(const std::vector<int> & mask)237 void thread_bind_to_processor(const std::vector<int>& mask)
238 {
239     thread_bind_to_processor(get_current_thread_id(), mask);
240 }
241 
thread_bind_to_processor(gr_thread_t thread,int n)242 void thread_bind_to_processor(gr_thread_t thread, int n)
243 {
244     std::vector<int> mask(1, n);
245     thread_bind_to_processor(thread, mask);
246 }
247 
thread_bind_to_processor(gr_thread_t thread,const std::vector<int> & mask)248 void thread_bind_to_processor(gr_thread_t thread, const std::vector<int>& mask)
249 {
250     cpu_set_t set;
251     size_t len = sizeof(cpu_set_t);
252     std::vector<int> _mask = mask;
253     std::vector<int>::iterator itr;
254 
255     CPU_ZERO(&set);
256     for (itr = _mask.begin(); itr != _mask.end(); itr++)
257         CPU_SET(*itr, &set);
258 
259     int ret = pthread_setaffinity_np(thread, len, &set);
260     if (ret != 0) {
261         std::stringstream s;
262         s << "thread_bind_to_processor failed with error: " << ret << std::endl;
263         throw std::runtime_error(s.str());
264     }
265 }
266 
thread_unbind()267 void thread_unbind() { thread_unbind(get_current_thread_id()); }
268 
thread_unbind(gr_thread_t thread)269 void thread_unbind(gr_thread_t thread)
270 {
271     cpu_set_t set;
272     size_t len = sizeof(cpu_set_t);
273 
274     CPU_ZERO(&set);
275     long ncpus = sysconf(_SC_NPROCESSORS_ONLN);
276     for (long n = 0; n < ncpus; n++) {
277         CPU_SET(n, &set);
278     }
279 
280     int ret = pthread_setaffinity_np(thread, len, &set);
281     if (ret != 0) {
282         std::stringstream s;
283         s << "thread_unbind failed with error: " << ret << std::endl;
284         throw std::runtime_error(s.str());
285     }
286 }
287 
thread_priority(gr_thread_t thread)288 int thread_priority(gr_thread_t thread)
289 {
290     sched_param param;
291     int priority;
292     int policy;
293     int ret;
294     ret = pthread_getschedparam(thread, &policy, &param);
295     priority = param.sched_priority;
296     return (ret == 0) ? priority : ret;
297 }
298 
set_thread_priority(gr_thread_t thread,int priority)299 int set_thread_priority(gr_thread_t thread, int priority)
300 {
301     int policy;
302     struct sched_param param;
303     pthread_getschedparam(thread, &policy, &param);
304     param.sched_priority = priority;
305     return pthread_setschedparam(thread, policy, &param);
306 }
307 
set_thread_name(gr_thread_t thread,std::string name)308 void set_thread_name(gr_thread_t thread, std::string name)
309 {
310     if (thread != pthread_self()) // Naming another thread is not supported
311         return;
312 
313     if (name.empty())
314         name = boost::str(boost::format("thread %llu") % ((unsigned long long)thread));
315 
316     const int max_len = 16; // Maximum accepted by PR_SET_NAME
317 
318     if ((int)name.size() > max_len) // Shorten the name if necessary by taking as many
319                                     // characters from the front
320     {                               // so that the unique_id can still fit on the end
321         int i = name.size() - 1;
322         for (; i >= 0; --i) {
323             std::string s = name.substr(i, 1);
324             int n = atoi(s.c_str());
325             if ((n == 0) && (s != "0"))
326                 break;
327         }
328 
329         name = name.substr(0, std::max(0, max_len - ((int)name.size() - (i + 1)))) +
330                name.substr(i + 1);
331     }
332 
333     prctl(PR_SET_NAME, name.c_str(), 0, 0, 0);
334 }
335 
336 } /* namespace thread */
337 } /* namespace gr */
338 
339 #endif
340