GNU Radio 3.6.5 C++ API

thread.h

Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2009-2012 Free Software Foundation, Inc.
00004  *
00005  * This file is part of GNU Radio
00006  *
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  *
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License along
00018  * with this program; if not, write to the Free Software Foundation, Inc.,
00019  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00020  */
00021 #ifndef INCLUDED_THREAD_H
00022 #define INCLUDED_THREAD_H
00023 
00024 #include <gruel/api.h>
00025 #include <boost/thread/thread.hpp>
00026 #include <boost/thread/mutex.hpp>
00027 #include <boost/thread/locks.hpp>
00028 #include <boost/thread/condition_variable.hpp>
00029 #include <vector>
00030 
00031 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
00032 
00033 #ifndef WIN32_LEAN_AND_MEAN
00034 #define WIN32_LEAN_AND_MEAN
00035 #endif
00036 
00037 #include <windows.h>
00038 
00039 #endif
00040 
00041 namespace gruel {
00042 
00043   typedef boost::thread                    thread;
00044   typedef boost::mutex                     mutex;
00045   typedef boost::unique_lock<boost::mutex> scoped_lock;
00046   typedef boost::condition_variable        condition_variable;
00047 
00048   /*! \brief a system-dependent typedef for the underlying thread type.
00049    */
00050 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
00051   typedef HANDLE gr_thread_t;
00052 #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
00053   typedef pthread_t gr_thread_t;
00054 #else
00055   typedef pthread_t gr_thread_t;
00056 #endif
00057 
00058   /*! \brief Get the current thread's ID as a gr_thread_t
00059    *
00060    * We use this when setting the thread affinity or any other
00061    * low-level thread settings. Can be called withing a GNU Radio
00062    * block to get a reference to its current thread ID.
00063    */
00064   GRUEL_API gr_thread_t get_current_thread_id();
00065 
00066   /*! \brief Bind the current thread to a set of cores.
00067    *
00068    * Wrapper for system-dependent calls to set the affinity of the
00069    * current thread to the processor mask. The mask is simply a
00070    * 1-demensional vector containing the processor or core number from
00071    * 0 to N-1 for N cores.
00072    *
00073    * Note: this does not work on OSX; it is a nop call since OSX does
00074    * not support the concept of thread affinity (and what they do
00075    * support in this way since 10.5 is not what we want or can use in
00076    * this fashion).
00077    */
00078   GRUEL_API void thread_bind_to_processor(const std::vector<int> &mask);
00079 
00080   /*! \brief Convineince function to bind the current thread to a single core.
00081    *
00082    * Wrapper for system-dependent calls to set the affinity of the
00083    * current thread to a given core from 0 to N-1 for N cores.
00084    *
00085    * Note: this does not work on OSX; it is a nop call since OSX does
00086    * not support the concept of thread affinity (and what they do
00087    * support in this way since 10.5 is not what we want or can use in
00088    * this fashion).
00089    */
00090   GRUEL_API void thread_bind_to_processor(int n);
00091 
00092   /*! \brief Bind a thread to a set of cores.
00093    *
00094    * Wrapper for system-dependent calls to set the affinity of the
00095    * given thread ID to the processor mask. The mask is simply a
00096    * 1-demensional vector containing the processor or core number from
00097    * 0 to N-1 for N cores.
00098    *
00099    * Note: this does not work on OSX; it is a nop call since OSX does
00100    * not support the concept of thread affinity (and what they do
00101    * support in this way since 10.5 is not what we want or can use in
00102    * this fashion).
00103    */
00104   GRUEL_API void thread_bind_to_processor(gr_thread_t thread, const std::vector<int> &mask);
00105 
00106 
00107   /*! \brief Convineince function to bind the a thread to a single core.
00108    *
00109    * Wrapper for system-dependent calls to set the affinity of the
00110    * given thread ID to a given core from 0 to N-1 for N cores.
00111    *
00112    * Note: this does not work on OSX; it is a nop call since OSX does
00113    * not support the concept of thread affinity (and what they do
00114    * support in this way since 10.5 is not what we want or can use in
00115    * this fashion).
00116    */
00117   GRUEL_API void thread_bind_to_processor(gr_thread_t thread, unsigned int n);
00118 
00119   /*! \brief Remove any thread-processor affinity for the current thread.
00120    *
00121    * Note: this does not work on OSX; it is a nop call since OSX does
00122    * not support the concept of thread affinity (and what they do
00123    * support in this way since 10.5 is not what we want or can use in
00124    * this fashion).
00125    */
00126   GRUEL_API void thread_unbind();
00127 
00128   /*! \brief Remove any thread-processor affinity for a given thread ID.
00129    *
00130    * Note: this does not work on OSX; it is a nop call since OSX does
00131    * not support the concept of thread affinity (and what they do
00132    * support in this way since 10.5 is not what we want or can use in
00133    * this fashion).
00134    */
00135   GRUEL_API void thread_unbind(gr_thread_t thread);
00136 
00137 } /* namespace gruel */
00138 
00139 #endif /* INCLUDED_THREAD_H */