OpenWalnut  1.4.0
WWorkerThread.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WWORKERTHREAD_H
26 #define WWORKERTHREAD_H
27 
28 #include <string> // because of std::size_t
29 #include <exception>
30 
31 #ifndef Q_MOC_RUN
32 #include <boost/shared_ptr.hpp>
33 #endif
34 #ifndef Q_MOC_RUN
35 #include <boost/signals2/signal.hpp>
36 #endif
37 
38 #include "WAssert.h"
39 #include "WException.h"
40 #include "WThreadedRunner.h"
41 
42 /**
43  * A worker thread that belongs to a \see WThreadedFunction object.
44  */
45 template< class Function_T >
47 {
48  // typedefs
49  //! a type for stop signals
50  typedef boost::signals2::signal< void () > StopSignal;
51 
52  //! a type for exception signals
53  typedef boost::signals2::signal< void ( WException const& ) > ExceptionSignal;
54 
55 public:
56  //typedefs
57  //! a type for stop callbacks
58  typedef boost::function< void () > StopFunction;
59 
60  //! a type for exception callbacks
61  typedef boost::function< void ( WException const& ) > ExceptionFunction;
62 
63  /**
64  * Default constructor.
65  *
66  * \param func A pointer to the function object.
67  * \param id A thread id.
68  * \param numThreads The number of threads.
69  */
70  WWorkerThread( boost::shared_ptr< Function_T > func, std::size_t id, std::size_t numThreads );
71 
72  /**
73  * Default destructor.
74  */
75  virtual ~WWorkerThread();
76 
77  /**
78  * Subscribe a function to the exception signal.
79  *
80  * \param func The function.
81  */
82  void subscribeExceptionSignal( ExceptionFunction func );
83 
84  /**
85  * Subscribe a function to the stop signal.
86  *
87  * \param func The function.
88  */
89  void subscribeStopSignal( StopFunction func );
90 
91 protected:
92  /**
93  * The thread's main function.
94  */
95  virtual void threadMain();
96 
97 private:
98  /**
99  * WWorkerThread is non-copyable, so the copy constructor is not implemented.
100  */
101  WWorkerThread( WWorkerThread const& ); // NOLINT
102 
103  /**
104  * WWorkerThread is non-copyable, so the copy operator is not implemented.
105  *
106  * \return this worker-thread.
107  */
109 
110  //! the functor called in threadMain()
111  boost::shared_ptr< Function_T > m_func;
112 
113  //! a thread id between 0 and m_numThreads - 1
114  std::size_t m_id;
115 
116  //! the number of threads
117  std::size_t m_numThreads;
118 
119  //! the exception signal
120  ExceptionSignal m_exceptionSignal;
121 
122  //! the stop signal
123  StopSignal m_stopSignal;
124 };
125 
126 template< class Function_T >
127 WWorkerThread< Function_T >::WWorkerThread( boost::shared_ptr< Function_T > func, std::size_t id, std::size_t numThreads )
128  : m_func( func ),
129  m_id( id ),
130  m_numThreads( numThreads ),
131  m_exceptionSignal(),
132  m_stopSignal()
133 {
134  if( id >= numThreads )
135  {
136  throw WException( std::string( "The id of this thread is not valid." ) );
137  }
138  if( !m_func )
139  {
140  throw WException( std::string( "No thread function provided!" ) );
141  }
142 }
143 
144 template< class Function_T >
146 {
147  m_exceptionSignal.disconnect_all_slots();
148  m_stopSignal.disconnect_all_slots();
149 }
150 
151 template< class Function_T >
153 {
154  if( func )
155  {
156  m_exceptionSignal.connect( func );
157  }
158 }
159 
160 template< class Function_T >
162 {
163  if( func )
164  {
165  m_stopSignal.connect( func );
166  }
167 }
168 
169 template< class Function_T >
171 {
172  if( m_func )
173  {
174  try
175  {
176  m_func->operator() ( m_id, m_numThreads, m_shutdownFlag );
177  }
178  catch( WException const& e )
179  {
180  m_exceptionSignal( e );
181  return;
182  }
183  catch( std::exception const& e )
184  {
185  WException w( std::string( e.what() ) );
186  m_exceptionSignal( w );
187  return;
188  }
189  catch( ... )
190  {
191  WException w( std::string( "An exception was thrown." ) );
192  m_exceptionSignal( w );
193  return;
194  }
195  }
196  m_stopSignal();
197 }
198 
199 #endif // WWORKERTHREAD_H
boost::signals2::signal< void(WException const &) > ExceptionSignal
a type for exception signals
Definition: WWorkerThread.h:53
A worker thread that belongs to a.
Definition: WWorkerThread.h:46
std::size_t m_numThreads
the number of threads
ExceptionSignal m_exceptionSignal
the exception signal
void subscribeExceptionSignal(ExceptionFunction func)
Subscribe a function to the exception signal.
boost::function< void() > StopFunction
a type for stop callbacks
Definition: WWorkerThread.h:58
Base class for all classes needing to be executed in a separate thread.
boost::shared_ptr< Function_T > m_func
the functor called in threadMain()
boost::function< void(WException const &) > ExceptionFunction
a type for exception callbacks
Definition: WWorkerThread.h:61
void subscribeStopSignal(StopFunction func)
Subscribe a function to the stop signal.
virtual ~WWorkerThread()
Default destructor.
WWorkerThread(boost::shared_ptr< Function_T > func, std::size_t id, std::size_t numThreads)
Default constructor.
std::size_t m_id
a thread id between 0 and m_numThreads - 1
WWorkerThread & operator=(WWorkerThread const &)
WWorkerThread is non-copyable, so the copy operator is not implemented.
Basic exception handler.
Definition: WException.h:38
boost::signals2::signal< void() > StopSignal
a type for stop signals
Definition: WWorkerThread.h:50
virtual void threadMain()
The thread's main function.
StopSignal m_stopSignal
the stop signal