OpenWalnut  1.5.0dev
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 <exception>
29 #include <memory>
30 #include <string> // because of std::size_t
31 
32 #include <boost/signals2/signal.hpp>
33 
34 #include "WAssert.h"
35 #include "WException.h"
36 #include "WThreadedRunner.h"
37 
38 /**
39  * A worker thread that belongs to a \see WThreadedFunction object.
40  */
41 template< class Function_T >
43 {
44  // typedefs
45  //! a type for stop signals
46  typedef boost::signals2::signal< void () > StopSignal;
47 
48  //! a type for exception signals
49  typedef boost::signals2::signal< void ( WException const& ) > ExceptionSignal;
50 
51 public:
52  //typedefs
53  //! a type for stop callbacks
54  typedef boost::function< void () > StopFunction;
55 
56  //! a type for exception callbacks
57  typedef boost::function< void ( WException const& ) > ExceptionFunction;
58 
59  /**
60  * Default constructor.
61  *
62  * \param func A pointer to the function object.
63  * \param id A thread id.
64  * \param numThreads The number of threads.
65  */
66  WWorkerThread( std::shared_ptr< Function_T > func, std::size_t id, std::size_t numThreads );
67 
68  /**
69  * Default destructor.
70  */
71  virtual ~WWorkerThread();
72 
73  /**
74  * Subscribe a function to the exception signal.
75  *
76  * \param func The function.
77  */
79 
80  /**
81  * Subscribe a function to the stop signal.
82  *
83  * \param func The function.
84  */
85  void subscribeStopSignal( StopFunction func );
86 
87 protected:
88  /**
89  * The thread's main function.
90  */
91  virtual void threadMain();
92 
93 private:
94  /**
95  * WWorkerThread is non-copyable, so the copy constructor is not implemented.
96  */
97  WWorkerThread( WWorkerThread const& ); // NOLINT
98 
99  /**
100  * WWorkerThread is non-copyable, so the copy operator is not implemented.
101  *
102  * \return this worker-thread.
103  */
105 
106  //! the functor called in threadMain()
107  std::shared_ptr< Function_T > m_func;
108 
109  //! a thread id between 0 and m_numThreads - 1
110  std::size_t m_id;
111 
112  //! the number of threads
113  std::size_t m_numThreads;
114 
115  //! the exception signal
117 
118  //! the stop signal
120 };
121 
122 template< class Function_T >
123 WWorkerThread< Function_T >::WWorkerThread( std::shared_ptr< Function_T > func, std::size_t id, std::size_t numThreads )
124  : m_func( func ),
125  m_id( id ),
126  m_numThreads( numThreads ),
127  m_exceptionSignal(),
128  m_stopSignal()
129 {
130  if( id >= numThreads )
131  {
132  throw WException( std::string( "The id of this thread is not valid." ) );
133  }
134  if( !m_func )
135  {
136  throw WException( std::string( "No thread function provided!" ) );
137  }
138 }
139 
140 template< class Function_T >
142 {
143  m_exceptionSignal.disconnect_all_slots();
144  m_stopSignal.disconnect_all_slots();
145 }
146 
147 template< class Function_T >
149 {
150  if( func )
151  {
152  m_exceptionSignal.connect( func );
153  }
154 }
155 
156 template< class Function_T >
158 {
159  if( func )
160  {
161  m_stopSignal.connect( func );
162  }
163 }
164 
165 template< class Function_T >
167 {
168  if( m_func )
169  {
170  try
171  {
172  m_func->operator() ( m_id, m_numThreads, m_shutdownFlag );
173  }
174  catch( WException const& e )
175  {
176  m_exceptionSignal( e );
177  return;
178  }
179  catch( std::exception const& e )
180  {
181  WException w( std::string( e.what() ) );
182  m_exceptionSignal( w );
183  return;
184  }
185  catch( ... )
186  {
187  WException w( std::string( "An exception was thrown." ) );
188  m_exceptionSignal( w );
189  return;
190  }
191  }
192  m_stopSignal();
193 }
194 
195 #endif // WWORKERTHREAD_H
Basic exception handler.
Definition: WException.h:39
virtual const char * what() const
Returns the message string set on throw.
Definition: WException.cpp:90
Base class for all classes needing to be executed in a separate thread.
A worker thread that belongs to a.
Definition: WWorkerThread.h:43
WWorkerThread & operator=(WWorkerThread const &)
WWorkerThread is non-copyable, so the copy operator is not implemented.
boost::signals2::signal< void(WException const &) > ExceptionSignal
a type for exception signals
Definition: WWorkerThread.h:49
virtual void threadMain()
The thread's main function.
ExceptionSignal m_exceptionSignal
the exception signal
boost::function< void() > StopFunction
a type for stop callbacks
Definition: WWorkerThread.h:54
void subscribeStopSignal(StopFunction func)
Subscribe a function to the stop signal.
WWorkerThread(WWorkerThread const &)
WWorkerThread is non-copyable, so the copy constructor is not implemented.
boost::function< void(WException const &) > ExceptionFunction
a type for exception callbacks
Definition: WWorkerThread.h:57
void subscribeExceptionSignal(ExceptionFunction func)
Subscribe a function to the exception signal.
std::size_t m_id
a thread id between 0 and m_numThreads - 1
std::size_t m_numThreads
the number of threads
boost::signals2::signal< void() > StopSignal
a type for stop signals
Definition: WWorkerThread.h:46
std::shared_ptr< Function_T > m_func
the functor called in threadMain()
virtual ~WWorkerThread()
Default destructor.
WWorkerThread(std::shared_ptr< Function_T > func, std::size_t id, std::size_t numThreads)
Default constructor.
StopSignal m_stopSignal
the stop signal