From 512273a5077e1aaf6ab8506595d7d1e36e4d7a02 Mon Sep 17 00:00:00 2001 From: Scott Mansell Date: Mon, 23 Jan 2023 19:48:07 +1300 Subject: [PATCH] WorkQueueThread: Add flush capability --- Source/Core/Common/WorkQueueThread.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Source/Core/Common/WorkQueueThread.h b/Source/Core/Common/WorkQueueThread.h index 6039f7b3b9..f74c164e60 100644 --- a/Source/Core/Common/WorkQueueThread.h +++ b/Source/Core/Common/WorkQueueThread.h @@ -70,6 +70,19 @@ public: } } + // Doesn't return until the most recent function invocation has finished. + void Flush() + { + if (m_thread.joinable()) + { + m_flush.Set(); + Clear(); + m_flushed.Wait(); + } + } + + bool IsFlushing() const { return m_flush.IsSet() || m_shutdown.IsSet(); } + private: void ThreadLoop() { @@ -83,7 +96,14 @@ private: { std::unique_lock lg(m_lock); if (m_items.empty()) + { + if (m_flush.IsSet()) + { + m_flush.Clear(); + m_flushed.Set(); + } break; + } T item{std::move(m_items.front())}; m_items.pop(); lg.unlock(); @@ -101,6 +121,8 @@ private: Common::Event m_wakeup; Common::Flag m_shutdown; Common::Flag m_cancelled; + Common::Flag m_flush; + Common::Event m_flushed; std::mutex m_lock; std::queue m_items; };