Merge pull request #6457 from Icinga/bugfix/daemonize-timer-6445

Ensure that timer thread is initialized after Daemonize()
This commit is contained in:
Michael Friedrich 2018-07-19 13:45:53 +02:00 committed by GitHub
commit 15a8f87ac1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 20 deletions

View File

@ -138,8 +138,11 @@ void Application::InitializeBase()
Loader::ExecuteDeferredInitializers(); Loader::ExecuteDeferredInitializers();
/* make sure the thread pool gets initialized */ /* Make sure the thread pool gets initialized. */
GetTP().Start(); GetTP().Start();
/* Make sure the timer thread gets initialized. */
Timer::Initialize();
} }
void Application::UninitializeBase() void Application::UninitializeBase()
@ -300,7 +303,6 @@ void Application::SetArgV(char **argv)
*/ */
void Application::RunEventLoop() void Application::RunEventLoop()
{ {
#ifdef HAVE_SYSTEMD #ifdef HAVE_SYSTEMD
sd_notify(0, "READY=1"); sd_notify(0, "READY=1");
#endif /* HAVE_SYSTEMD */ #endif /* HAVE_SYSTEMD */

View File

@ -19,6 +19,7 @@
#include "base/timer.hpp" #include "base/timer.hpp"
#include "base/debug.hpp" #include "base/debug.hpp"
#include "base/logger.hpp"
#include "base/utility.hpp" #include "base/utility.hpp"
#include <boost/thread/mutex.hpp> #include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp> #include <boost/thread/condition_variable.hpp>
@ -71,7 +72,6 @@ static boost::condition_variable l_TimerCV;
static std::thread l_TimerThread; static std::thread l_TimerThread;
static bool l_StopTimerThread; static bool l_StopTimerThread;
static TimerSet l_Timers; static TimerSet l_Timers;
static int l_AliveTimers;
/** /**
* Destructor for the Timer class. * Destructor for the Timer class.
@ -81,6 +81,13 @@ Timer::~Timer()
Stop(true); Stop(true);
} }
void Timer::Initialize()
{
boost::mutex::scoped_lock lock(l_TimerMutex);
l_StopTimerThread = false;
l_TimerThread = std::thread(&Timer::TimerThreadProc);
}
void Timer::Uninitialize() void Timer::Uninitialize()
{ {
{ {
@ -139,11 +146,6 @@ void Timer::Start()
{ {
boost::mutex::scoped_lock lock(l_TimerMutex); boost::mutex::scoped_lock lock(l_TimerMutex);
m_Started = true; m_Started = true;
if (l_AliveTimers++ == 0) {
l_StopTimerThread = false;
l_TimerThread = std::thread(&Timer::TimerThreadProc);
}
} }
InternalReschedule(false); InternalReschedule(false);
@ -159,18 +161,6 @@ void Timer::Stop(bool wait)
boost::mutex::scoped_lock lock(l_TimerMutex); 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; m_Started = false;
l_Timers.erase(this); l_Timers.erase(this);
@ -270,6 +260,8 @@ void Timer::AdjustTimers(double adjustment)
*/ */
void Timer::TimerThreadProc() void Timer::TimerThreadProc()
{ {
Log(LogDebug, "Timer", "TimerThreadProc started.");
Utility::SetThreadName("Timer Thread"); Utility::SetThreadName("Timer Thread");
for (;;) { for (;;) {

View File

@ -40,6 +40,7 @@ public:
~Timer() override; ~Timer() override;
static void Initialize();
static void Uninitialize(); static void Uninitialize();
void SetInterval(double interval); void SetInterval(double interval);

View File

@ -88,6 +88,9 @@ static bool Daemonize()
_exit(EXIT_SUCCESS); _exit(EXIT_SUCCESS);
} }
Log(LogDebug, "Daemonize()")
<< "Child process with PID " << Utility::GetPid() << " continues; re-initializing base.";
Application::InitializeBase(); Application::InitializeBase();
#endif /* _WIN32 */ #endif /* _WIN32 */