icinga2/lib/base/timer.cpp

289 lines
7.0 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
2016-01-12 08:29:59 +01:00
* Copyright (C) 2012-2016 Icinga Development Team (https://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. *
******************************************************************************/
2014-05-25 16:23:35 +02:00
#include "base/timer.hpp"
#include "base/debug.hpp"
#include "base/utility.hpp"
2013-03-15 18:21:29 +01:00
#include <boost/bind.hpp>
#include <boost/foreach.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-03-18 11:02:18 +01:00
typedef boost::multi_index_container<
2014-05-28 13:46:39 +02:00
Timer::Holder,
2013-03-18 11:02:18 +01:00
boost::multi_index::indexed_by<
2014-05-28 13:46:39 +02:00
boost::multi_index::ordered_unique<boost::multi_index::const_mem_fun<Timer::Holder, Timer *, &Timer::Holder::GetObject> >,
boost::multi_index::ordered_non_unique<boost::multi_index::const_mem_fun<Timer::Holder, double, &Timer::Holder::GetNextUnlocked> >
2013-03-18 11:02:18 +01:00
>
> TimerSet;
2014-12-05 15:55:20 +01:00
static boost::mutex l_TimerMutex;
static boost::condition_variable l_TimerCV;
static boost::thread l_TimerThread;
static bool l_StopTimerThread;
2013-03-18 11:02:18 +01:00
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)
2015-02-27 14:07:12 +01:00
: m_Interval(0), m_Next(0), m_Started(false), m_Running(false)
2012-08-03 15:35:27 +02:00
{ }
2012-03-28 13:24:49 +02:00
2014-05-28 13:46:39 +02:00
/**
* Destructor for the Timer class.
*/
Timer::~Timer(void)
{
Stop(true);
2014-05-28 13:46:39 +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
{
2014-12-05 15:55:20 +01:00
boost::mutex::scoped_lock lock(l_TimerMutex);
l_StopTimerThread = false;
l_TimerThread = boost::thread(&Timer::TimerThreadProc);
2013-02-19 23:02:08 +01:00
}
/**
* Disables the timer sub-system.
*/
void Timer::Uninitialize(void)
{
{
2014-12-05 15:55:20 +01:00
boost::mutex::scoped_lock lock(l_TimerMutex);
l_StopTimerThread = true;
l_TimerCV.notify_all();
2013-02-19 23:02:08 +01:00
}
2014-12-05 15:55:20 +01:00
if (l_TimerThread.joinable())
l_TimerThread.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
{
try {
OnTimerExpired(Timer::Ptr(this));
} catch (...) {
InternalReschedule(true);
throw;
}
InternalReschedule(true);
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
{
2014-12-05 15:55:20 +01:00
boost::mutex::scoped_lock lock(l_TimerMutex);
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
{
2014-12-05 15:55:20 +01:00
boost::mutex::scoped_lock lock(l_TimerMutex);
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)
{
2013-03-01 12:07:52 +01:00
{
2014-12-05 15:55:20 +01:00
boost::mutex::scoped_lock lock(l_TimerMutex);
2013-03-01 12:07:52 +01:00
m_Started = true;
}
2013-02-18 14:40:24 +01:00
InternalReschedule(false);
2012-03-28 13:24:49 +02:00
}
/**
* Unregisters the timer and stops processing events for it.
*/
void Timer::Stop(bool wait)
2012-03-28 13:24:49 +02:00
{
2014-12-05 15:55:20 +01:00
if (l_StopTimerThread)
return;
2014-12-05 15:55:20 +01:00
boost::mutex::scoped_lock lock(l_TimerMutex);
2013-02-18 14:40:24 +01:00
m_Started = false;
2014-05-28 13:46:39 +02:00
l_Timers.erase(this);
2013-02-17 19:14:34 +01:00
/* Notify the worker thread that we've disabled a timer. */
2014-12-05 15:55:20 +01:00
l_TimerCV.notify_all();
while (wait && m_Running)
l_TimerCV.wait(lock);
}
void Timer::Reschedule(double next)
{
InternalReschedule(false, next);
2012-03-28 13:24:49 +02:00
}
/**
* Reschedules this timer.
*
* @param completed Whether the timer has just completed its callback.
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::InternalReschedule(bool completed, double next)
2012-03-28 13:24:49 +02:00
{
2014-12-05 15:55:20 +01:00
boost::mutex::scoped_lock lock(l_TimerMutex);
2013-02-17 19:14:34 +01:00
if (completed)
m_Running = false;
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
if (m_Started && !m_Running) {
2013-02-18 14:40:24 +01:00
/* Remove and re-add the timer to update the index. */
2014-05-28 13:46:39 +02:00
l_Timers.erase(this);
l_Timers.insert(this);
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. */
2014-12-05 15:55:20 +01:00
l_TimerCV.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
{
2014-12-05 15:55:20 +01:00
boost::mutex::scoped_lock lock(l_TimerMutex);
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)
{
2014-12-05 15:55:20 +01:00
boost::mutex::scoped_lock lock(l_TimerMutex);
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
2014-05-28 13:46:39 +02:00
std::vector<Timer *> timers;
2014-05-28 13:46:39 +02:00
BOOST_FOREACH(Timer *timer, idx) {
2015-01-12 12:36:49 +01:00
if (std::fabs(now - (timer->m_Next + adjustment)) <
std::fabs(now - timer->m_Next)) {
timer->m_Next += adjustment;
timers.push_back(timer);
2013-03-18 11:15:46 +01:00
}
2013-02-17 19:14:34 +01:00
}
2014-05-28 13:46:39 +02:00
BOOST_FOREACH(Timer *timer, timers) {
l_Timers.erase(timer);
2014-05-28 13:46:39 +02:00
l_Timers.insert(timer);
}
2013-02-17 19:14:34 +01:00
/* Notify the worker that we've rescheduled some timers. */
2014-12-05 15:55:20 +01:00
l_TimerCV.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 (;;) {
2014-12-05 15:55:20 +01:00
boost::mutex::scoped_lock lock(l_TimerMutex);
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. */
2014-12-05 15:55:20 +01:00
while (idx.empty() && !l_StopTimerThread)
l_TimerCV.wait(lock);
2013-02-17 19:14:34 +01:00
2014-12-05 15:55:20 +01:00
if (l_StopTimerThread)
2013-02-19 23:02:08 +01:00
break;
2013-02-17 19:14:34 +01:00
NextTimerView::iterator it = idx.begin();
2014-05-28 13:46:39 +02:00
Timer *timer = *it;
2013-02-17 19:14:34 +01:00
double wait = timer->m_Next - Utility::GetTime();
if (wait > 0.01) {
2013-02-17 19:14:34 +01:00
/* Wait for the next timer. */
2014-12-05 15:55:20 +01:00
l_TimerCV.timed_wait(lock, boost::posix_time::milliseconds(wait * 1000));
2013-02-17 19:14:34 +01:00
continue;
}
Timer::Ptr ptimer = timer;
2013-02-17 19:14:34 +01:00
/* 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
2015-02-27 14:07:12 +01:00
timer->m_Running = true;
2013-02-26 10:13:54 +01:00
lock.unlock();
2013-02-17 19:14:34 +01:00
/* Asynchronously call the timer. */
2014-05-28 13:46:39 +02:00
Utility::QueueAsyncCallback(boost::bind(&Timer::Call, ptimer));
2012-09-25 14:03:41 +02:00
}
}