OpenWalnut 1.2.5
|
00001 //--------------------------------------------------------------------------- 00002 // 00003 // Project: OpenWalnut ( http://www.openwalnut.org ) 00004 // 00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS 00006 // For more information see http://www.openwalnut.org/copying 00007 // 00008 // This file is part of OpenWalnut. 00009 // 00010 // OpenWalnut is free software: you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as published by 00012 // the Free Software Foundation, either version 3 of the License, or 00013 // (at your option) any later version. 00014 // 00015 // OpenWalnut is distributed in the hope that it will be useful, 00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 // GNU Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public License 00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>. 00022 // 00023 //--------------------------------------------------------------------------- 00024 00025 #ifndef WTHREADEDRUNNER_H 00026 #define WTHREADEDRUNNER_H 00027 00028 #include <stdint.h> 00029 00030 #include <boost/function.hpp> 00031 00032 #include <boost/thread.hpp> 00033 #include <boost/thread/thread.hpp> 00034 00035 #include "WFlag.h" 00036 #include "WExportCommon.h" 00037 00038 /** 00039 * Base class for all classes needing to be executed in a separate thread. 00040 */ 00041 class OWCOMMON_EXPORT WThreadedRunner // NOLINT 00042 { 00043 public: 00044 00045 /** 00046 * Type used for simple thread functions. 00047 */ 00048 typedef boost::function< void ( void ) > THREADFUNCTION; 00049 00050 /** 00051 * Default constructor. 00052 */ 00053 WThreadedRunner(); 00054 00055 /** 00056 * Destructor. 00057 */ 00058 virtual ~WThreadedRunner(); 00059 00060 /** 00061 * Run thread. 00062 */ 00063 virtual void run(); 00064 00065 /** 00066 * Run thread. This does not start threadMain(() but runs a specified function instead. 00067 * 00068 * \param f the function to run instead of the threadMain method. 00069 */ 00070 void run( THREADFUNCTION f ); 00071 00072 /** 00073 * Wait for the thread to be finished. 00074 * 00075 * \param requestFinish true if the thread should be notified. 00076 */ 00077 void wait( bool requestFinish = false ); 00078 00079 /** 00080 * This method's purpose is to request a stop without waiting for it. 00081 */ 00082 virtual void requestStop(); 00083 00084 protected: 00085 00086 /** 00087 * Function that has to be overwritten for execution. It gets executed in a separate thread after run() 00088 * has been called. 00089 */ 00090 virtual void threadMain(); 00091 00092 /** 00093 * Gets called when the thread should be stopped. The purpose of this method is to allow derived classes to handle this kind of event. 00094 */ 00095 virtual void notifyStop(); 00096 00097 /** 00098 * Thread instance. 00099 */ 00100 boost::thread m_thread; 00101 00102 /** 00103 * Give remaining execution timeslice to another thread. 00104 */ 00105 void yield() const; 00106 00107 /** 00108 * Sets thread asleep. 00109 * 00110 * \param t time to sleep in seconds. 00111 */ 00112 void sleep( const int32_t t ) const; 00113 00114 /** 00115 * Sets thread asleep. 00116 * 00117 * \param t time to sleep in microseconds. 00118 */ 00119 void msleep( const int32_t t ) const; 00120 00121 /** 00122 * Let the thread sleep until a stop request was given. 00123 */ 00124 void waitForStop(); 00125 00126 /** 00127 * Condition getting fired whenever the thread should quit. This is useful for waiting for stop requests. 00128 */ 00129 WBoolFlag m_shutdownFlag; 00130 00131 private: 00132 00133 /** 00134 * Disallow copy construction. 00135 * 00136 * \param rhs the other threaded runner. 00137 */ 00138 WThreadedRunner( const WThreadedRunner & rhs ); 00139 00140 /** 00141 * Disallow copy assignment. 00142 * 00143 * \param rhs the other threaded runner. 00144 * \return this. 00145 */ 00146 WThreadedRunner& operator=( const WThreadedRunner & rhs ); 00147 }; 00148 00149 #endif // WTHREADEDRUNNER_H