GNU Radio Manual and C++ API Reference  3.7.9.2
The Free & Open Software Radio Ecosystem
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
thread.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2009-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 #ifndef INCLUDED_THREAD_H
23 #define INCLUDED_THREAD_H
24 
25 #include <gnuradio/api.h>
26 #include <boost/thread/thread.hpp>
27 #include <boost/thread/mutex.hpp>
28 #include <boost/thread/locks.hpp>
29 #include <boost/thread/condition_variable.hpp>
30 #include <vector>
31 
32 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
33 
34 #ifndef WIN32_LEAN_AND_MEAN
35 #define WIN32_LEAN_AND_MEAN
36 #endif
37 
38 #include <windows.h>
39 
40 #endif
41 
42 namespace gr {
43  namespace thread {
44 
47  typedef boost::unique_lock<boost::mutex> scoped_lock;
49 
50  /*! \brief a system-dependent typedef for the underlying thread type.
51  */
52 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
53  typedef HANDLE gr_thread_t;
54 #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
55  typedef pthread_t gr_thread_t;
56 #else
57  typedef pthread_t gr_thread_t;
58 #endif
59 
60  /*! \brief Get the current thread's ID as a gr_thread_t
61  *
62  * We use this when setting the thread affinity or any other
63  * low-level thread settings. Can be called withing a GNU Radio
64  * block to get a reference to its current thread ID.
65  */
67 
68  /*! \brief Bind the current thread to a set of cores.
69  *
70  * Wrapper for system-dependent calls to set the affinity of the
71  * current thread to the processor mask. The mask is simply a
72  * 1-demensional vector containing the processor or core number
73  * from 0 to N-1 for N cores.
74  *
75  * Note: this does not work on OSX; it is a nop call since OSX
76  * does not support the concept of thread affinity (and what they
77  * do support in this way since 10.5 is not what we want or can
78  * use in this fashion).
79  */
80  GR_RUNTIME_API void thread_bind_to_processor(const std::vector<int> &mask);
81 
82  /*! \brief Convineince function to bind the current thread to a single core.
83  *
84  * Wrapper for system-dependent calls to set the affinity of the
85  * current thread to a given core from 0 to N-1 for N cores.
86  *
87  * Note: this does not work on OSX; it is a nop call since OSX
88  * does not support the concept of thread affinity (and what they
89  * do support in this way since 10.5 is not what we want or can
90  * use in this fashion).
91  */
93 
94  /*! \brief Bind a thread to a set of cores.
95  *
96  * Wrapper for system-dependent calls to set the affinity of the
97  * given thread ID to the processor mask. The mask is simply a
98  * 1-demensional vector containing the processor or core number
99  * from 0 to N-1 for N cores.
100  *
101  * Note: this does not work on OSX; it is a nop call since OSX
102  * does not support the concept of thread affinity (and what they
103  * do support in this way since 10.5 is not what we want or can
104  * use in this fashion).
105  */
106  GR_RUNTIME_API void thread_bind_to_processor(gr_thread_t thread,
107  const std::vector<int> &mask);
108 
109 
110  /*! \brief Convineince function to bind the a thread to a single core.
111  *
112  * Wrapper for system-dependent calls to set the affinity of the
113  * given thread ID to a given core from 0 to N-1 for N cores.
114  *
115  * Note: this does not work on OSX; it is a nop call since OSX
116  * does not support the concept of thread affinity (and what they
117  * do support in this way since 10.5 is not what we want or can
118  * use in this fashion).
119  */
120  GR_RUNTIME_API void thread_bind_to_processor(gr_thread_t thread,
121  unsigned int n);
122 
123  /*! \brief Remove any thread-processor affinity for the current thread.
124  *
125  * Note: this does not work on OSX; it is a nop call since OSX
126  * does not support the concept of thread affinity (and what they
127  * do support in this way since 10.5 is not what we want or can
128  * use in this fashion).
129  */
131 
132  /*! \brief Remove any thread-processor affinity for a given thread ID.
133  *
134  * Note: this does not work on OSX; it is a nop call since OSX
135  * does not support the concept of thread affinity (and what they
136  * do support in this way since 10.5 is not what we want or can
137  * use in this fashion).
138  */
139  GR_RUNTIME_API void thread_unbind(gr_thread_t thread);
140 
141  /*! \brief get current thread priority for a given thread ID
142  */
143  GR_RUNTIME_API int thread_priority(gr_thread_t thread);
144 
145  /*! \brief set current thread priority for a given thread ID
146  */
147  GR_RUNTIME_API int set_thread_priority(gr_thread_t thread, int priority);
148 
149  GR_RUNTIME_API void set_thread_name(gr_thread_t thread,
150  std::string name);
151 
152  } /* namespace thread */
153 } /* namespace gr */
154 
155 #endif /* INCLUDED_THREAD_H */
GR_RUNTIME_API gr_thread_t get_current_thread_id()
Get the current thread's ID as a gr_thread_t.
boost::unique_lock< boost::mutex > scoped_lock
Definition: thread.h:47
GR_RUNTIME_API void thread_bind_to_processor(const std::vector< int > &mask)
Bind the current thread to a set of cores.
pthread_t gr_thread_t
a system-dependent typedef for the underlying thread type.
Definition: thread.h:57
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:30
GR_RUNTIME_API int set_thread_priority(gr_thread_t thread, int priority)
set current thread priority for a given thread ID
boost::thread thread
Definition: thread.h:45
Include this header to use the message passing features.
Definition: logger.h:131
GR_RUNTIME_API int thread_priority(gr_thread_t thread)
get current thread priority for a given thread ID
boost::mutex mutex
Definition: thread.h:46
GR_RUNTIME_API void set_thread_name(gr_thread_t thread, std::string name)
GR_RUNTIME_API void thread_unbind()
Remove any thread-processor affinity for the current thread.
boost::condition_variable condition_variable
Definition: thread.h:48