]> granicus.if.org Git - icinga2/blob - lib/base/timer.cpp
Fix an issue where expired Timer pointers caused other timers to be delayed.
[icinga2] / lib / base / timer.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "base/timer.h"
21 #include "base/application.h"
22 #include "base/debug.h"
23 #include "base/utility.h"
24 #include <boost/bind.hpp>
25 #include <boost/foreach.hpp>
26 #include <boost/thread/thread.hpp>
27 #include <boost/thread/mutex.hpp>
28 #include <boost/thread/condition_variable.hpp>
29 #include <boost/multi_index_container.hpp>
30 #include <boost/multi_index/ordered_index.hpp>
31 #include <boost/multi_index/key_extractors.hpp>
32
33 using namespace icinga;
34
35 struct TimerHolder
36 {
37         Timer::WeakPtr Object;
38         double Next;
39 };
40
41 typedef boost::multi_index_container<
42         TimerHolder,
43         boost::multi_index::indexed_by<
44                 boost::multi_index::ordered_unique<boost::multi_index::member<TimerHolder, Timer::WeakPtr, &TimerHolder::Object> >,
45                 boost::multi_index::ordered_non_unique<boost::multi_index::member<TimerHolder, double, &TimerHolder::Next> >
46         >
47 > TimerSet;
48
49 static boost::mutex l_Mutex;
50 static boost::condition_variable l_CV;
51 static boost::thread l_Thread;
52 static bool l_StopThread;
53 static TimerSet l_Timers;
54
55 /**
56  * Constructor for the Timer class.
57  */
58 Timer::Timer(void)
59         : m_Interval(0), m_Next(0)
60 { }
61
62 /**
63  * Initializes the timer sub-system.
64  */
65 void Timer::Initialize(void)
66 {
67         boost::mutex::scoped_lock lock(l_Mutex);
68         l_StopThread = false;
69         l_Thread = boost::thread(&Timer::TimerThreadProc);
70 }
71
72 /**
73  * Disables the timer sub-system.
74  */
75 void Timer::Uninitialize(void)
76 {
77         {
78                 boost::mutex::scoped_lock lock(l_Mutex);
79                 l_StopThread = true;
80                 l_CV.notify_all();
81         }
82
83         l_Thread.join();
84 }
85
86 /**
87  * Calls this timer.
88  */
89 void Timer::Call(void)
90 {
91         ASSERT(!OwnsLock());
92
93         Timer::Ptr self = GetSelf();
94
95         try {
96                 OnTimerExpired(self);
97         } catch (...) {
98                 Reschedule();
99
100                 throw;
101         }
102
103         Reschedule();
104 }
105
106 /**
107  * Sets the interval for this timer.
108  *
109  * @param interval The new interval.
110  */
111 void Timer::SetInterval(double interval)
112 {
113         ASSERT(!OwnsLock());
114
115         boost::mutex::scoped_lock lock(l_Mutex);
116         m_Interval = interval;
117 }
118
119 /**
120  * Retrieves the interval for this timer.
121  *
122  * @returns The interval.
123  */
124 double Timer::GetInterval(void) const
125 {
126         ASSERT(!OwnsLock());
127
128         boost::mutex::scoped_lock lock(l_Mutex);
129         return m_Interval;
130 }
131
132 /**
133  * Registers the timer and starts processing events for it.
134  */
135 void Timer::Start(void)
136 {
137         ASSERT(!OwnsLock());
138
139         {
140                 boost::mutex::scoped_lock lock(l_Mutex);
141                 m_Started = true;
142         }
143
144         Reschedule();
145 }
146
147 /**
148  * Unregisters the timer and stops processing events for it.
149  */
150 void Timer::Stop(void)
151 {
152         ASSERT(!OwnsLock());
153
154         boost::mutex::scoped_lock lock(l_Mutex);
155
156         m_Started = false;
157         l_Timers.erase(GetSelf());
158
159         /* Notify the worker thread that we've disabled a timer. */
160         l_CV.notify_all();
161 }
162
163 /**
164  * Reschedules this timer.
165  *
166  * @param next The time when this timer should be called again. Use -1 to let
167  *             the timer figure out a suitable time based on the interval.
168  */
169 void Timer::Reschedule(double next)
170 {
171         ASSERT(!OwnsLock());
172
173         boost::mutex::scoped_lock lock(l_Mutex);
174
175         if (next < 0) {
176                 /* Don't schedule the next call if this is not a periodic timer. */
177                 if (m_Interval <= 0)
178                         return;
179
180                 next = Utility::GetTime() + m_Interval;
181         }
182
183         m_Next = next;
184
185         if (m_Started) {
186                 /* Remove and re-add the timer to update the index. */
187                 l_Timers.erase(GetSelf());
188
189                 TimerHolder th;
190                 th.Object = GetSelf();
191                 th.Next = m_Next;
192                 l_Timers.insert(th);
193
194                 /* Notify the worker that we've rescheduled a timer. */
195                 l_CV.notify_all();
196         }
197 }
198
199 /**
200  * Retrieves when the timer is next due.
201  *
202  * @returns The timestamp.
203  */
204 double Timer::GetNext(void) const
205 {
206         ASSERT(!OwnsLock());
207
208         boost::mutex::scoped_lock lock(l_Mutex);
209         return m_Next;
210 }
211
212 /**
213  * Adjusts all timers by adding the specified amount of time to their
214  * next scheduled timestamp.
215  *
216  * @param adjustment The adjustment.
217  */
218 void Timer::AdjustTimers(double adjustment)
219 {
220         boost::mutex::scoped_lock lock(l_Mutex);
221
222         double now = Utility::GetTime();
223
224         typedef boost::multi_index::nth_index<TimerSet, 1>::type TimerView;
225         TimerView& idx = boost::get<1>(l_Timers);
226
227         std::vector<Timer::Ptr> timers;
228
229         BOOST_FOREACH(const TimerHolder& th, idx) {
230                 Timer::Ptr timer = th.Object.lock();
231
232                 if (!timer)
233                         continue;
234
235                 if (abs(now - (timer->m_Next + adjustment)) <
236                     abs(now - timer->m_Next)) {
237                         timer->m_Next += adjustment;
238                         timers.push_back(timer);
239                 }
240         }
241
242         BOOST_FOREACH(const Timer::Ptr& timer, timers) {
243                 l_Timers.erase(timer);
244
245                 TimerHolder th;
246                 th.Object = timer;
247                 th.Next = timer->m_Next;
248                 l_Timers.insert(th);
249         }
250
251         /* Notify the worker that we've rescheduled some timers. */
252         l_CV.notify_all();
253 }
254
255 /**
256  * Worker thread proc for Timer objects.
257  */
258 void Timer::TimerThreadProc(void)
259 {
260         Utility::SetThreadName("Timer Thread");
261
262         for (;;) {
263                 boost::mutex::scoped_lock lock(l_Mutex);
264
265                 typedef boost::multi_index::nth_index<TimerSet, 1>::type NextTimerView;
266                 NextTimerView& idx = boost::get<1>(l_Timers);
267
268                 /* Wait until there is at least one timer. */
269                 while (idx.empty() && !l_StopThread)
270                         l_CV.wait(lock);
271
272                 if (l_StopThread)
273                         break;
274
275                 NextTimerView::iterator it = idx.begin();
276                 Timer::Ptr timer = it->Object.lock();
277
278                 if (!timer) {
279                         /* Remove the timer from the list if it's not alive anymore. */
280                         idx.erase(it);
281                         continue;
282                 }
283
284                 double wait = timer->m_Next - Utility::GetTime();
285
286                 if (wait > 0.01) {
287                         /* Make sure the timer we just examined can be destroyed while we're waiting. */
288                         timer.reset();
289
290                         /* Wait for the next timer. */
291                         l_CV.timed_wait(lock, boost::posix_time::milliseconds(wait * 1000));
292
293                         continue;
294                 }
295
296                 /* Remove the timer from the list so it doesn't get called again
297                  * until the current call is completed. */
298                 l_Timers.erase(timer);
299
300                 lock.unlock();
301
302                 /* Asynchronously call the timer. */
303                 Utility::QueueAsyncCallback(boost::bind(&Timer::Call, timer));
304         }
305 }