diff options
author | bunnei <bunneidev@gmail.com> | 2019-02-16 15:34:49 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-16 15:34:49 -0500 |
commit | cd7e1183e21002e9941e97358ff373abfade9132 (patch) | |
tree | 10f77ac5b28e08f1380a27410b15d0e0ab92e9c9 /src/common/threadsafe_queue.h | |
parent | 99da6362c43a24c608a2790f668f10a62e3b80a6 (diff) | |
parent | 2195f10d152a52e01ccab0a6528f8758752d66a9 (diff) |
Merge pull request #2128 from FearlessTobi/port-4197
Port citra-emu/citra#4197: "threadsafe_queue: Add PopWait and use it where possible "
Diffstat (limited to 'src/common/threadsafe_queue.h')
-rw-r--r-- | src/common/threadsafe_queue.h | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/common/threadsafe_queue.h b/src/common/threadsafe_queue.h index f553efdc9..821e8536a 100644 --- a/src/common/threadsafe_queue.h +++ b/src/common/threadsafe_queue.h @@ -8,6 +8,7 @@ // single reader, single writer queue #include <atomic> +#include <condition_variable> #include <cstddef> #include <mutex> #include <utility> @@ -45,6 +46,7 @@ public: ElementPtr* new_ptr = new ElementPtr(); write_ptr->next.store(new_ptr, std::memory_order_release); write_ptr = new_ptr; + cv.notify_one(); ++size; } @@ -74,6 +76,16 @@ public: return true; } + T PopWait() { + if (Empty()) { + std::unique_lock<std::mutex> lock(cv_mutex); + cv.wait(lock, [this]() { return !Empty(); }); + } + T t; + Pop(t); + return t; + } + // not thread-safe void Clear() { size.store(0); @@ -101,6 +113,8 @@ private: ElementPtr* write_ptr; ElementPtr* read_ptr; std::atomic_size_t size{0}; + std::mutex cv_mutex; + std::condition_variable cv; }; // a simple thread-safe, @@ -135,6 +149,10 @@ public: return spsc_queue.Pop(t); } + T PopWait() { + return spsc_queue.PopWait(); + } + // not thread-safe void Clear() { spsc_queue.Clear(); |