From 25e8b28935f92e2bdf15bea992301fbd35a5bbde Mon Sep 17 00:00:00 2001 From: yuzubot Date: Thu, 12 Dec 2019 13:01:22 +0000 Subject: [PATCH] "Merge Tagged PR 3173" --- src/common/threadsafe_queue.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/common/threadsafe_queue.h b/src/common/threadsafe_queue.h index e714ba5b3..dda7b3a09 100644 --- a/src/common/threadsafe_queue.h +++ b/src/common/threadsafe_queue.h @@ -46,9 +46,19 @@ public: ElementPtr* new_ptr = new ElementPtr(); write_ptr->next.store(new_ptr, std::memory_order_release); write_ptr = new_ptr; - cv.notify_one(); + const size_t previous_size{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(); + } else { + cv.notify_one(); + } } void Pop() {