From 591c6a64d7623911f78812a065a647a43c7ad324 Mon Sep 17 00:00:00 2001 From: bunnei Date: Mon, 16 Dec 2019 20:39:53 -0500 Subject: [PATCH] common: SPSCQueue: Notify after incrementing queue size. --- src/common/threadsafe_queue.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/common/threadsafe_queue.h b/src/common/threadsafe_queue.h index 9017ff64f..594d14c15 100644 --- a/src/common/threadsafe_queue.h +++ b/src/common/threadsafe_queue.h @@ -46,8 +46,15 @@ public: ElementPtr* new_ptr = new ElementPtr(); write_ptr->next.store(new_ptr, std::memory_order_release); write_ptr = new_ptr; - ++size; + const size_t previous_size{size++}; + + // Acquire the mutex and then immediately release it as a fence. + // TODO(bunnei): This can be replaced with C++20 waitable atomics when properly supported. + // See discussion on https://github.com/yuzu-emu/yuzu/pull/3173 for details. + if (previous_size == 0) { + std::lock_guard lock{cv_mutex}; + } cv.notify_one(); }