GNU Radio 3.7.1 C++ API
thread.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2009-2013 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 
00022 #ifndef INCLUDED_THREAD_H
00023 #define INCLUDED_THREAD_H
00024 
00025 #include <gnuradio/api.h>
00026 #include <boost/thread/thread.hpp>
00027 #include <boost/thread/mutex.hpp>
00028 #include <boost/thread/locks.hpp>
00029 #include <boost/thread/condition_variable.hpp>
00030 #include <vector>
00031 
00032 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
00033 
00034 #ifndef WIN32_LEAN_AND_MEAN
00035 #define WIN32_LEAN_AND_MEAN
00036 #endif
00037 
00038 #include <windows.h>
00039 
00040 #endif
00041 
00042 namespace gr {
00043   namespace thread {
00044 
00045     typedef boost::thread                    thread;
00046     typedef boost::mutex                     mutex;
00047     typedef boost::unique_lock<boost::mutex> scoped_lock;
00048     typedef boost::condition_variable        condition_variable;
00049 
00050     /*! \brief a system-dependent typedef for the underlying thread type.
00051      */
00052 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
00053     typedef HANDLE gr_thread_t;
00054 #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
00055     typedef pthread_t gr_thread_t;
00056 #else
00057     typedef pthread_t gr_thread_t;
00058 #endif
00059 
00060     /*! \brief Get the current thread's ID as a gr_thread_t
00061      *
00062      * We use this when setting the thread affinity or any other
00063      * low-level thread settings. Can be called withing a GNU Radio
00064      * block to get a reference to its current thread ID.
00065      */
00066     GR_RUNTIME_API gr_thread_t get_current_thread_id();
00067 
00068     /*! \brief Bind the current thread to a set of cores.
00069      *
00070      * Wrapper for system-dependent calls to set the affinity of the
00071      * current thread to the processor mask. The mask is simply a
00072      * 1-demensional vector containing the processor or core number
00073      * from 0 to N-1 for N cores.
00074      *
00075      * Note: this does not work on OSX; it is a nop call since OSX
00076      * does not support the concept of thread affinity (and what they
00077      * do support in this way since 10.5 is not what we want or can
00078      * use in this fashion).
00079      */
00080     GR_RUNTIME_API void thread_bind_to_processor(const std::vector<int> &mask);
00081 
00082     /*! \brief Convineince function to bind the current thread to a single core.
00083      *
00084      * Wrapper for system-dependent calls to set the affinity of the
00085      * current thread to a given core from 0 to N-1 for N cores.
00086      *
00087      * Note: this does not work on OSX; it is a nop call since OSX
00088      * does not support the concept of thread affinity (and what they
00089      * do support in this way since 10.5 is not what we want or can
00090      * use in this fashion).
00091      */
00092     GR_RUNTIME_API void thread_bind_to_processor(int n);
00093 
00094     /*! \brief Bind a thread to a set of cores.
00095      *
00096      * Wrapper for system-dependent calls to set the affinity of the
00097      * given thread ID to the processor mask. The mask is simply a
00098      * 1-demensional vector containing the processor or core number
00099      * from 0 to N-1 for N cores.
00100      *
00101      * Note: this does not work on OSX; it is a nop call since OSX
00102      * does not support the concept of thread affinity (and what they
00103      * do support in this way since 10.5 is not what we want or can
00104      * use in this fashion).
00105      */
00106     GR_RUNTIME_API void thread_bind_to_processor(gr_thread_t thread,
00107                                                  const std::vector<int> &mask);
00108 
00109 
00110     /*! \brief Convineince function to bind the a thread to a single core.
00111      *
00112      * Wrapper for system-dependent calls to set the affinity of the
00113      * given thread ID to a given core from 0 to N-1 for N cores.
00114      *
00115      * Note: this does not work on OSX; it is a nop call since OSX
00116      * does not support the concept of thread affinity (and what they
00117      * do support in this way since 10.5 is not what we want or can
00118      * use in this fashion).
00119      */
00120     GR_RUNTIME_API void thread_bind_to_processor(gr_thread_t thread,
00121                                                  unsigned int n);
00122 
00123     /*! \brief Remove any thread-processor affinity for the current thread.
00124      *
00125      * Note: this does not work on OSX; it is a nop call since OSX
00126      * does not support the concept of thread affinity (and what they
00127      * do support in this way since 10.5 is not what we want or can
00128      * use in this fashion).
00129      */
00130     GR_RUNTIME_API void thread_unbind();
00131 
00132     /*! \brief Remove any thread-processor affinity for a given thread ID.
00133      *
00134      * Note: this does not work on OSX; it is a nop call since OSX
00135      * does not support the concept of thread affinity (and what they
00136      * do support in this way since 10.5 is not what we want or can
00137      * use in this fashion).
00138      */
00139     GR_RUNTIME_API void thread_unbind(gr_thread_t thread);
00140 
00141     /*! \brief get current thread priority for a given thread ID
00142      *
00143      * Note: this does not work on OSX
00144      */
00145     GR_RUNTIME_API int thread_priority(gr_thread_t thread);
00146     
00147     /*! \brief get current thread priority for a given thread ID
00148      *
00149      * Note: this does not work on OSX
00150      */
00151     GR_RUNTIME_API int set_thread_priority(gr_thread_t thread, int priority);
00152 
00153   } /* namespace thread */
00154 } /* namespace gr */
00155 
00156 #endif /* INCLUDED_THREAD_H */