1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
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. *
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. *
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 ******************************************************************************/
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>
33 using namespace icinga;
38 struct icinga::TimerNextExtractor
40 typedef double result_type;
43 * Extracts the next timestamp from a Timer.
45 * Note: Caller must hold l_Mutex.
47 * @param wtimer Weak pointer to the timer.
48 * @returns The next timestamp
50 double operator()(const weak_ptr<Timer>& wtimer) const
52 Timer::Ptr timer = wtimer.lock();
61 typedef boost::multi_index_container<
63 boost::multi_index::indexed_by<
64 boost::multi_index::ordered_unique<boost::multi_index::identity<Timer::WeakPtr> >,
65 boost::multi_index::ordered_non_unique<TimerNextExtractor>
69 static boost::mutex l_Mutex;
70 static boost::condition_variable l_CV;
71 static boost::thread l_Thread;
72 static bool l_StopThread;
73 static TimerSet l_Timers;
76 * Constructor for the Timer class.
79 : m_Interval(0), m_Next(0)
83 * Initializes the timer sub-system.
85 void Timer::Initialize(void)
87 boost::mutex::scoped_lock lock(l_Mutex);
89 l_Thread = boost::thread(&Timer::TimerThreadProc);
93 * Disables the timer sub-system.
95 void Timer::Uninitialize(void)
98 boost::mutex::scoped_lock lock(l_Mutex);
109 void Timer::Call(void)
113 Timer::Ptr self = GetSelf();
116 OnTimerExpired(self);
127 * Sets the interval for this timer.
129 * @param interval The new interval.
131 void Timer::SetInterval(double interval)
135 boost::mutex::scoped_lock lock(l_Mutex);
136 m_Interval = interval;
140 * Retrieves the interval for this timer.
142 * @returns The interval.
144 double Timer::GetInterval(void) const
148 boost::mutex::scoped_lock lock(l_Mutex);
153 * Registers the timer and starts processing events for it.
155 void Timer::Start(void)
160 boost::mutex::scoped_lock lock(l_Mutex);
168 * Unregisters the timer and stops processing events for it.
170 void Timer::Stop(void)
174 boost::mutex::scoped_lock lock(l_Mutex);
177 l_Timers.erase(GetSelf());
179 /* Notify the worker thread that we've disabled a timer. */
184 * Reschedules this timer.
186 * @param next The time when this timer should be called again. Use -1 to let
187 * the timer figure out a suitable time based on the interval.
189 void Timer::Reschedule(double next)
193 boost::mutex::scoped_lock lock(l_Mutex);
196 /* Don't schedule the next call if this is not a periodic timer. */
200 next = Utility::GetTime() + m_Interval;
206 /* Remove and re-add the timer to update the index. */
207 l_Timers.erase(GetSelf());
208 l_Timers.insert(GetSelf());
210 /* Notify the worker that we've rescheduled a timer. */
216 * Retrieves when the timer is next due.
218 * @returns The timestamp.
220 double Timer::GetNext(void) const
224 boost::mutex::scoped_lock lock(l_Mutex);
229 * Adjusts all timers by adding the specified amount of time to their
230 * next scheduled timestamp.
232 * @param adjustment The adjustment.
234 void Timer::AdjustTimers(double adjustment)
236 boost::mutex::scoped_lock lock(l_Mutex);
238 double now = Utility::GetTime();
240 typedef boost::multi_index::nth_index<TimerSet, 1>::type TimerView;
241 TimerView& idx = boost::get<1>(l_Timers);
243 std::vector<Timer::Ptr> timers;
245 BOOST_FOREACH(const Timer::WeakPtr& wtimer, idx) {
246 Timer::Ptr timer = wtimer.lock();
251 if (abs(now - (timer->m_Next + adjustment)) <
252 abs(now - timer->m_Next)) {
253 timer->m_Next += adjustment;
254 timers.push_back(timer);
258 BOOST_FOREACH(const Timer::Ptr& timer, timers) {
259 l_Timers.erase(timer);
260 l_Timers.insert(timer);
263 /* Notify the worker that we've rescheduled some timers. */
268 * Worker thread proc for Timer objects.
270 void Timer::TimerThreadProc(void)
272 Utility::SetThreadName("Timer Thread");
275 boost::mutex::scoped_lock lock(l_Mutex);
277 typedef boost::multi_index::nth_index<TimerSet, 1>::type NextTimerView;
278 NextTimerView& idx = boost::get<1>(l_Timers);
280 /* Wait until there is at least one timer. */
281 while (idx.empty() && !l_StopThread)
287 NextTimerView::iterator it = idx.begin();
288 Timer::Ptr timer = it->lock();
291 /* Remove the timer from the list if it's not alive anymore. */
296 double wait = timer->m_Next - Utility::GetTime();
299 /* Make sure the timer we just examined can be destroyed while we're waiting. */
302 /* Wait for the next timer. */
303 l_CV.timed_wait(lock, boost::posix_time::milliseconds(wait * 1000));
308 /* Remove the timer from the list so it doesn't get called again
309 * until the current call is completed. */
310 l_Timers.erase(timer);
314 /* Asynchronously call the timer. */
315 Utility::QueueAsyncCallback(boost::bind(&Timer::Call, timer));