icinga2/lib/base/timer.cpp

355 lines
6.9 KiB
C++
Raw Permalink Normal View History

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "base/defer.hpp"
2014-05-25 10:23:35 -04:00
#include "base/timer.hpp"
#include "base/debug.hpp"
#include "base/logger.hpp"
2014-05-25 10:23:35 -04:00
#include "base/utility.hpp"
2013-03-18 06:02:18 -04:00
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/key_extractors.hpp>
2021-02-02 04:16:04 -05:00
#include <chrono>
#include <condition_variable>
#include <mutex>
2017-11-21 06:12:58 -05:00
#include <thread>
#include <utility>
2012-03-28 07:24:49 -04: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 06:02:18 -04:00
typedef boost::multi_index_container<
TimerHolder,
2013-03-18 06:02:18 -04: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 06:02:18 -04:00
>
> TimerSet;
2021-02-02 04:16:04 -05:00
static std::mutex l_TimerMutex;
static std::condition_variable l_TimerCV;
2017-11-21 06:12:58 -05:00
static std::thread l_TimerThread;
2014-12-05 09:55:20 -05:00
static bool l_StopTimerThread;
2013-03-18 06:02:18 -04:00
static TimerSet l_Timers;
static int l_AliveTimers = 0;
2012-03-28 07:24:49 -04:00
static Defer l_ShutdownTimersCleanlyOnExit (&Timer::Uninitialize);
Timer::Ptr Timer::Create()
{
Ptr t (new Timer());
t->m_Self = t;
return t;
}
2014-05-28 07:46:39 -04:00
/**
* Destructor for the Timer class.
*/
Timer::~Timer()
2014-05-28 07:46:39 -04:00
{
Stop(true);
2014-05-28 07:46:39 -04:00
}
void Timer::Initialize()
{
2021-02-02 04:16:04 -05:00
std::unique_lock<std::mutex> lock(l_TimerMutex);
if (l_AliveTimers > 0) {
InitializeThread();
}
}
void Timer::Uninitialize()
{
2021-02-02 04:16:04 -05:00
std::unique_lock<std::mutex> 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 17:02:08 -05:00
{
{
l_StopTimerThread = true;
l_TimerCV.notify_all();
}
l_TimerMutex.unlock();
if (l_TimerThread.joinable())
l_TimerThread.join();
l_TimerMutex.lock();
2012-03-28 07:24:49 -04:00
}
/**
2013-02-17 13:14:34 -05:00
* Calls this timer.
*/
void Timer::Call()
2012-03-28 07:24:49 -04:00
{
try {
OnTimerExpired(this);
} catch (...) {
InternalReschedule(true);
throw;
}
InternalReschedule(true);
2012-03-28 07:24:49 -04:00
}
/**
* Sets the interval for this timer.
*
* @param interval The new interval.
*/
void Timer::SetInterval(double interval)
2012-03-28 07:24:49 -04:00
{
2021-02-02 04:16:04 -05:00
std::unique_lock<std::mutex> lock(l_TimerMutex);
2012-03-28 07:24:49 -04:00
m_Interval = interval;
}
/**
* Retrieves the interval for this timer.
*
* @returns The interval.
*/
double Timer::GetInterval() const
2012-03-28 07:24:49 -04:00
{
2021-02-02 04:16:04 -05:00
std::unique_lock<std::mutex> lock(l_TimerMutex);
2012-03-28 07:24:49 -04:00
return m_Interval;
}
/**
* Registers the timer and starts processing events for it.
*/
void Timer::Start()
2012-03-28 07:24:49 -04:00
{
std::unique_lock<std::mutex> lock(l_TimerMutex);
if (!m_Started && ++l_AliveTimers == 1) {
InitializeThread();
2013-03-01 06:07:52 -05:00
}
2013-02-18 08:40:24 -05:00
m_Started = true;
InternalRescheduleUnlocked(false, m_Interval > 0 ? -1 : m_Next);
2012-03-28 07:24:49 -04:00
}
/**
* Unregisters the timer and stops processing events for it.
*/
void Timer::Stop(bool wait)
2012-03-28 07:24:49 -04:00
{
2014-12-05 09:55:20 -05:00
if (l_StopTimerThread)
return;
2021-02-02 04:16:04 -05:00
std::unique_lock<std::mutex> lock(l_TimerMutex);
2013-02-18 08:40:24 -05:00
if (m_Started && --l_AliveTimers == 0) {
UninitializeThread();
}
2013-02-18 08:40:24 -05:00
m_Started = false;
2014-05-28 07:46:39 -04:00
l_Timers.erase(this);
2013-02-17 13:14:34 -05:00
/* Notify the worker thread that we've disabled a timer. */
2014-12-05 09:55:20 -05:00
l_TimerCV.notify_all();
while (wait && m_Running)
l_TimerCV.wait(lock);
}
void Timer::Reschedule(double next)
{
InternalReschedule(false, next);
2012-03-28 07:24:49 -04:00
}
void Timer::InternalReschedule(bool completed, double next)
{
std::unique_lock<std::mutex> lock (l_TimerMutex);
InternalRescheduleUnlocked(completed, next);
}
/**
* Reschedules this timer.
*
* @param completed Whether the timer has just completed its callback.
2013-02-17 13:14:34 -05: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::InternalRescheduleUnlocked(bool completed, double next)
2012-03-28 07:24:49 -04: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 13:14:34 -05:00
2012-03-28 07:24:49 -04:00
m_Next = next;
2013-02-17 13:14:34 -05:00
if (m_Started && !m_Running) {
2013-02-18 08:40:24 -05:00
/* Remove and re-add the timer to update the index. */
2014-05-28 07:46:39 -04:00
l_Timers.erase(this);
l_Timers.insert(this);
2013-02-17 13:14:34 -05:00
2013-02-18 08:40:24 -05:00
/* Notify the worker that we've rescheduled a timer. */
2014-12-05 09:55:20 -05:00
l_TimerCV.notify_all();
2013-02-18 08:40:24 -05:00
}
2013-02-17 13:14:34 -05:00
}
/**
* Retrieves when the timer is next due.
*
* @returns The timestamp.
*/
double Timer::GetNext() const
2013-02-17 13:14:34 -05:00
{
2021-02-02 04:16:04 -05:00
std::unique_lock<std::mutex> lock(l_TimerMutex);
2013-02-17 13:14:34 -05:00
return m_Next;
2012-03-28 07:24:49 -04:00
}
2012-09-25 08:03:41 -04:00
/**
* Adjusts all periodic timers by adding the specified amount of time to their
2012-09-25 08:03:41 -04:00
* next scheduled timestamp.
*
* @param adjustment The adjustment.
*/
void Timer::AdjustTimers(double adjustment)
{
2021-02-02 04:16:04 -05:00
std::unique_lock<std::mutex> lock(l_TimerMutex);
2013-02-17 13:14:34 -05:00
double now = Utility::GetTime();
2013-03-16 16:18:53 -04:00
typedef boost::multi_index::nth_index<TimerSet, 1>::type TimerView;
2013-03-18 06:02:18 -04:00
TimerView& idx = boost::get<1>(l_Timers);
2013-02-17 13:14:34 -05:00
2014-05-28 07:46:39 -04:00
std::vector<Timer *> timers;
for (Timer *timer : idx) {
/* Don't schedule the next call if this is not a periodic timer. */
if (timer->m_Interval <= 0) {
continue;
}
2015-01-12 06:36:49 -05: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 06:15:46 -04:00
}
2013-02-17 13:14:34 -05:00
}
for (Timer *timer : timers) {
l_Timers.erase(timer);
2014-05-28 07:46:39 -04:00
l_Timers.insert(timer);
}
2013-02-17 13:14:34 -05:00
/* Notify the worker that we've rescheduled some timers. */
2014-12-05 09:55:20 -05:00
l_TimerCV.notify_all();
2013-02-17 13:14:34 -05:00
}
/**
* Worker thread proc for Timer objects.
*/
void Timer::TimerThreadProc()
2013-02-17 13:14:34 -05:00
{
2021-02-02 04:16:04 -05:00
namespace ch = std::chrono;
Log(LogDebug, "Timer", "TimerThreadProc started.");
Utility::SetThreadName("Timer Thread");
std::unique_lock<std::mutex> lock (l_TimerMutex);
2013-02-17 13:14:34 -05:00
for (;;) {
2013-03-16 16:18:53 -04:00
typedef boost::multi_index::nth_index<TimerSet, 1>::type NextTimerView;
2013-03-18 06:02:18 -04:00
NextTimerView& idx = boost::get<1>(l_Timers);
2013-02-17 13:14:34 -05:00
/* Wait until there is at least one timer. */
2014-12-05 09:55:20 -05:00
while (idx.empty() && !l_StopTimerThread)
l_TimerCV.wait(lock);
2013-02-17 13:14:34 -05:00
2014-12-05 09:55:20 -05:00
if (l_StopTimerThread)
2013-02-19 17:02:08 -05:00
break;
auto it = idx.begin();
// timer->~Timer() may be called at any moment (if the last
// smart pointer gets destroyed) or even already waiting for
// l_TimerMutex (before doing anything else) which we have
// locked at the moment. Until our unlock using *timer is safe.
2014-05-28 07:46:39 -04:00
Timer *timer = *it;
2013-02-17 13:14:34 -05:00
2021-02-02 04:16:04 -05:00
ch::time_point<ch::system_clock, ch::duration<double>> next (ch::duration<double>(timer->m_Next));
2013-02-17 13:14:34 -05:00
2021-02-02 04:16:04 -05:00
if (next - ch::system_clock::now() > ch::duration<double>(0.01)) {
2013-02-17 13:14:34 -05:00
/* Wait for the next timer. */
2021-02-02 04:16:04 -05:00
l_TimerCV.wait_until(lock, next);
2013-02-17 13:14:34 -05:00
continue;
}
/* Remove the timer from the list so it doesn't get called again
* until the current call is completed. */
2013-03-18 06:02:18 -04:00
l_Timers.erase(timer);
2013-02-17 13:14:34 -05:00
auto keepAlive (timer->m_Self.lock());
if (!keepAlive) {
// The last std::shared_ptr is gone, let ~Timer() proceed
continue;
}
2015-02-27 08:07:12 -05:00
timer->m_Running = true;
2013-02-26 04:13:54 -05:00
lock.unlock();
2013-02-17 13:14:34 -05:00
/* Asynchronously call the timer. */
Utility::QueueAsyncCallback([timer=std::move(keepAlive)]() { timer->Call(); });
lock.lock();
2012-09-25 08:03:41 -04:00
}
}