MCPcopy Create free account
hub / github.com/Icinga/icinga2 / TimerThreadProc

Method TimerThreadProc

lib/base/timer.cpp:297–355  ·  view source on GitHub ↗

* Worker thread proc for Timer objects. */

Source from the content-addressed store, hash-verified

295 * Worker thread proc for Timer objects.
296 */
297void Timer::TimerThreadProc()
298{
299 namespace ch = std::chrono;
300
301 Log(LogDebug, "Timer", "TimerThreadProc started.");
302
303 Utility::SetThreadName("Timer Thread");
304
305 std::unique_lock<std::mutex> lock (l_TimerMutex);
306
307 for (;;) {
308 typedef boost::multi_index::nth_index<TimerSet, 1>::type NextTimerView;
309 NextTimerView& idx = boost::get<1>(l_Timers);
310
311 /* Wait until there is at least one timer. */
312 while (idx.empty() && !l_StopTimerThread)
313 l_TimerCV.wait(lock);
314
315 if (l_StopTimerThread)
316 break;
317
318 auto it = idx.begin();
319
320 // timer->~Timer() may be called at any moment (if the last
321 // smart pointer gets destroyed) or even already waiting for
322 // l_TimerMutex (before doing anything else) which we have
323 // locked at the moment. Until our unlock using *timer is safe.
324 Timer *timer = *it;
325
326 ch::time_point<ch::system_clock, ch::duration<double>> next (ch::duration<double>(timer->m_Next));
327
328 if (next - ch::system_clock::now() > ch::duration<double>(0.01)) {
329 /* Wait for the next timer. */
330 l_TimerCV.wait_until(lock, next);
331
332 continue;
333 }
334
335 /* Remove the timer from the list so it doesn't get called again
336 * until the current call is completed. */
337 l_Timers.erase(timer);
338
339 auto keepAlive (timer->m_Self.lock());
340
341 if (!keepAlive) {
342 // The last std::shared_ptr is gone, let ~Timer() proceed
343 continue;
344 }
345
346 timer->m_Running = true;
347
348 lock.unlock();
349
350 /* Asynchronously call the timer. */
351 Utility::QueueAsyncCallback([timer=std::move(keepAlive)]() { timer->Call(); });
352
353 lock.lock();
354 }

Callers

nothing calls this directly

Calls 5

LogClass · 0.85
CallMethod · 0.80
emptyMethod · 0.45
beginMethod · 0.45
eraseMethod · 0.45

Tested by

no test coverage detected