diff options
author | Lioncash <mathew1800@gmail.com> | 2019-02-13 04:03:48 +0100 |
---|---|---|
committer | Lioncash <mathew1800@gmail.com> | 2019-02-13 04:09:51 +0100 |
commit | f0bfb24c61ceb61b621ba38bcc1a1aa020a9d397 (patch) | |
tree | 9c66014b934179c03e93e3ae640326e7ad3a2a57 /src/common/threadsafe_queue.h | |
parent | Merge pull request #2114 from lioncash/global (diff) | |
download | yuzu-f0bfb24c61ceb61b621ba38bcc1a1aa020a9d397.tar yuzu-f0bfb24c61ceb61b621ba38bcc1a1aa020a9d397.tar.gz yuzu-f0bfb24c61ceb61b621ba38bcc1a1aa020a9d397.tar.bz2 yuzu-f0bfb24c61ceb61b621ba38bcc1a1aa020a9d397.tar.lz yuzu-f0bfb24c61ceb61b621ba38bcc1a1aa020a9d397.tar.xz yuzu-f0bfb24c61ceb61b621ba38bcc1a1aa020a9d397.tar.zst yuzu-f0bfb24c61ceb61b621ba38bcc1a1aa020a9d397.zip |
Diffstat (limited to '')
-rw-r--r-- | src/common/threadsafe_queue.h | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/src/common/threadsafe_queue.h b/src/common/threadsafe_queue.h index edf13bc49..2660b118a 100644 --- a/src/common/threadsafe_queue.h +++ b/src/common/threadsafe_queue.h @@ -14,10 +14,10 @@ #include "common/common_types.h" namespace Common { -template <typename T, bool NeedSize = true> +template <typename T> class SPSCQueue { public: - SPSCQueue() : size(0) { + SPSCQueue() { write_ptr = read_ptr = new ElementPtr(); } ~SPSCQueue() { @@ -26,12 +26,11 @@ public: } u32 Size() const { - static_assert(NeedSize, "using Size() on FifoQueue without NeedSize"); return size.load(); } bool Empty() const { - return !read_ptr->next.load(); + return Size() == 0; } T& Front() const { @@ -47,13 +46,13 @@ public: ElementPtr* new_ptr = new ElementPtr(); write_ptr->next.store(new_ptr, std::memory_order_release); write_ptr = new_ptr; - if (NeedSize) - size++; + + ++size; } void Pop() { - if (NeedSize) - size--; + --size; + ElementPtr* tmpptr = read_ptr; // advance the read pointer read_ptr = tmpptr->next.load(); @@ -66,8 +65,7 @@ public: if (Empty()) return false; - if (NeedSize) - size--; + --size; ElementPtr* tmpptr = read_ptr; read_ptr = tmpptr->next.load(std::memory_order_acquire); @@ -103,13 +101,13 @@ private: ElementPtr* write_ptr; ElementPtr* read_ptr; - std::atomic<u32> size; + std::atomic<u32> size{0}; }; // a simple thread-safe, // single reader, multiple writer queue -template <typename T, bool NeedSize = true> +template <typename T> class MPSCQueue { public: u32 Size() const { @@ -144,7 +142,7 @@ public: } private: - SPSCQueue<T, NeedSize> spsc_queue; + SPSCQueue<T> spsc_queue; std::mutex write_lock; }; } // namespace Common |