icinga2/lib/base/timer.cpp

288 lines
6.4 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 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. *
******************************************************************************/
2012-03-28 13:24:49 +02:00
#include "i2-base.h"
using namespace icinga;
2013-02-17 19:14:34 +01:00
Timer::TimerSet Timer::m_Timers;
2013-02-19 23:02:08 +01:00
thread Timer::m_Thread;
2013-02-17 19:14:34 +01:00
boost::mutex Timer::m_Mutex;
boost::condition_variable Timer::m_CV;
2013-02-19 23:02:08 +01:00
bool Timer::m_StopThread;
2013-02-17 19:14:34 +01:00
/**
* Extracts the next timestamp from a Timer.
*
* @param wtimer Weak pointer to the timer.
* @returns The next timestamp
* @threadsafety Caller must hold Timer::m_Mutex.
*/
double TimerNextExtractor::operator()(const Timer::WeakPtr& wtimer)
{
Timer::Ptr timer = wtimer.lock();
if (!timer)
return 0;
return timer->m_Next;
}
2012-03-28 13:24:49 +02:00
/**
* Constructor for the Timer class.
2013-02-17 19:14:34 +01:00
*
* @threadsafety Always.
*/
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
* @threadsafety Always.
*/
2013-02-17 19:14:34 +01:00
void Timer::Initialize(void)
2012-03-28 13:24:49 +02:00
{
2013-02-19 23:02:08 +01:00
boost::mutex::scoped_lock lock(m_Mutex);
m_StopThread = false;
m_Thread = thread(boost::bind(&Timer::TimerThreadProc));
}
/**
* Disables the timer sub-system.
*
* @threadsafety Always.
*/
void Timer::Uninitialize(void)
{
{
boost::mutex::scoped_lock lock(m_Mutex);
m_StopThread = true;
m_CV.notify_all();
}
m_Thread.join();
2012-03-28 13:24:49 +02:00
}
/**
2013-02-17 19:14:34 +01:00
* Calls this timer.
*
* @threadsafety Always.
*/
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();
OnTimerExpired(self);
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.
2013-02-17 19:14:34 +01:00
* @threadsafety Always.
*/
void Timer::SetInterval(double interval)
2012-03-28 13:24:49 +02:00
{
ASSERT(!OwnsLock());
2013-03-01 12:07:52 +01:00
2013-02-17 19:14:34 +01:00
boost::mutex::scoped_lock lock(m_Mutex);
2012-03-28 13:24:49 +02:00
m_Interval = interval;
}
/**
* Retrieves the interval for this timer.
*
* @returns The interval.
2013-02-17 19:14:34 +01:00
* @threadsafety Always.
*/
double Timer::GetInterval(void) const
2012-03-28 13:24:49 +02:00
{
ASSERT(!OwnsLock());
2013-03-01 12:07:52 +01:00
2013-02-17 19:14:34 +01:00
boost::mutex::scoped_lock lock(m_Mutex);
2012-03-28 13:24:49 +02:00
return m_Interval;
}
/**
* Registers the timer and starts processing events for it.
2013-02-17 19:14:34 +01:00
*
* @threadsafety Always.
*/
2012-03-28 13:24:49 +02:00
void Timer::Start(void)
{
ASSERT(!OwnsLock());
2013-03-01 12:07:52 +01:00
{
boost::mutex::scoped_lock lock(m_Mutex);
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.
2013-02-17 19:14:34 +01:00
*
* @threadsafety Always.
*/
2012-03-28 13:24:49 +02:00
void Timer::Stop(void)
{
ASSERT(!OwnsLock());
2013-03-01 12:07:52 +01:00
2013-02-17 19:14:34 +01:00
boost::mutex::scoped_lock lock(m_Mutex);
2013-02-18 14:40:24 +01:00
m_Started = false;
2013-02-17 19:14:34 +01:00
m_Timers.erase(GetSelf());
2013-02-17 19:14:34 +01:00
/* Notify the worker thread that we've disabled a timer. */
m_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.
* @threadsafety Always.
*/
void Timer::Reschedule(double next)
2012-03-28 13:24:49 +02:00
{
ASSERT(!OwnsLock());
2013-03-01 12:07:52 +01:00
2013-02-17 19:14:34 +01:00
boost::mutex::scoped_lock lock(m_Mutex);
if (next < 0)
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. */
m_Timers.erase(GetSelf());
m_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. */
m_CV.notify_all();
}
2013-02-17 19:14:34 +01:00
}
/**
* Retrieves when the timer is next due.
*
* @returns The timestamp.
* @threadsafety Always.
*/
double Timer::GetNext(void) const
{
ASSERT(!OwnsLock());
2013-03-01 12:07:52 +01:00
2013-02-17 19:14:34 +01:00
boost::mutex::scoped_lock lock(m_Mutex);
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.
2013-02-17 19:14:34 +01:00
* @threadsafety Always.
2012-09-25 14:03:41 +02:00
*/
void Timer::AdjustTimers(double adjustment)
{
2013-02-17 19:14:34 +01:00
boost::mutex::scoped_lock lock(m_Mutex);
double now = Utility::GetTime();
2013-02-17 19:14:34 +01:00
typedef nth_index<TimerSet, 1>::type TimerView;
TimerView& idx = boost::get<1>(m_Timers);
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-02-17 19:14:34 +01:00
m_Timers.erase(timer);
m_Timers.insert(timer);
}
}
/* Notify the worker that we've rescheduled some timers. */
m_CV.notify_all();
}
/**
* Worker thread proc for Timer objects.
*
* @threadsafety Always.
*/
void Timer::TimerThreadProc(void)
{
for (;;) {
boost::mutex::scoped_lock lock(m_Mutex);
typedef nth_index<TimerSet, 1>::type NextTimerView;
NextTimerView& idx = boost::get<1>(m_Timers);
/* Wait until there is at least one timer. */
2013-02-19 23:02:08 +01:00
while (idx.empty() && !m_StopThread)
2013-02-17 19:14:34 +01:00
m_CV.wait(lock);
2013-02-19 23:02:08 +01:00
if (m_StopThread)
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. */
m_CV.timed_wait(lock, boost::posix_time::milliseconds(wait * 1000));
continue;
}
/* Remove the timer from the list so it doesn't get called again
* until the current call is completed. */
m_Timers.erase(timer);
2013-02-26 10:13:54 +01:00
lock.unlock();
2013-02-17 19:14:34 +01:00
/* Asynchronously call the timer. */
Application::GetEQ().Post(boost::bind(&Timer::Call, timer));
2012-09-25 14:03:41 +02:00
}
}