From 25e915fe95da228ccfc5b8b9b06fae31c32d7320 Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Tue, 21 Jun 2022 16:29:09 +0100 Subject: [PATCH] fix #5990: deadlock in the scoped_timer Thanks to Felix Kohlgrueber for reporting the bug and for the analysis --- src/util/scoped_timer.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/util/scoped_timer.cpp b/src/util/scoped_timer.cpp index e8a72e245..b5968cc06 100644 --- a/src/util/scoped_timer.cpp +++ b/src/util/scoped_timer.cpp @@ -51,10 +51,9 @@ static atomic num_workers(0); static void thread_func(scoped_timer_state *s) { workers.lock(); while (true) { - s->cv.wait(workers, [=]{ return s->work > IDLE; }); + s->cv.wait(workers, [=]{ return s->work != IDLE; }); workers.unlock(); - // exiting.. if (s->work == EXITING) return; @@ -87,16 +86,17 @@ scoped_timer::scoped_timer(unsigned ms, event_handler * eh) { s = new scoped_timer_state; new_worker = true; ++num_workers; + s->work = WORKING; } else { s = available_workers.back(); available_workers.pop_back(); + s->work = WORKING; workers.unlock(); } s->ms = ms; s->eh = eh; s->m_mutex.lock(); - s->work = WORKING; if (new_worker) { s->m_thread = std::thread(thread_func, s); }