OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 <stdint.h>
29 
30 #include <boost/function.hpp>
31 
32 #include <boost/thread.hpp>
33 #include <boost/thread/thread.hpp>
34 
35 #include "WFlag.h"
36 #include "WExportCommon.h"
37 
38 /**
39  * Base class for all classes needing to be executed in a separate thread.
40  */
41 class OWCOMMON_EXPORT WThreadedRunner // NOLINT
42 {
43 public:
44 
45  /**
46  * Type used for simple thread functions.
47  */
48  typedef boost::function< void ( void ) > THREADFUNCTION;
49 
50  /**
51  * Default constructor.
52  */
54 
55  /**
56  * Destructor.
57  */
58  virtual ~WThreadedRunner();
59 
60  /**
61  * Run thread.
62  */
63  virtual void run();
64 
65  /**
66  * Run thread. This does not start threadMain(() but runs a specified function instead.
67  *
68  * \param f the function to run instead of the threadMain method.
69  */
70  void run( THREADFUNCTION f );
71 
72  /**
73  * Wait for the thread to be finished.
74  *
75  * \param requestFinish true if the thread should be notified.
76  */
77  void wait( bool requestFinish = false );
78 
79  /**
80  * This method's purpose is to request a stop without waiting for it.
81  */
82  virtual void requestStop();
83 
84 protected:
85 
86  /**
87  * Function that has to be overwritten for execution. It gets executed in a separate thread after run()
88  * has been called.
89  */
90  virtual void threadMain();
91 
92  /**
93  * Gets called when the thread should be stopped. The purpose of this method is to allow derived classes to handle this kind of event.
94  */
95  virtual void notifyStop();
96 
97  /**
98  * Thread instance.
99  */
100  boost::thread m_thread;
101 
102  /**
103  * Give remaining execution timeslice to another thread.
104  */
105  void yield() const;
106 
107  /**
108  * Sets thread asleep.
109  *
110  * \param t time to sleep in seconds.
111  */
112  void sleep( const int32_t t ) const;
113 
114  /**
115  * Sets thread asleep.
116  *
117  * \param t time to sleep in microseconds.
118  */
119  void msleep( const int32_t t ) const;
120 
121  /**
122  * Let the thread sleep until a stop request was given.
123  */
124  void waitForStop();
125 
126  /**
127  * Condition getting fired whenever the thread should quit. This is useful for waiting for stop requests.
128  */
130 
131 private:
132 
133  /**
134  * Disallow copy construction.
135  *
136  * \param rhs the other threaded runner.
137  */
138  WThreadedRunner( const WThreadedRunner & rhs );
139 
140  /**
141  * Disallow copy assignment.
142  *
143  * \param rhs the other threaded runner.
144  * \return this.
145  */
146  WThreadedRunner& operator=( const WThreadedRunner & rhs );
147 };
148 
149 #endif // WTHREADEDRUNNER_H