OpenWalnut  1.5.0dev
WThreadedRunner.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 WTHREADEDRUNNER_H
26 #define WTHREADEDRUNNER_H
27 
28 #include <memory>
29 #include <stdint.h>
30 #include <string>
31 
32 #include <boost/function.hpp>
33 #include <boost/signals2.hpp>
34 #include <boost/thread.hpp>
35 #include <boost/thread/thread.hpp>
36 
37 #include "WFlag.h"
38 #include "WThreadedRunnerSignals.h"
39 
40 /**
41  * Base class for all classes needing to be executed in a separate thread.
42  */
43 class WThreadedRunner // NOLINT
44 {
45 public:
46  /**
47  * Abbreviation to a shared_ptr to this type
48  */
49  typedef std::shared_ptr< WThreadedRunner > SPtr;
50 
51  /**
52  * Abbreviation to a const shared_ptr to this type
53  */
54  typedef std::shared_ptr< const WThreadedRunner > ConstSPtr;
55 
56  /**
57  * Type used for simple thread functions.
58  */
59  typedef boost::function< void ( void ) > THREADFUNCTION;
60 
61  /**
62  * Default constructor.
63  */
65 
66  /**
67  * Destructor.
68  */
69  virtual ~WThreadedRunner();
70 
71  /**
72  * Run thread.
73  */
74  virtual void run();
75 
76  /**
77  * Run thread. This does not start threadMain(() but runs a specified function instead.
78  *
79  * \param f the function to run instead of the threadMain method.
80  */
81  void run( THREADFUNCTION f );
82 
83  /**
84  * Wait for the thread to be finished.
85  *
86  * \param requestFinish true if the thread should be notified.
87  */
88  void wait( bool requestFinish = false );
89 
90  /**
91  * This method's purpose is to request a stop without waiting for it.
92  */
93  virtual void requestStop();
94 
95  /**
96  * Connects a specified notify function with a signal this thread instance is offering.
97  *
98  * \exception WSignalSubscriptionFailed thrown if the signal can't be connected.
99  *
100  * \param signal the signal to connect to.
101  * \param notifier the notifier function to bind.
102  *
103  * \return connection descriptor.
104  */
105  virtual boost::signals2::connection subscribeSignal( THREAD_SIGNAL signal, t_ThreadErrorSignalHandlerType notifier );
106 
107  /**
108  * Checks whether this thread has been crashed. This will be true whenever the code in the thread throws an unhandled
109  * exception.
110  *
111  * \return true if there has been an exception during threadMain().
112  */
113  const WBoolFlag& isCrashed() const;
114 
115  /**
116  * Get the message of the exception finally causing the crash.
117  *
118  * \return the message
119  */
120  const std::string& getCrashMessage() const;
121 
122  /**
123  * Set the name of the thread. This can be handy for debugging as it gets set on Linux as the pthread name. You MUST set this before starting
124  * the thread.
125  *
126  * \param name the name
127  */
128  void setThreadName( std::string name );
129 
130  /**
131  * Returns the current thread name
132  *
133  * \return the name, empty if no name was specified.
134  */
135  std::string getThreadName() const;
136 
137  /**
138  * Static function to set the name of the calling thread.
139  *
140  * \param name the name.
141  */
142  static void setThisThreadName( std::string name );
143 protected:
144  /**
145  * Function that has to be overwritten for execution. It gets executed in a separate thread after run()
146  * has been called.
147  */
148  virtual void threadMain();
149 
150  /**
151  * Gets called when the thread should be stopped. The purpose of this method is to allow derived classes to handle this kind of event.
152  */
153  virtual void notifyStop();
154 
155  /**
156  * Thread instance.
157  */
158  boost::thread m_thread;
159 
160  /**
161  * Give remaining execution timeslice to another thread.
162  */
163  void yield() const;
164 
165  /**
166  * Sets thread asleep.
167  *
168  * \param t time to sleep in seconds.
169  */
170  void sleep( const int32_t t ) const;
171 
172  /**
173  * Sets thread asleep.
174  *
175  * \param t time to sleep in microseconds.
176  */
177  void msleep( const int32_t t ) const;
178 
179  /**
180  * Let the thread sleep until a stop request was given.
181  */
182  void waitForStop();
183 
184  /**
185  * Condition getting fired whenever the thread should quit. This is useful for waiting for stop requests.
186  */
188 
189  /**
190  * This method is called if an exception was caught, which came from the custom thread code. This method is virtual and allows you to
191  * overwrite the default behaviour. If you overwrite this method, you should call \ref handleDeadlyException or
192  * WThreadedRunner::onThreadException if you are finished with your customized code.
193  *
194  * \param e the exception that was caught.
195  */
196  virtual void onThreadException( const WException& e );
197 
198  /**
199  * Handle the specified exception which was not caught in the thread, which basically means the thread has crashed. This triggers the error
200  * notification and marks the thread as crashed. If you write your own exception/error mechanism (like \ref WModule), you should take care
201  * that these method gets called.
202  *
203  * \note this method does not re-throw the exception
204  * \note you should specify a custom sender string if you overwrite \ref onThreadException.
205  *
206  * \param e the exception
207  * \param sender allows to customize the sender information in the log entry created by this method.
208  */
209  void handleDeadlyException( const WException& e, std::string sender = "WThreadedRunner" );
210 
211  /**
212  * True whenever an exception is thrown during threadMain.
213  */
215 
216  /**
217  * The crash message. Only filled if m_isCrashed is true.
218  */
219  std::string m_crashMessage;
220 
221 private:
222  /**
223  * Disallow copy construction.
224  *
225  * \param rhs the other threaded runner.
226  */
228 
229  /**
230  * Disallow copy assignment.
231  *
232  * \param rhs the other threaded runner.
233  * \return this.
234  */
236 
237  /**
238  * Signal fired whenever a thread throws an exception/error.
239  */
240  t_ThreadErrorSignalType signal_thread_error;
241 
242  /**
243  * The is the thread entry point. It does exception handling and calls threadMain.
244  */
245  void threadMainSave();
246 
247  /**
248  * This threads name.
249  */
250  std::string m_threadName;
251 };
252 
253 #endif // WTHREADEDRUNNER_H
Basic exception handler.
Definition: WException.h:39
Base class for all classes needing to be executed in a separate thread.
void msleep(const int32_t t) const
Sets thread asleep.
virtual boost::signals2::connection subscribeSignal(THREAD_SIGNAL signal, t_ThreadErrorSignalHandlerType notifier)
Connects a specified notify function with a signal this thread instance is offering.
virtual void run()
Run thread.
void threadMainSave()
The is the thread entry point.
void yield() const
Give remaining execution timeslice to another thread.
boost::function< void(void) > THREADFUNCTION
Type used for simple thread functions.
const std::string & getCrashMessage() const
Get the message of the exception finally causing the crash.
std::string m_threadName
This threads name.
virtual void threadMain()
Function that has to be overwritten for execution.
std::string getThreadName() const
Returns the current thread name.
std::shared_ptr< WThreadedRunner > SPtr
Abbreviation to a shared_ptr to this type.
void setThreadName(std::string name)
Set the name of the thread.
std::string m_crashMessage
The crash message.
static void setThisThreadName(std::string name)
Static function to set the name of the calling thread.
WThreadedRunner(const WThreadedRunner &rhs)
Disallow copy construction.
void sleep(const int32_t t) const
Sets thread asleep.
void handleDeadlyException(const WException &e, std::string sender="WThreadedRunner")
Handle the specified exception which was not caught in the thread, which basically means the thread h...
WThreadedRunner()
Default constructor.
void waitForStop()
Let the thread sleep until a stop request was given.
virtual void requestStop()
This method's purpose is to request a stop without waiting for it.
virtual ~WThreadedRunner()
Destructor.
void wait(bool requestFinish=false)
Wait for the thread to be finished.
virtual void notifyStop()
Gets called when the thread should be stopped.
boost::thread m_thread
Thread instance.
WBoolFlag m_isCrashed
True whenever an exception is thrown during threadMain.
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.
std::shared_ptr< const WThreadedRunner > ConstSPtr
Abbreviation to a const shared_ptr to this type.
t_ThreadErrorSignalType signal_thread_error
Signal fired whenever a thread throws an exception/error.
const WBoolFlag & isCrashed() const
Checks whether this thread has been crashed.
WThreadedRunner & operator=(const WThreadedRunner &rhs)
Disallow copy assignment.
virtual void onThreadException(const WException &e)
This method is called if an exception was caught, which came from the custom thread code.