From eb377da959b7cfdf427fc568d75f1f1f1d188f39 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Fri, 21 Feb 2025 08:52:37 -0400 Subject: [PATCH] MT#62181 wheeltimer: add shutdown procedure Add flag to stop the thread and then wait for it to actually stop. Change-Id: Ibdcf68b7e574301dec3abcf1c2ac57182503214c --- core/sip/wheeltimer.cpp | 4 +++- core/sip/wheeltimer.h | 16 +++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/core/sip/wheeltimer.cpp b/core/sip/wheeltimer.cpp index 11954447..be760bb4 100644 --- a/core/sip/wheeltimer.cpp +++ b/core/sip/wheeltimer.cpp @@ -74,7 +74,7 @@ void _wheeltimer::remove_timer(timer* t) void _wheeltimer::run() { - while(true){ + while(!stopped){ // figure out whether there's anything to run, and for how long to sleep @@ -112,6 +112,8 @@ void _wheeltimer::run() // all done, remove bucket buckets.erase(beg); } + + shutdown_finished.set(true); } void _wheeltimer::process_current_timers(timer_list& list, std::unique_lock& lock) diff --git a/core/sip/wheeltimer.h b/core/sip/wheeltimer.h index 91e28539..a1ada79b 100644 --- a/core/sip/wheeltimer.h +++ b/core/sip/wheeltimer.h @@ -124,6 +124,9 @@ class _wheeltimer: // future (or if no timers exist). Needed not to miss the shutdown flag being set. const uint64_t max_sleep_time = 500000; // half a second + atomic_bool stopped; + AmCondition shutdown_finished; + void place_timer(timer* t, uint64_t, uint64_t); void add_timer_to_bucket(timer* t, uint64_t); @@ -135,15 +138,18 @@ class _wheeltimer: protected: void run(); - void on_stop(){} + void on_stop(){ + stopped = true; + shutdown_finished.wait_for(); + } _wheeltimer() - : resolution(20000) // 20 ms == 20000 us - {} + : resolution(20000), // 20 ms == 20000 us + stopped(false), shutdown_finished(false) {} _wheeltimer(uint64_t _resolution) - : resolution(_resolution) - {} + : resolution(_resolution), + stopped(false), shutdown_finished(false) {} public: //clock reference