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;
37 Timer::WeakPtr Object;
41 typedef boost::multi_index_container<
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> >
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;
56 * Constructor for the Timer class.
59 : m_Interval(0), m_Next(0)
63 * Initializes the timer sub-system.
65 void Timer::Initialize(void)
67 boost::mutex::scoped_lock lock(l_Mutex);
69 l_Thread = boost::thread(&Timer::TimerThreadProc);
73 * Disables the timer sub-system.
75 void Timer::Uninitialize(void)
78 boost::mutex::scoped_lock lock(l_Mutex);
89 void Timer::Call(void)
93 Timer::Ptr self = GetSelf();
107 * Sets the interval for this timer.
109 * @param interval The new interval.
111 void Timer::SetInterval(double interval)
115 boost::mutex::scoped_lock lock(l_Mutex);
116 m_Interval = interval;
120 * Retrieves the interval for this timer.
122 * @returns The interval.
124 double Timer::GetInterval(void) const
128 boost::mutex::scoped_lock lock(l_Mutex);
133 * Registers the timer and starts processing events for it.
135 void Timer::Start(void)
140 boost::mutex::scoped_lock lock(l_Mutex);
148 * Unregisters the timer and stops processing events for it.
150 void Timer::Stop(void)
154 boost::mutex::scoped_lock lock(l_Mutex);
157 l_Timers.erase(GetSelf());
159 /* Notify the worker thread that we've disabled a timer. */
164 * Reschedules this timer.
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.
169 void Timer::Reschedule(double next)
173 boost::mutex::scoped_lock lock(l_Mutex);
176 /* Don't schedule the next call if this is not a periodic timer. */
180 next = Utility::GetTime() + m_Interval;
186 /* Remove and re-add the timer to update the index. */
187 l_Timers.erase(GetSelf());
190 th.Object = GetSelf();
194 /* Notify the worker that we've rescheduled a timer. */
200 * Retrieves when the timer is next due.
202 * @returns The timestamp.
204 double Timer::GetNext(void) const
208 boost::mutex::scoped_lock lock(l_Mutex);
213 * Adjusts all timers by adding the specified amount of time to their
214 * next scheduled timestamp.
216 * @param adjustment The adjustment.
218 void Timer::AdjustTimers(double adjustment)
220 boost::mutex::scoped_lock lock(l_Mutex);
222 double now = Utility::GetTime();
224 typedef boost::multi_index::nth_index<TimerSet, 1>::type TimerView;
225 TimerView& idx = boost::get<1>(l_Timers);
227 std::vector<Timer::Ptr> timers;
229 BOOST_FOREACH(const TimerHolder& th, idx) {
230 Timer::Ptr timer = th.Object.lock();
235 if (abs(now - (timer->m_Next + adjustment)) <
236 abs(now - timer->m_Next)) {
237 timer->m_Next += adjustment;
238 timers.push_back(timer);
242 BOOST_FOREACH(const Timer::Ptr& timer, timers) {
243 l_Timers.erase(timer);
247 th.Next = timer->m_Next;
251 /* Notify the worker that we've rescheduled some timers. */
256 * Worker thread proc for Timer objects.
258 void Timer::TimerThreadProc(void)
260 Utility::SetThreadName("Timer Thread");
263 boost::mutex::scoped_lock lock(l_Mutex);
265 typedef boost::multi_index::nth_index<TimerSet, 1>::type NextTimerView;
266 NextTimerView& idx = boost::get<1>(l_Timers);
268 /* Wait until there is at least one timer. */
269 while (idx.empty() && !l_StopThread)
275 NextTimerView::iterator it = idx.begin();
276 Timer::Ptr timer = it->Object.lock();
279 /* Remove the timer from the list if it's not alive anymore. */
284 double wait = timer->m_Next - Utility::GetTime();
287 /* Make sure the timer we just examined can be destroyed while we're waiting. */
290 /* Wait for the next timer. */
291 l_CV.timed_wait(lock, boost::posix_time::milliseconds(wait * 1000));
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);
302 /* Asynchronously call the timer. */
303 Utility::QueueAsyncCallback(boost::bind(&Timer::Call, timer));