From f0bfb24c61ceb61b621ba38bcc1a1aa020a9d397 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 12 Feb 2019 22:03:48 -0500 Subject: [PATCH 1/2] threadsafe_queue: Remove NeedSize template parameter The necessity of this parameter is dubious at best, and in 2019 probably offers completely negligible savings as opposed to just leaving this enabled. This removes it and simplifies the overall interface. --- src/common/threadsafe_queue.h | 24 +++++++++++------------- src/core/core_timing.cpp | 4 ++-- 2 files changed, 13 insertions(+), 15 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 +template 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 size; + std::atomic size{0}; }; // a simple thread-safe, // single reader, multiple writer queue -template +template class MPSCQueue { public: u32 Size() const { @@ -144,7 +142,7 @@ public: } private: - SPSCQueue spsc_queue; + SPSCQueue spsc_queue; std::mutex write_lock; }; } // namespace Common diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp index 2b7ca9766..0308030c5 100644 --- a/src/core/core_timing.cpp +++ b/src/core/core_timing.cpp @@ -54,10 +54,10 @@ static std::vector event_queue; static u64 event_fifo_id; // the queue for storing the events from other threads threadsafe until they will be added // to the event_queue by the emu thread -static Common::MPSCQueue ts_queue; +static Common::MPSCQueue ts_queue; // the queue for unscheduling the events from other threads threadsafe -static Common::MPSCQueue, false> unschedule_queue; +static Common::MPSCQueue> unschedule_queue; constexpr int MAX_SLICE_LENGTH = 20000; From 0829ef97cac6cb96c49d1401433d9f46639bfeb8 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 12 Feb 2019 22:12:23 -0500 Subject: [PATCH 2/2] threadsafe_queue: Use std::size_t for representing size Makes it consistent with the regular standard containers in terms of size representation. This also gets rid of dependence on our own type aliases, removing the need for an include. --- src/common/threadsafe_queue.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/common/threadsafe_queue.h b/src/common/threadsafe_queue.h index 2660b118a..f553efdc9 100644 --- a/src/common/threadsafe_queue.h +++ b/src/common/threadsafe_queue.h @@ -7,11 +7,10 @@ // a simple lockless thread-safe, // single reader, single writer queue -#include #include #include #include -#include "common/common_types.h" +#include namespace Common { template @@ -25,7 +24,7 @@ public: delete read_ptr; } - u32 Size() const { + std::size_t Size() const { return size.load(); } @@ -87,7 +86,7 @@ private: // and a pointer to the next ElementPtr class ElementPtr { public: - ElementPtr() : next(nullptr) {} + ElementPtr() {} ~ElementPtr() { ElementPtr* next_ptr = next.load(); @@ -96,12 +95,12 @@ private: } T current; - std::atomic next; + std::atomic next{nullptr}; }; ElementPtr* write_ptr; ElementPtr* read_ptr; - std::atomic size{0}; + std::atomic_size_t size{0}; }; // a simple thread-safe, @@ -110,7 +109,7 @@ private: template class MPSCQueue { public: - u32 Size() const { + std::size_t Size() const { return spsc_queue.Size(); }