GNU Radio Manual and C++ API Reference  3.10.9.1
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  * SPDX-License-Identifier: GPL-3.0-or-later
8  *
9  */
10 
11 #ifndef INCLUDED_THREAD_H
12 #define INCLUDED_THREAD_H
13 
14 #include <gnuradio/api.h>
15 #include <boost/thread/barrier.hpp>
16 #include <boost/thread/condition_variable.hpp>
17 #include <boost/thread/locks.hpp>
18 #include <boost/thread/mutex.hpp>
19 #include <boost/thread/thread.hpp>
20 #include <memory>
21 #include <vector>
22 
23 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
24 
25 #ifndef WIN32_LEAN_AND_MEAN
26 #define WIN32_LEAN_AND_MEAN
27 #endif
28 
29 #include <windows.h>
30 
31 #endif
32 
33 namespace gr {
34 namespace thread {
35 
38 typedef boost::unique_lock<boost::mutex> scoped_lock;
41 typedef std::shared_ptr<barrier> barrier_sptr;
42 
43 /*! \brief a system-dependent typedef for the underlying thread type.
44  */
45 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
46 typedef HANDLE gr_thread_t;
47 #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
48 typedef pthread_t gr_thread_t;
49 #else
50 typedef pthread_t gr_thread_t;
51 #endif
52 
53 /*! \brief Get the current thread's ID as a gr_thread_t
54  *
55  * We use this when setting the thread affinity or any other
56  * low-level thread settings. Can be called within a GNU Radio
57  * block to get a reference to its current thread ID.
58  */
60 
61 /*! \brief Bind the current thread to a set of cores.
62  *
63  * Wrapper for system-dependent calls to set the affinity of the
64  * current thread to the processor mask. The mask is simply a
65  * 1-demensional vector containing the processor or core number
66  * from 0 to N-1 for N cores.
67  *
68  * Note: this does not work on OSX; it is a nop call since OSX
69  * does not support the concept of thread affinity (and what they
70  * do support in this way since 10.5 is not what we want or can
71  * use in this fashion).
72  */
73 GR_RUNTIME_API void thread_bind_to_processor(const std::vector<int>& mask);
74 
75 /*! \brief Convineince function to bind the current thread to a single core.
76  *
77  * Wrapper for system-dependent calls to set the affinity of the
78  * current thread to a given core from 0 to N-1 for N cores.
79  *
80  * Note: this does not work on OSX; it is a nop call since OSX
81  * does not support the concept of thread affinity (and what they
82  * do support in this way since 10.5 is not what we want or can
83  * use in this fashion).
84  */
86 
87 /*! \brief Bind a thread to a set of cores.
88  *
89  * Wrapper for system-dependent calls to set the affinity of the
90  * given thread ID to the processor mask. The mask is simply a
91  * 1-demensional vector containing the processor or core number
92  * from 0 to N-1 for N cores.
93  *
94  * Note: this does not work on OSX; it is a nop call since OSX
95  * does not support the concept of thread affinity (and what they
96  * do support in this way since 10.5 is not what we want or can
97  * use in this fashion).
98  */
100  const std::vector<int>& mask);
101 
102 
103 /*! \brief Convineince function to bind the a thread to a single core.
104  *
105  * Wrapper for system-dependent calls to set the affinity of the
106  * given thread ID to a given core from 0 to N-1 for N cores.
107  *
108  * Note: this does not work on OSX; it is a nop call since OSX
109  * does not support the concept of thread affinity (and what they
110  * do support in this way since 10.5 is not what we want or can
111  * use in this fashion).
112  */
114 
115 /*! \brief Remove any thread-processor affinity for the current thread.
116  *
117  * Note: this does not work on OSX; it is a nop call since OSX
118  * does not support the concept of thread affinity (and what they
119  * do support in this way since 10.5 is not what we want or can
120  * use in this fashion).
121  */
123 
124 /*! \brief Remove any thread-processor affinity for a given thread ID.
125  *
126  * Note: this does not work on OSX; it is a nop call since OSX
127  * does not support the concept of thread affinity (and what they
128  * do support in this way since 10.5 is not what we want or can
129  * use in this fashion).
130  */
132 
133 /*! \brief get current thread priority for a given thread ID
134  */
136 
137 /*! \brief set current thread priority for a given thread ID
138  */
140 
142 
143 } /* namespace thread */
144 } /* namespace gr */
145 
146 #endif /* INCLUDED_THREAD_H */
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
boost::barrier barrier
Definition: thread.h:40
boost::mutex mutex
Definition: thread.h:37
boost::unique_lock< boost::mutex > scoped_lock
Definition: thread.h:38
GR_RUNTIME_API void set_thread_name(gr_thread_t thread, std::string name)
GR_RUNTIME_API int thread_priority(gr_thread_t thread)
get current thread priority for a given thread ID
GR_RUNTIME_API int set_thread_priority(gr_thread_t thread, int priority)
set current thread priority for a given thread ID
GR_RUNTIME_API void thread_unbind()
Remove any thread-processor affinity for the current thread.
boost::thread thread
Definition: thread.h:36
pthread_t gr_thread_t
a system-dependent typedef for the underlying thread type.
Definition: thread.h:50
GR_RUNTIME_API void thread_bind_to_processor(const std::vector< int > &mask)
Bind the current thread to a set of cores.
boost::condition_variable condition_variable
Definition: thread.h:39
GR_RUNTIME_API gr_thread_t get_current_thread_id()
Get the current thread's ID as a gr_thread_t.
GNU Radio logging wrapper.
Definition: basic_block.h:29