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