/* make sure the thread pool gets initialized */
GetTP().Start();
-
- Timer::Initialize();
}
void Application::UninitializeBase(void)
*/
void Application::RunEventLoop(void)
{
- Timer::Initialize();
-
double lastLoop = Utility::GetTime();
mainloop:
static std::thread l_TimerThread;
static bool l_StopTimerThread;
static TimerSet l_Timers;
+static int l_AliveTimers;
/**
* Constructor for the Timer class.
Stop(true);
}
-/**
- * Initializes the timer sub-system.
- */
-void Timer::Initialize(void)
-{
- boost::mutex::scoped_lock lock(l_TimerMutex);
- l_StopTimerThread = false;
- l_TimerThread = std::thread(&Timer::TimerThreadProc);
-}
-
-/**
- * Disables the timer sub-system.
- */
void Timer::Uninitialize(void)
{
- {
- boost::mutex::scoped_lock lock(l_TimerMutex);
- l_StopTimerThread = true;
- l_TimerCV.notify_all();
- }
-
- if (l_TimerThread.joinable())
- l_TimerThread.join();
+ {
+ boost::mutex::scoped_lock lock(l_TimerMutex);
+ l_StopTimerThread = true;
+ l_TimerCV.notify_all();
+ }
+
+ if (l_TimerThread.joinable())
+ l_TimerThread.join();
}
/**
{
boost::mutex::scoped_lock lock(l_TimerMutex);
m_Started = true;
+
+ if (l_AliveTimers++ == 0) {
+ l_StopTimerThread = false;
+ l_TimerThread = std::thread(&Timer::TimerThreadProc);
+ }
}
InternalReschedule(false);
boost::mutex::scoped_lock lock(l_TimerMutex);
+ if (m_Started && --l_AliveTimers == 0) {
+ l_StopTimerThread = true;
+ l_TimerCV.notify_all();
+
+ lock.unlock();
+
+ if (l_TimerThread.joinable() && l_TimerThread.get_id() != std::this_thread::get_id())
+ l_TimerThread.join();
+
+ lock.lock();
+ }
+
m_Started = false;
l_Timers.erase(this);
Timer(void);
~Timer(void);
+ static void Uninitialize(void);
+
void SetInterval(double interval);
double GetInterval(void) const;
static void TimerThreadProc(void);
- static void Initialize(void);
- static void Uninitialize(void);
-
- friend class Application;
friend class TimerHolder;
};
#include "base/timer.hpp"
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
+#include <boost/thread/once.hpp>
using namespace icinga;
boost::signals2::signal<void (const Comment::Ptr&)> Comment::OnCommentAdded;
boost::signals2::signal<void (const Comment::Ptr&)> Comment::OnCommentRemoved;
-INITIALIZE_ONCE(&Comment::StaticInitialize);
-
REGISTER_TYPE(Comment);
-void Comment::StaticInitialize(void)
-{
- l_CommentsExpireTimer = new Timer();
- l_CommentsExpireTimer->SetInterval(60);
- l_CommentsExpireTimer->OnTimerExpired.connect(std::bind(&Comment::CommentsExpireTimerHandler));
- l_CommentsExpireTimer->Start();
-}
-
String CommentNameComposer::MakeName(const String& shortName, const Object::Ptr& context) const
{
Comment::Ptr comment = dynamic_pointer_cast<Comment>(context);
{
ObjectImpl<Comment>::Start(runtimeCreated);
+ static boost::once_flag once = BOOST_ONCE_INIT;
+
+ boost::call_once(once, []() {
+ l_CommentsExpireTimer = new Timer();
+ l_CommentsExpireTimer->SetInterval(60);
+ l_CommentsExpireTimer->OnTimerExpired.connect(std::bind(&Comment::CommentsExpireTimerHandler));
+ l_CommentsExpireTimer->Start();
+ });
+
{
boost::mutex::scoped_lock lock(l_CommentMutex);
static String GetCommentIDFromLegacyID(int id);
- static void StaticInitialize(void);
-
protected:
virtual void OnAllConfigLoaded(void) override;
virtual void Start(bool runtimeCreated) override;
#include "base/timer.hpp"
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
+#include <boost/thread/once.hpp>
using namespace icinga;
boost::signals2::signal<void (const Downtime::Ptr&)> Downtime::OnDowntimeStarted;
boost::signals2::signal<void (const Downtime::Ptr&)> Downtime::OnDowntimeTriggered;
-INITIALIZE_ONCE(&Downtime::StaticInitialize);
-
REGISTER_TYPE(Downtime);
-void Downtime::StaticInitialize(void)
-{
- l_DowntimesStartTimer = new Timer();
- l_DowntimesStartTimer->SetInterval(5);
- l_DowntimesStartTimer->OnTimerExpired.connect(std::bind(&Downtime::DowntimesStartTimerHandler));
- l_DowntimesStartTimer->Start();
-
- l_DowntimesExpireTimer = new Timer();
- l_DowntimesExpireTimer->SetInterval(60);
- l_DowntimesExpireTimer->OnTimerExpired.connect(std::bind(&Downtime::DowntimesExpireTimerHandler));
- l_DowntimesExpireTimer->Start();
-}
-
String DowntimeNameComposer::MakeName(const String& shortName, const Object::Ptr& context) const
{
Downtime::Ptr downtime = dynamic_pointer_cast<Downtime>(context);
{
ObjectImpl<Downtime>::Start(runtimeCreated);
+ static boost::once_flag once = BOOST_ONCE_INIT;
+
+ boost::call_once(once, []() {
+ l_DowntimesStartTimer = new Timer();
+ l_DowntimesStartTimer->SetInterval(5);
+ l_DowntimesStartTimer->OnTimerExpired.connect(std::bind(&Downtime::DowntimesStartTimerHandler));
+ l_DowntimesStartTimer->Start();
+
+ l_DowntimesExpireTimer = new Timer();
+ l_DowntimesExpireTimer->SetInterval(60);
+ l_DowntimesExpireTimer->OnTimerExpired.connect(std::bind(&Downtime::DowntimesExpireTimerHandler));
+ l_DowntimesExpireTimer->Start();
+ });
+
{
boost::mutex::scoped_lock lock(l_DowntimeMutex);
static String GetDowntimeIDFromLegacyID(int id);
- static void StaticInitialize(void);
-
protected:
virtual void OnAllConfigLoaded(void) override;
virtual void Start(bool runtimeCreated) override;
#include <fstream>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
+#include <boost/thread/once.hpp>
using namespace icinga;
{
ExternalCommandInfo eci;
+ static boost::once_flag once = BOOST_ONCE_INIT;
+
+ boost::call_once(once, []() {
+ RegisterCommands();
+ });
+
{
boost::mutex::scoped_lock lock(GetMutex());
#include "icinga/service.hpp"
#include "base/timer.hpp"
#include "base/configtype.hpp"
-#include "base/initialize.hpp"
#include "base/utility.hpp"
#include "base/objectlock.hpp"
#include "base/convert.hpp"
#include "base/exception.hpp"
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
+#include <boost/thread/once.hpp>
using namespace icinga;
REGISTER_TYPE(ScheduledDowntime);
-INITIALIZE_ONCE(&ScheduledDowntime::StaticInitialize);
-
static Timer::Ptr l_Timer;
String ScheduledDowntimeNameComposer::MakeName(const String& shortName, const Object::Ptr& context) const
return result;
}
-void ScheduledDowntime::StaticInitialize(void)
-{
- l_Timer = new Timer();
- l_Timer->SetInterval(60);
- l_Timer->OnTimerExpired.connect(std::bind(&ScheduledDowntime::TimerProc));
- l_Timer->Start();
-}
-
void ScheduledDowntime::OnAllConfigLoaded(void)
{
ObjectImpl<ScheduledDowntime>::OnAllConfigLoaded();
{
ObjectImpl<ScheduledDowntime>::Start(runtimeCreated);
+ static boost::once_flag once = BOOST_ONCE_INIT;
+
+ boost::call_once(once, []() {
+ l_Timer = new Timer();
+ l_Timer->SetInterval(60);
+ l_Timer->OnTimerExpired.connect(std::bind(&ScheduledDowntime::TimerProc));
+ l_Timer->Start();
+ });
+
Utility::QueueAsyncCallback(std::bind(&ScheduledDowntime::CreateNextDowntime, this));
}
DECLARE_OBJECT(ScheduledDowntime);
DECLARE_OBJECTNAME(ScheduledDowntime);
- static void StaticInitialize(void);
-
Checkable::Ptr GetCheckable(void) const;
static void EvaluateApplyRules(const intrusive_ptr<Host>& host);
#include "base/logger.hpp"
#include "base/timer.hpp"
#include "base/utility.hpp"
+#include <boost/thread/once.hpp>
using namespace icinga;
static Timer::Ptr l_UpdateTimer;
-INITIALIZE_ONCE(&TimePeriod::StaticInitialize);
-
-void TimePeriod::StaticInitialize(void)
-{
- l_UpdateTimer = new Timer();
- l_UpdateTimer->SetInterval(300);
- l_UpdateTimer->OnTimerExpired.connect(std::bind(&TimePeriod::UpdateTimerHandler));
- l_UpdateTimer->Start();
-}
-
void TimePeriod::Start(bool runtimeCreated)
{
ObjectImpl<TimePeriod>::Start(runtimeCreated);
+ static boost::once_flag once = BOOST_ONCE_INIT;
+
+ boost::call_once(once, []() {
+ l_UpdateTimer = new Timer();
+ l_UpdateTimer->SetInterval(300);
+ l_UpdateTimer->OnTimerExpired.connect(std::bind(&TimePeriod::UpdateTimerHandler));
+ l_UpdateTimer->Start();
+ });
+
/* Pre-fill the time period for the next 24 hours. */
double now = Utility::GetTime();
UpdateRegion(now, now + 24 * 3600, true);
DECLARE_OBJECT(TimePeriod);
DECLARE_OBJECTNAME(TimePeriod);
- static void StaticInitialize(void);
-
virtual void Start(bool runtimeCreated) override;
void UpdateRegion(double begin, double end, bool clearExisting);
#include "base/timer.hpp"
#include "base/initialize.hpp"
#include <boost/algorithm/string.hpp>
+#include <boost/thread/once.hpp>
#include <set>
using namespace icinga;
l_ApiScriptFrames.erase(key);
}
-INITIALIZE_ONCE([]() {
- l_FrameCleanupTimer = new Timer();
- l_FrameCleanupTimer->OnTimerExpired.connect(std::bind(ScriptFrameCleanupHandler));
- l_FrameCleanupTimer->SetInterval(30);
- l_FrameCleanupTimer->Start();
-});
+static void EnsureFrameCleanupTimer(void)
+{
+ static boost::once_flag once = BOOST_ONCE_INIT;
+
+ boost::call_once(once, []() {
+ l_FrameCleanupTimer = new Timer();
+ l_FrameCleanupTimer->OnTimerExpired.connect(std::bind(ScriptFrameCleanupHandler));
+ l_FrameCleanupTimer->SetInterval(30);
+ l_FrameCleanupTimer->Start();
+ });
+}
bool ConsoleHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response, const Dictionary::Ptr& params)
{
Log(LogNotice, "Console")
<< "Executing expression: " << command;
+ EnsureFrameCleanupTimer();
+
ApiScriptFrame& lsf = l_ApiScriptFrames[session];
lsf.Seen = Utility::GetTime();
Log(LogInformation, "Console")
<< "Auto-completing expression: " << command;
+ EnsureFrameCleanupTimer();
+
ApiScriptFrame& lsf = l_ApiScriptFrames[session];
lsf.Seen = Utility::GetTime();
REGISTER_APIFUNCTION(Heartbeat, event, &JsonRpcConnection::HeartbeatAPIHandler);
-static Timer::Ptr l_HeartbeatTimer;
-
-INITIALIZE_ONCE([]() {
- l_HeartbeatTimer = new Timer();
- l_HeartbeatTimer->OnTimerExpired.connect(std::bind(&JsonRpcConnection::HeartbeatTimerHandler));
- l_HeartbeatTimer->SetInterval(10);
- l_HeartbeatTimer->Start();
-});
-
void JsonRpcConnection::HeartbeatTimerHandler(void)
{
for (const Endpoint::Ptr& endpoint : ConfigType::GetObjectsByType<Endpoint>()) {
static WorkQueue *l_JsonRpcConnectionWorkQueues;
static size_t l_JsonRpcConnectionWorkQueueCount;
static int l_JsonRpcConnectionNextID;
+static Timer::Ptr l_HeartbeatTimer;
JsonRpcConnection::JsonRpcConnection(const String& identity, bool authenticated,
const TlsStream::Ptr& stream, ConnectionRole role)
for (size_t i = 0; i < l_JsonRpcConnectionWorkQueueCount; i++) {
l_JsonRpcConnectionWorkQueues[i].SetName("JsonRpcConnection, #" + Convert::ToString(i));
}
+
+ l_HeartbeatTimer = new Timer();
+ l_HeartbeatTimer->OnTimerExpired.connect(std::bind(&JsonRpcConnection::HeartbeatTimerHandler));
+ l_HeartbeatTimer->SetInterval(10);
+ l_HeartbeatTimer->Start();
}
void JsonRpcConnection::Start(void)