3
0
Fork 0
forked from suyu/suyu

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.
This commit is contained in:
Lioncash 2019-02-12 22:03:48 -05:00
parent c425a1a857
commit f0bfb24c61
2 changed files with 13 additions and 15 deletions

View file

@ -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

View file

@ -54,10 +54,10 @@ static std::vector<Event> 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<Event, false> ts_queue;
static Common::MPSCQueue<Event> ts_queue;
// the queue for unscheduling the events from other threads threadsafe
static Common::MPSCQueue<std::pair<const EventType*, u64>, false> unschedule_queue;
static Common::MPSCQueue<std::pair<const EventType*, u64>> unschedule_queue;
constexpr int MAX_SLICE_LENGTH = 20000;