icinga2/lib/base/timer.cpp

310 lines
7.0 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
2014-01-09 00:32:11 +01:00
* Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
2012-05-11 13:33:57 +02:00
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
2013-03-16 21:18:53 +01:00
#include "base/timer.h"
#include "base/application.h"
2013-08-28 08:18:58 +02:00
#include "base/debug.h"
2013-03-18 11:02:18 +01:00
#include "base/utility.h"
2013-04-19 12:58:16 +02:00
#include "base/logger_fwd.h"
2013-03-15 18:21:29 +01:00
#include <boost/bind.hpp>
2013-03-18 11:02:18 +01:00
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/key_extractors.hpp>
2012-03-28 13:24:49 +02:00
using namespace icinga;
2013-02-17 19:14:34 +01:00
/**
2013-03-18 11:02:18 +01:00
* @ingroup base
2013-02-17 19:14:34 +01:00
*/
2013-03-18 11:02:18 +01:00
struct icinga::TimerNextExtractor
2013-02-17 19:14:34 +01:00
{
2013-03-18 11:02:18 +01:00
typedef double result_type;
/**
* Extracts the next timestamp from a Timer.
*
* Note: Caller must hold l_Mutex.
*
2013-03-18 11:02:18 +01:00
* @param wtimer Weak pointer to the timer.
* @returns The next timestamp
*/
double operator()(const weak_ptr<Timer>& wtimer)
{
Timer::Ptr timer = wtimer.lock();
2013-02-17 19:14:34 +01:00
2013-03-18 11:02:18 +01:00
if (!timer)
return 0;
2013-02-17 19:14:34 +01:00
2013-03-18 11:02:18 +01:00
return timer->m_Next;
}
};
typedef boost::multi_index_container<
Timer::WeakPtr,
boost::multi_index::indexed_by<
boost::multi_index::ordered_unique<boost::multi_index::identity<Timer::WeakPtr> >,
boost::multi_index::ordered_non_unique<TimerNextExtractor>
>
> TimerSet;
static boost::mutex l_Mutex;
static boost::condition_variable l_CV;
static boost::thread l_Thread;
static bool l_StopThread;
static TimerSet l_Timers;
2012-03-28 13:24:49 +02:00
/**
* Constructor for the Timer class.
*/
2012-03-28 13:24:49 +02:00
Timer::Timer(void)
2013-02-17 19:14:34 +01:00
: m_Interval(0), m_Next(0)
2012-08-03 15:35:27 +02:00
{ }
2012-03-28 13:24:49 +02:00
/**
2013-02-17 19:14:34 +01:00
* Initializes the timer sub-system.
*/
2013-02-17 19:14:34 +01:00
void Timer::Initialize(void)
2012-03-28 13:24:49 +02:00
{
2013-03-18 11:02:18 +01:00
boost::mutex::scoped_lock lock(l_Mutex);
l_StopThread = false;
l_Thread = boost::thread(boost::bind(&Timer::TimerThreadProc));
2013-02-19 23:02:08 +01:00
}
/**
* Disables the timer sub-system.
*/
void Timer::Uninitialize(void)
{
{
2013-03-18 11:02:18 +01:00
boost::mutex::scoped_lock lock(l_Mutex);
l_StopThread = true;
l_CV.notify_all();
2013-02-19 23:02:08 +01:00
}
2013-03-18 11:02:18 +01:00
l_Thread.join();
2012-03-28 13:24:49 +02:00
}
/**
2013-02-17 19:14:34 +01:00
* Calls this timer.
*/
2013-03-04 15:52:42 +01:00
void Timer::Call(void)
2012-03-28 13:24:49 +02:00
{
ASSERT(!OwnsLock());
2013-03-04 15:52:42 +01:00
Timer::Ptr self = GetSelf();
try {
OnTimerExpired(self);
} catch (...) {
Reschedule();
throw;
}
2013-03-06 11:03:50 +01:00
Reschedule();
2012-03-28 13:24:49 +02:00
}
/**
* Sets the interval for this timer.
*
* @param interval The new interval.
*/
void Timer::SetInterval(double interval)
2012-03-28 13:24:49 +02:00
{
ASSERT(!OwnsLock());
2013-03-01 12:07:52 +01:00
2013-03-18 11:02:18 +01:00
boost::mutex::scoped_lock lock(l_Mutex);
2012-03-28 13:24:49 +02:00
m_Interval = interval;
}
/**
* Retrieves the interval for this timer.
*
* @returns The interval.
*/
double Timer::GetInterval(void) const
2012-03-28 13:24:49 +02:00
{
ASSERT(!OwnsLock());
2013-03-01 12:07:52 +01:00
2013-03-18 11:02:18 +01:00
boost::mutex::scoped_lock lock(l_Mutex);
2012-03-28 13:24:49 +02:00
return m_Interval;
}
/**
* Registers the timer and starts processing events for it.
*/
2012-03-28 13:24:49 +02:00
void Timer::Start(void)
{
ASSERT(!OwnsLock());
2013-03-01 12:07:52 +01:00
{
2013-03-18 11:02:18 +01:00
boost::mutex::scoped_lock lock(l_Mutex);
2013-03-01 12:07:52 +01:00
m_Started = true;
}
2013-02-18 14:40:24 +01:00
2013-02-17 19:14:34 +01:00
Reschedule();
2012-03-28 13:24:49 +02:00
}
/**
* Unregisters the timer and stops processing events for it.
*/
2012-03-28 13:24:49 +02:00
void Timer::Stop(void)
{
ASSERT(!OwnsLock());
2013-03-01 12:07:52 +01:00
2013-03-18 11:02:18 +01:00
boost::mutex::scoped_lock lock(l_Mutex);
2013-02-18 14:40:24 +01:00
m_Started = false;
2013-03-18 11:02:18 +01:00
l_Timers.erase(GetSelf());
2013-02-17 19:14:34 +01:00
/* Notify the worker thread that we've disabled a timer. */
2013-03-18 11:02:18 +01:00
l_CV.notify_all();
2012-03-28 13:24:49 +02:00
}
/**
* Reschedules this timer.
*
2013-02-17 19:14:34 +01:00
* @param next The time when this timer should be called again. Use -1 to let
* the timer figure out a suitable time based on the interval.
*/
void Timer::Reschedule(double next)
2012-03-28 13:24:49 +02:00
{
ASSERT(!OwnsLock());
2013-03-01 12:07:52 +01:00
2013-03-18 11:02:18 +01:00
boost::mutex::scoped_lock lock(l_Mutex);
2013-02-17 19:14:34 +01:00
if (next < 0) {
/* Don't schedule the next call if this is not a periodic timer. */
if (m_Interval <= 0)
return;
next = Utility::GetTime() + m_Interval;
}
2013-02-17 19:14:34 +01:00
2012-03-28 13:24:49 +02:00
m_Next = next;
2013-02-17 19:14:34 +01:00
2013-02-18 14:40:24 +01:00
if (m_Started) {
/* Remove and re-add the timer to update the index. */
2013-03-18 11:02:18 +01:00
l_Timers.erase(GetSelf());
l_Timers.insert(GetSelf());
2013-02-17 19:14:34 +01:00
2013-02-18 14:40:24 +01:00
/* Notify the worker that we've rescheduled a timer. */
2013-03-18 11:02:18 +01:00
l_CV.notify_all();
2013-02-18 14:40:24 +01:00
}
2013-02-17 19:14:34 +01:00
}
/**
* Retrieves when the timer is next due.
*
* @returns The timestamp.
*/
double Timer::GetNext(void) const
{
ASSERT(!OwnsLock());
2013-03-01 12:07:52 +01:00
2013-03-18 11:02:18 +01:00
boost::mutex::scoped_lock lock(l_Mutex);
2013-02-17 19:14:34 +01:00
return m_Next;
2012-03-28 13:24:49 +02:00
}
2012-09-25 14:03:41 +02:00
/**
* Adjusts all timers by adding the specified amount of time to their
* next scheduled timestamp.
*
* @param adjustment The adjustment.
*/
void Timer::AdjustTimers(double adjustment)
{
2013-03-18 11:02:18 +01:00
boost::mutex::scoped_lock lock(l_Mutex);
2013-02-17 19:14:34 +01:00
double now = Utility::GetTime();
2013-03-16 21:18:53 +01:00
typedef boost::multi_index::nth_index<TimerSet, 1>::type TimerView;
2013-03-18 11:02:18 +01:00
TimerView& idx = boost::get<1>(l_Timers);
2013-02-17 19:14:34 +01:00
TimerView::iterator it;
for (it = idx.begin(); it != idx.end(); it++) {
Timer::Ptr timer = it->lock();
if (abs(now - (timer->m_Next + adjustment)) <
2013-02-17 19:14:34 +01:00
abs(now - timer->m_Next)) {
timer->m_Next += adjustment;
2013-03-18 11:02:18 +01:00
l_Timers.erase(timer);
l_Timers.insert(timer);
2013-03-18 11:15:46 +01:00
}
2013-02-17 19:14:34 +01:00
}
/* Notify the worker that we've rescheduled some timers. */
2013-03-18 11:02:18 +01:00
l_CV.notify_all();
2013-02-17 19:14:34 +01:00
}
/**
* Worker thread proc for Timer objects.
*/
void Timer::TimerThreadProc(void)
{
Utility::SetThreadName("Timer Thread");
2013-02-17 19:14:34 +01:00
for (;;) {
2013-03-18 11:02:18 +01:00
boost::mutex::scoped_lock lock(l_Mutex);
2013-02-17 19:14:34 +01:00
2013-03-16 21:18:53 +01:00
typedef boost::multi_index::nth_index<TimerSet, 1>::type NextTimerView;
2013-03-18 11:02:18 +01:00
NextTimerView& idx = boost::get<1>(l_Timers);
2013-02-17 19:14:34 +01:00
/* Wait until there is at least one timer. */
2013-03-18 11:02:18 +01:00
while (idx.empty() && !l_StopThread)
l_CV.wait(lock);
2013-02-17 19:14:34 +01:00
2013-03-18 11:02:18 +01:00
if (l_StopThread)
2013-02-19 23:02:08 +01:00
break;
2013-02-17 19:14:34 +01:00
NextTimerView::iterator it = idx.begin();
Timer::Ptr timer = it->lock();
if (!timer) {
/* Remove the timer from the list if it's not alive anymore. */
idx.erase(it);
continue;
}
double wait = timer->m_Next - Utility::GetTime();
if (wait > 0) {
/* Make sure the timer we just examined can be destroyed while we're waiting. */
timer.reset();
/* Wait for the next timer. */
2013-03-18 11:02:18 +01:00
l_CV.timed_wait(lock, boost::posix_time::milliseconds(wait * 1000));
2013-02-17 19:14:34 +01:00
continue;
}
/* Remove the timer from the list so it doesn't get called again
* until the current call is completed. */
2013-03-18 11:02:18 +01:00
l_Timers.erase(timer);
2013-02-17 19:14:34 +01:00
2013-02-26 10:13:54 +01:00
lock.unlock();
2013-02-17 19:14:34 +01:00
/* Asynchronously call the timer. */
2013-04-19 12:58:16 +02:00
Utility::QueueAsyncCallback(boost::bind(&Timer::Call, timer));
2012-09-25 14:03:41 +02:00
}
}