icinga2/lib/base/timer.cpp

319 lines
6.1 KiB
C++
Raw Normal View History

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "base/defer.hpp"
2014-05-25 16:23:35 +02:00
#include "base/timer.hpp"
#include "base/debug.hpp"
#include "base/logger.hpp"
2014-05-25 16:23:35 +02:00
#include "base/utility.hpp"
2013-03-18 11:02:18 +01:00
#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>
2017-11-21 12:12:58 +01:00
#include <thread>
2012-03-28 13:24:49 +02:00
using namespace icinga;
namespace icinga {
class TimerHolder {
public:
TimerHolder(Timer *timer)
: m_Timer(timer)
{ }
inline Timer *GetObject() const
{
return m_Timer;
}
inline double GetNextUnlocked() const
{
return m_Timer->m_Next;
}
operator Timer *() const
{
return m_Timer;
}
private:
Timer *m_Timer;
};
}
2013-03-18 11:02:18 +01:00
typedef boost::multi_index_container<
TimerHolder,
2013-03-18 11:02:18 +01:00
boost::multi_index::indexed_by<
boost::multi_index::ordered_unique<boost::multi_index::const_mem_fun<TimerHolder, Timer *, &TimerHolder::GetObject> >,
boost::multi_index::ordered_non_unique<boost::multi_index::const_mem_fun<TimerHolder, double, &TimerHolder::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;
2017-11-21 12:12:58 +01:00
static std::thread l_TimerThread;
2014-12-05 15:55:20 +01:00
static bool l_StopTimerThread;
2013-03-18 11:02:18 +01:00
static TimerSet l_Timers;
static int l_AliveTimers = 0;
2012-03-28 13:24:49 +02:00
static Defer l_ShutdownTimersCleanlyOnExit (&Timer::Uninitialize);
2014-05-28 13:46:39 +02:00
/**
* Destructor for the Timer class.
*/
Timer::~Timer()
2014-05-28 13:46:39 +02:00
{
Stop(true);
2014-05-28 13:46:39 +02:00
}
void Timer::Initialize()
{
boost::mutex::scoped_lock lock(l_TimerMutex);
if (l_AliveTimers > 0) {
InitializeThread();
}
}
void Timer::Uninitialize()
{
boost::mutex::scoped_lock lock(l_TimerMutex);
if (l_AliveTimers > 0) {
UninitializeThread();
}
}
void Timer::InitializeThread()
{
l_StopTimerThread = false;
l_TimerThread = std::thread(&Timer::TimerThreadProc);
}
void Timer::UninitializeThread()
2013-02-19 23:02:08 +01:00
{
{
l_StopTimerThread = true;
l_TimerCV.notify_all();
}
l_TimerMutex.unlock();
if (l_TimerThread.joinable())
l_TimerThread.join();
l_TimerMutex.lock();
2012-03-28 13:24:49 +02:00
}
/**
2013-02-17 19:14:34 +01:00
* Calls this timer.
*/
void Timer::Call()
2012-03-28 13:24:49 +02:00
{
try {
OnTimerExpired(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() 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.
*/
void Timer::Start()
2012-03-28 13:24:49 +02:00
{
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;
if (++l_AliveTimers == 1) {
InitializeThread();
}
2013-03-01 12:07:52 +01:00
}
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
if (m_Started && --l_AliveTimers == 0) {
UninitializeThread();
}
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() const
2013-02-17 19:14:34 +01:00
{
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;
for (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
}
for (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()
2013-02-17 19:14:34 +01:00
{
Log(LogDebug, "Timer", "TimerThreadProc started.");
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;
auto 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. */
Explicitly use long with boost::posix_time In file included from lib/base/base_unity.cpp:61: lib/base/timer.cpp:295:31: error: no matching conversion for functional-style cast from 'double' to 'boost::posix_time::milliseconds' (aka 'subsecond_duration<boost::posix_time::time_duration, 1000>') l_TimerCV.timed_wait(lock, boost::posix_time::milliseconds(wait * 1000)); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from lib/remote/remote_unity.cpp:19: lib/remote/eventqueue.cpp:111:30: error: no matching conversion for functional-style cast from 'double' to 'boost::posix_time::milliseconds' (aka 'subsecond_duration<boost::posix_time::time_duration, 1000>') if (!m_CV.timed_wait(lock, boost::posix_time::milliseconds(timeout * 1000))) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from lib/checker/checker_unity.cpp:1: lib/checker/checkercomponent.cpp:128:26: error: no matching conversion for functional-style cast from 'double' to 'boost::posix_time::milliseconds' (aka 'subsecond_duration<boost::posix_time::time_duration, 1000>') m_CV.timed_wait(lock, boost::posix_time::milliseconds(wait * 1000)); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/local/include/boost/date_time/time_duration.hpp:270:30: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'double' to 'const boost::date_time::subsecond_duration<boost::posix_time::time_duration, 1000>' for 1st argument class BOOST_SYMBOL_VISIBLE subsecond_duration : public base_duration ^ /usr/local/include/boost/date_time/time_duration.hpp:270:30: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'double' to 'boost::date_time::subsecond_duration<boost::posix_time::time_duration, 1000>' for 1st argument /usr/local/include/boost/date_time/time_duration.hpp:286:59: note: candidate template ignored: disabled by 'enable_if' [with T = double] typename boost::enable_if<boost::is_integral<T>, void>::type* = 0) : ^
2018-04-15 06:02:42 +02:00
l_TimerCV.timed_wait(lock, boost::posix_time::milliseconds(long(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
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. */
Utility::QueueAsyncCallback([timer]() { timer->Call(); });
2012-09-25 14:03:41 +02:00
}
}