summaryrefslogtreecommitdiff
path: root/gnuradio-runtime/lib/thread/thread.cc
blob: f2606c71ae8742279c17cf9465f5a7f38ec7999d (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* -*- c++ -*- */
/*
 * Copyright 2012-2014 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 <gnuradio/thread/thread.h>
#include <boost/format.hpp>

#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)

#include <windows.h>

namespace gr {
  namespace thread {

    gr_thread_t
    get_current_thread_id()
    {
      return GetCurrentThread();
    }

    void
    thread_bind_to_processor(int n)
    {
      std::vector<int> mask(1, n);
      thread_bind_to_processor(get_current_thread_id(), mask);
    }

    void
    thread_bind_to_processor(const std::vector<int> &mask)
    {
      thread_bind_to_processor(get_current_thread_id(), mask);
    }

    void
    thread_bind_to_processor(gr_thread_t thread, int n)
    {
      std::vector<int> mask(1, n);
      thread_bind_to_processor(thread, mask);
    }

    void
    thread_bind_to_processor(gr_thread_t thread, const std::vector<int> &mask)
    {
      //DWORD_PTR mask = (1 << n);
      DWORD_PTR dword_mask = 0;

      std::vector<int> _mask = mask;
      std::vector<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());
      }
    }

    int
    thread_priority(gr_thread_t thread)
    {
      // Not implemented on Windows
      return -1;
    }

    int
    set_thread_priority(gr_thread_t thread, int priority)
    {
      // Not implemented on Windows
      return -1;
    }
#ifndef __MINGW32__
#pragma pack(push,8)
    typedef struct tagTHREADNAME_INFO
    {
      DWORD dwType;     // Must be 0x1000
      LPCSTR szName;    // Pointer to name (in user addr space)
      DWORD dwThreadID; // Thread ID (-1 = caller thread)
      DWORD dwFlags;    // Reserved for future use, must be zero
    } THREADNAME_INFO;
#pragma pack(pop)
    static void
    _set_thread_name(gr_thread_t thread, const char* name, DWORD dwThreadId)
    {
      const DWORD SET_THREAD_NAME_EXCEPTION = 0x406D1388;

      THREADNAME_INFO info;
      info.dwType = 0x1000;
      info.szName = name;
      info.dwThreadID = dwThreadId;
      info.dwFlags = 0;

      __try
      {
        RaiseException(SET_THREAD_NAME_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info);
      }
      __except(EXCEPTION_EXECUTE_HANDLER)
      {
      }
    }

    void
    set_thread_name(gr_thread_t thread, std::string name)
    {
      DWORD dwThreadId = GetThreadId(thread);
      if (dwThreadId == 0)
        return;

      if (name.empty())
        name = boost::str(boost::format("thread %lu") % dwThreadId);

      _set_thread_name(thread, name.c_str(), dwThreadId);
    }
#else
    void
    set_thread_name(gr_thread_t thread, std::string name)
    {
        /* Not implemented on mingw-w64 */
    }
#endif /* !__MINGW32__ */

  } /* namespace thread */
} /* namespace gr */


#elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__) || \
    defined(__FreeBSD__)

namespace gr {
  namespace thread {

    gr_thread_t
    get_current_thread_id()
    {
      // Not implemented on OSX
      return NULL;
    }

    void
    thread_bind_to_processor(int n)
    {
      // Not implemented on OSX
    }

    void
    thread_bind_to_processor(gr_thread_t thread, int n)
    {
      // Not implemented on OSX
    }

    void
    thread_bind_to_processor(const std::vector<int> &mask)
    {
      // Not implemented on OSX
    }

    void
    thread_bind_to_processor(gr_thread_t thread, const std::vector<int> &mask)
    {
      // Not implemented on OSX
    }

    void
    thread_unbind()
    {
      // Not implemented on OSX
    }

    void
    thread_unbind(gr_thread_t thread)
    {
      // Not implemented on OSX
    }

    int
    thread_priority(gr_thread_t thread)
    {
      sched_param param;
      int priority;
      int policy;
      int ret;
      ret = pthread_getschedparam (thread, &policy, &param);
      priority = param.sched_priority;
      return (ret==0)?priority:ret;
    }

    int
    set_thread_priority(gr_thread_t thread, int priority)
    {
      int policy;
      struct sched_param param;
      pthread_getschedparam (thread, &policy, &param);
      param.sched_priority = priority;
      return pthread_setschedparam(thread, policy, &param);
    }

    void
    set_thread_name(gr_thread_t thread, std::string name)
    {
      // Not implemented on OSX
    }

  } /* namespace thread */
} /* namespace gr */

#else

#include <sstream>
#include <stdexcept>
#include <pthread.h>
#include <sys/prctl.h>

namespace gr {
  namespace thread {

    gr_thread_t
    get_current_thread_id()
    {
      return pthread_self();
    }

    void
    thread_bind_to_processor(int n)
    {
      std::vector<int> mask(1, n);
      thread_bind_to_processor(get_current_thread_id(), mask);
    }

    void
    thread_bind_to_processor(const std::vector<int> &mask)
    {
      thread_bind_to_processor(get_current_thread_id(), mask);
    }

    void
    thread_bind_to_processor(gr_thread_t thread, int n)
    {
      std::vector<int> mask(1, n);
      thread_bind_to_processor(thread, mask);
    }

    void
    thread_bind_to_processor(gr_thread_t thread, const std::vector<int> &mask)
    {
      cpu_set_t set;
      size_t len = sizeof(cpu_set_t);
      std::vector<int> _mask = mask;
      std::vector<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());
      }
    }

    int
    thread_priority(gr_thread_t thread)
    {
      sched_param param;
      int priority;
      int policy;
      int ret;
      ret = pthread_getschedparam (thread, &policy, &param);
      priority = param.sched_priority;
      return (ret==0)?priority:ret;
    }

    int
    set_thread_priority(gr_thread_t thread, int priority)
    {
      int policy;
      struct sched_param param;
      pthread_getschedparam (thread, &policy, &param);
      param.sched_priority = priority;
      return pthread_setschedparam(thread, policy, &param);
    }

    void
    set_thread_name(gr_thread_t thread, std::string name)
    {
      if (thread != pthread_self()) // Naming another thread is not supported
        return;

      if (name.empty())
        name = boost::str(boost::format("thread %llu") % ((unsigned long long)thread));

      const int max_len = 16; // Maximum accepted by PR_SET_NAME

      if ((int)name.size() > max_len) // Shorten the name if necessary by taking as many characters from the front
      {                               // so that the unique_id can still fit on the end
        int i = name.size() - 1;
        for (; i >= 0; --i)
        {
          std::string s = name.substr(i, 1);
          int n = atoi(s.c_str());
          if ((n == 0) && (s != "0"))
            break;
        }

        name = name.substr(0, std::max(0, max_len - ((int)name.size() - (i + 1)))) + name.substr(i + 1);
      }

      prctl(PR_SET_NAME, name.c_str(), 0, 0, 0);
    }

  } /* namespace thread */
} /* namespace gr */

#endif