1 | // |
---|
2 | // Timer.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/Timer.h#6 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Threading |
---|
8 | // Module: Timer |
---|
9 | // |
---|
10 | // Definition of the Timer and related classes. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_Timer_INCLUDED |
---|
40 | #define Foundation_Timer_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/Runnable.h" |
---|
45 | #include "Poco/Mutex.h" |
---|
46 | #include "Poco/Event.h" |
---|
47 | #include "Poco/Thread.h" |
---|
48 | #include "Poco/Timestamp.h" |
---|
49 | |
---|
50 | |
---|
51 | namespace Poco { |
---|
52 | |
---|
53 | |
---|
54 | class AbstractTimerCallback; |
---|
55 | class ThreadPool; |
---|
56 | |
---|
57 | |
---|
58 | class Foundation_API Timer: protected Runnable |
---|
59 | /// This class implements a thread-based timer. |
---|
60 | /// A timer starts a thread that first waits for a given start interval. |
---|
61 | /// Once that interval expires, the timer callback is called repeatedly |
---|
62 | /// in the given periodic interval. If the interval is 0, the timer is only |
---|
63 | /// called once. |
---|
64 | /// The timer callback method can stop the timer by setting the |
---|
65 | /// timer's periodic interval to 0. |
---|
66 | /// |
---|
67 | /// The timer callback runs in its own thread, so multithreading |
---|
68 | /// issues (proper synchronization) have to be considered when writing |
---|
69 | /// the callback method. |
---|
70 | /// |
---|
71 | /// The exact interval at which the callback is called depends on many |
---|
72 | /// factors like operating system, CPU performance and system load and |
---|
73 | /// may differ from the specified interval. |
---|
74 | /// |
---|
75 | /// The time needed to execute the timer callback is not included |
---|
76 | /// in the interval between invocations. For example, if the interval |
---|
77 | /// is 500 milliseconds, and the callback needs 400 milliseconds to |
---|
78 | /// execute, the callback function is nevertheless called every 500 |
---|
79 | /// milliseconds. If the callback takes longer to execute than the |
---|
80 | /// interval, the callback function will be immediately called again |
---|
81 | /// once it returns. |
---|
82 | /// |
---|
83 | /// The timer thread is taken from a thread pool, so |
---|
84 | /// there is a limit to the number of available concurrent timers. |
---|
85 | { |
---|
86 | public: |
---|
87 | Timer(long startInterval = 0, long periodicInterval = 0); |
---|
88 | /// Creates a new timer object. StartInterval and periodicInterval |
---|
89 | /// are given in milliseconds. If a periodicInterval of zero is |
---|
90 | /// specified, the callback will only be called once, after the |
---|
91 | /// startInterval expires. |
---|
92 | /// To start the timer, call the Start() method. |
---|
93 | |
---|
94 | virtual ~Timer(); |
---|
95 | /// Stops and destroys the timer. |
---|
96 | |
---|
97 | void start(const AbstractTimerCallback& method); |
---|
98 | /// Starts the timer. |
---|
99 | /// Create the TimerCallback as follows: |
---|
100 | /// TimerCallback<MyClass> callback(*this, &MyClass::onTimer); |
---|
101 | /// timer.start(callback); |
---|
102 | /// |
---|
103 | /// The timer thread is taken from the global default thread pool. |
---|
104 | |
---|
105 | void start(const AbstractTimerCallback& method, Thread::Priority priority); |
---|
106 | /// Starts the timer in a thread with the given priority. |
---|
107 | /// Create the TimerCallback as follows: |
---|
108 | /// TimerCallback<MyClass> callback(*this, &MyClass::onTimer); |
---|
109 | /// timer.start(callback); |
---|
110 | /// |
---|
111 | /// The timer thread is taken from the global default thread pool. |
---|
112 | |
---|
113 | void start(const AbstractTimerCallback& method, ThreadPool& threadPool); |
---|
114 | /// Starts the timer. |
---|
115 | /// Create the TimerCallback as follows: |
---|
116 | /// TimerCallback<MyClass> callback(*this, &MyClass::onTimer); |
---|
117 | /// timer.start(callback); |
---|
118 | |
---|
119 | void start(const AbstractTimerCallback& method, Thread::Priority priority, ThreadPool& threadPool); |
---|
120 | /// Starts the timer in a thread with the given priority. |
---|
121 | /// Create the TimerCallback as follows: |
---|
122 | /// TimerCallback<MyClass> callback(*this, &MyClass::onTimer); |
---|
123 | /// timer.start(callback); |
---|
124 | |
---|
125 | void stop(); |
---|
126 | /// Stops the timer. If the callback method is currently running |
---|
127 | /// it will be allowed to finish first. |
---|
128 | /// WARNING: Never call this method from within the callback method, |
---|
129 | /// as a deadlock would result. To stop the timer from within the |
---|
130 | /// callback method, call restart(0). |
---|
131 | |
---|
132 | void restart(); |
---|
133 | /// Restarts the periodic interval. If the callback method is already running, |
---|
134 | /// nothing will happen. |
---|
135 | |
---|
136 | void restart(long milliseconds); |
---|
137 | /// Sets a new periodic interval and restarts the timer. |
---|
138 | /// An interval of 0 will stop the timer. |
---|
139 | |
---|
140 | long getStartInterval() const; |
---|
141 | /// Returns the start interval. |
---|
142 | |
---|
143 | void setStartInterval(long milliseconds); |
---|
144 | /// Sets the start interval. Will only be |
---|
145 | /// effective before start() is called. |
---|
146 | |
---|
147 | long getPeriodicInterval() const; |
---|
148 | /// Returns the periodic interval. |
---|
149 | |
---|
150 | void setPeriodicInterval(long milliseconds); |
---|
151 | /// Sets the periodic interval. If the timer is already running |
---|
152 | /// the new interval will be effective when the current interval |
---|
153 | /// expires. |
---|
154 | |
---|
155 | protected: |
---|
156 | void run(); |
---|
157 | |
---|
158 | private: |
---|
159 | volatile long _startInterval; |
---|
160 | volatile long _periodicInterval; |
---|
161 | Event _wakeUp; |
---|
162 | Event _done; |
---|
163 | AbstractTimerCallback* _pCallback; |
---|
164 | Timestamp _nextInvocation; |
---|
165 | mutable FastMutex _mutex; |
---|
166 | |
---|
167 | Timer(const Timer&); |
---|
168 | Timer& operator = (const Timer&); |
---|
169 | }; |
---|
170 | |
---|
171 | |
---|
172 | class Foundation_API AbstractTimerCallback |
---|
173 | /// This is the base class for all instantiations of |
---|
174 | /// the TimerCallback template. |
---|
175 | { |
---|
176 | public: |
---|
177 | AbstractTimerCallback(); |
---|
178 | AbstractTimerCallback(const AbstractTimerCallback& callback); |
---|
179 | virtual ~AbstractTimerCallback(); |
---|
180 | |
---|
181 | AbstractTimerCallback& operator = (const AbstractTimerCallback& callback); |
---|
182 | |
---|
183 | virtual void invoke(Timer& timer) const = 0; |
---|
184 | virtual AbstractTimerCallback* clone() const = 0; |
---|
185 | }; |
---|
186 | |
---|
187 | |
---|
188 | template <class C> |
---|
189 | class TimerCallback: public AbstractTimerCallback |
---|
190 | /// This template class implements an adapter that sits between |
---|
191 | /// a Timer and an object's method invoked by the timer. |
---|
192 | /// It is quite similar in concept to the RunnableAdapter, but provides |
---|
193 | /// some Timer specific additional methods. |
---|
194 | /// See the Timer class for information on how |
---|
195 | /// to use this template class. |
---|
196 | { |
---|
197 | public: |
---|
198 | typedef void (C::*Callback)(Timer&); |
---|
199 | |
---|
200 | TimerCallback(C& object, Callback method): _pObject(&object), _method(method) |
---|
201 | { |
---|
202 | } |
---|
203 | |
---|
204 | TimerCallback(const TimerCallback& callback): _pObject(callback._pObject), _method(callback._method) |
---|
205 | { |
---|
206 | } |
---|
207 | |
---|
208 | ~TimerCallback() |
---|
209 | { |
---|
210 | } |
---|
211 | |
---|
212 | TimerCallback& operator = (const TimerCallback& callback) |
---|
213 | { |
---|
214 | if (&callback != this) |
---|
215 | { |
---|
216 | _pObject = callback._pObject; |
---|
217 | _method = callback._method; |
---|
218 | } |
---|
219 | return *this; |
---|
220 | } |
---|
221 | |
---|
222 | void invoke(Timer& timer) const |
---|
223 | { |
---|
224 | (_pObject->*_method)(timer); |
---|
225 | } |
---|
226 | |
---|
227 | AbstractTimerCallback* clone() const |
---|
228 | { |
---|
229 | return new TimerCallback(*this); |
---|
230 | } |
---|
231 | |
---|
232 | private: |
---|
233 | TimerCallback(); |
---|
234 | |
---|
235 | C* _pObject; |
---|
236 | Callback _method; |
---|
237 | }; |
---|
238 | |
---|
239 | |
---|
240 | } // namespace Poco |
---|
241 | |
---|
242 | |
---|
243 | #endif // Foundation_Timer_INCLUDED |
---|