OpenWalnut  1.4.0
WWorkerThread.h
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 WWORKERTHREAD_H
00026 #define WWORKERTHREAD_H
00027 
00028 #include <string> // because of std::size_t
00029 #include <exception>
00030 
00031 #include <boost/shared_ptr.hpp>
00032 #include <boost/signals2/signal.hpp>
00033 
00034 #include "WAssert.h"
00035 #include "WException.h"
00036 #include "WThreadedRunner.h"
00037 
00038 /**
00039  * A worker thread that belongs to a \see WThreadedFunction object.
00040  */
00041 template< class Function_T >
00042 class WWorkerThread : public WThreadedRunner
00043 {
00044     // typedefs
00045     //! a type for stop signals
00046     typedef boost::signals2::signal< void () > StopSignal;
00047 
00048     //! a type for exception signals
00049     typedef boost::signals2::signal< void ( WException const& ) > ExceptionSignal;
00050 
00051 public:
00052     //typedefs
00053     //! a type for stop callbacks
00054     typedef boost::function< void () > StopFunction;
00055 
00056     //! a type for exception callbacks
00057     typedef boost::function< void ( WException const& ) > ExceptionFunction;
00058 
00059     /**
00060      * Default constructor.
00061      *
00062      * \param func A pointer to the function object.
00063      * \param id A thread id.
00064      * \param numThreads The number of threads.
00065      */
00066     WWorkerThread( boost::shared_ptr< Function_T > func, std::size_t id, std::size_t numThreads );
00067 
00068     /**
00069      * Default destructor.
00070      */
00071     virtual ~WWorkerThread();
00072 
00073     /**
00074      * Subscribe a function to the exception signal.
00075      *
00076      * \param func The function.
00077      */
00078     void subscribeExceptionSignal( ExceptionFunction func );
00079 
00080     /**
00081      * Subscribe a function to the stop signal.
00082      *
00083      * \param func The function.
00084      */
00085     void subscribeStopSignal( StopFunction func );
00086 
00087 protected:
00088     /**
00089      * The thread's main function.
00090      */
00091     virtual void threadMain();
00092 
00093 private:
00094     /**
00095      * WWorkerThread is non-copyable, so the copy constructor is not implemented.
00096      */
00097     WWorkerThread( WWorkerThread const& ); // NOLINT
00098 
00099     /**
00100      * WWorkerThread is non-copyable, so the copy operator is not implemented.
00101      *
00102      * \return this worker-thread.
00103      */
00104     WWorkerThread& operator = ( WWorkerThread const& );
00105 
00106     //! the functor called in threadMain()
00107     boost::shared_ptr< Function_T > m_func;
00108 
00109     //! a thread id between 0 and m_numThreads - 1
00110     std::size_t m_id;
00111 
00112     //! the number of threads
00113     std::size_t m_numThreads;
00114 
00115     //! the exception signal
00116     ExceptionSignal m_exceptionSignal;
00117 
00118     //! the stop signal
00119     StopSignal m_stopSignal;
00120 };
00121 
00122 template< class Function_T >
00123 WWorkerThread< Function_T >::WWorkerThread( boost::shared_ptr< Function_T > func, std::size_t id, std::size_t numThreads )
00124     : m_func( func ),
00125       m_id( id ),
00126       m_numThreads( numThreads ),
00127       m_exceptionSignal(),
00128       m_stopSignal()
00129 {
00130     if( id >= numThreads )
00131     {
00132         throw WException( std::string( "The id of this thread is not valid." ) );
00133     }
00134     if( !m_func )
00135     {
00136         throw WException( std::string( "No thread function provided!" ) );
00137     }
00138 }
00139 
00140 template< class Function_T >
00141 WWorkerThread< Function_T >::~WWorkerThread()
00142 {
00143     m_exceptionSignal.disconnect_all_slots();
00144     m_stopSignal.disconnect_all_slots();
00145 }
00146 
00147 template< class Function_T >
00148 void WWorkerThread< Function_T >::subscribeExceptionSignal( ExceptionFunction func )
00149 {
00150     if( func )
00151     {
00152         m_exceptionSignal.connect( func );
00153     }
00154 }
00155 
00156 template< class Function_T >
00157 void WWorkerThread< Function_T >::subscribeStopSignal( StopFunction func )
00158 {
00159     if( func )
00160     {
00161         m_stopSignal.connect( func );
00162     }
00163 }
00164 
00165 template< class Function_T >
00166 void WWorkerThread< Function_T >::threadMain()
00167 {
00168     if( m_func )
00169     {
00170         try
00171         {
00172             m_func->operator() ( m_id, m_numThreads, m_shutdownFlag );
00173         }
00174         catch( WException const& e )
00175         {
00176             m_exceptionSignal( e );
00177             return;
00178         }
00179         catch( std::exception const& e )
00180         {
00181             WException w( std::string( e.what() ) );
00182             m_exceptionSignal( w );
00183             return;
00184         }
00185         catch( ... )
00186         {
00187             WException w( std::string( "An exception was thrown." ) );
00188             m_exceptionSignal( w );
00189             return;
00190         }
00191     }
00192     m_stopSignal();
00193 }
00194 
00195 #endif  // WWORKERTHREAD_H