mirror of https://github.com/Icinga/icinga2.git
Merge pull request #6467 from Icinga/bugfix/test-runner-does-not-clean-up-6461
Start and stop the timer thread lazily
This commit is contained in:
commit
95d46f57c3
|
@ -72,6 +72,7 @@ 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 = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Destructor for the Timer class.
|
* Destructor for the Timer class.
|
||||||
|
@ -84,20 +85,40 @@ Timer::~Timer()
|
||||||
void Timer::Initialize()
|
void Timer::Initialize()
|
||||||
{
|
{
|
||||||
boost::mutex::scoped_lock lock(l_TimerMutex);
|
boost::mutex::scoped_lock lock(l_TimerMutex);
|
||||||
l_StopTimerThread = false;
|
|
||||||
l_TimerThread = std::thread(&Timer::TimerThreadProc);
|
if (l_AliveTimers > 0) {
|
||||||
|
InitializeThread();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Timer::Uninitialize()
|
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()
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
boost::mutex::scoped_lock lock(l_TimerMutex);
|
|
||||||
l_StopTimerThread = true;
|
l_StopTimerThread = true;
|
||||||
l_TimerCV.notify_all();
|
l_TimerCV.notify_all();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
l_TimerMutex.unlock();
|
||||||
|
|
||||||
if (l_TimerThread.joinable())
|
if (l_TimerThread.joinable())
|
||||||
l_TimerThread.join();
|
l_TimerThread.join();
|
||||||
|
|
||||||
|
l_TimerMutex.lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -146,6 +167,10 @@ 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 == 1) {
|
||||||
|
InitializeThread();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
InternalReschedule(false);
|
InternalReschedule(false);
|
||||||
|
@ -161,6 +186,10 @@ 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) {
|
||||||
|
UninitializeThread();
|
||||||
|
}
|
||||||
|
|
||||||
m_Started = false;
|
m_Started = false;
|
||||||
l_Timers.erase(this);
|
l_Timers.erase(this);
|
||||||
|
|
||||||
|
|
|
@ -42,6 +42,8 @@ public:
|
||||||
|
|
||||||
static void Initialize();
|
static void Initialize();
|
||||||
static void Uninitialize();
|
static void Uninitialize();
|
||||||
|
static void InitializeThread();
|
||||||
|
static void UninitializeThread();
|
||||||
|
|
||||||
void SetInterval(double interval);
|
void SetInterval(double interval);
|
||||||
double GetInterval() const;
|
double GetInterval() const;
|
||||||
|
|
Loading…
Reference in New Issue