diff --git a/src/win32/ComWorker.cxx b/src/win32/ComWorker.cxx index 99e4badcf..c4757ec49 100644 --- a/src/win32/ComWorker.cxx +++ b/src/win32/ComWorker.cxx @@ -28,15 +28,18 @@ COMWorker::Work() noexcept { SetThreadName("COM Worker"); COM com; - while (true) { - if (!running_flag.test_and_set()) { - return; - } - while (!spsc_buffer.empty()) { - std::function function; - spsc_buffer.pop(function); + + std::unique_lock lock{mutex}; + while (running_flag) { + while (!queue.empty()) { + auto function = std::move(queue.front()); + queue.pop(); + + lock.unlock(); function(); + lock.lock(); } - event.Wait(200); + + cond.wait(lock); } } diff --git a/src/win32/ComWorker.hxx b/src/win32/ComWorker.hxx index 32aa38187..c0b2b3534 100644 --- a/src/win32/ComWorker.hxx +++ b/src/win32/ComWorker.hxx @@ -20,19 +20,23 @@ #ifndef MPD_WIN32_COM_WORKER_HXX #define MPD_WIN32_COM_WORKER_HXX -#include "WinEvent.hxx" +#include "thread/Cond.hxx" #include "thread/Future.hxx" +#include "thread/Mutex.hxx" #include "thread/Thread.hxx" -#include +#include +#include // Worker thread for all COM operation class COMWorker { - Thread thread{BIND_THIS_METHOD(Work)}; + Mutex mutex; + Cond cond; - boost::lockfree::spsc_queue> spsc_buffer{32}; - std::atomic_flag running_flag = true; - WinEvent event{}; + std::queue> queue; + bool running_flag = true; + + Thread thread{BIND_THIS_METHOD(Work)}; public: COMWorker() { @@ -70,13 +74,15 @@ public: private: void Finish() noexcept { - running_flag.clear(); - event.Set(); + const std::scoped_lock lock{mutex}; + running_flag = false; + cond.notify_one(); } void Push(const std::function &function) { - spsc_buffer.push(function); - event.Set(); + const std::scoped_lock lock{mutex}; + queue.push(function); + cond.notify_one(); } void Work() noexcept;