mirror of
https://github.com/Lime3DS/Lime3DS.git
synced 2024-11-13 05:25:14 +01:00
Merge pull request #4645 from FearlessTobi/port-2116
Port yuzu-emu/yuzu#2116: "threadsafe_queue: Remove NeedSize template parameter"
This commit is contained in:
commit
93275d752b
@ -7,18 +7,17 @@
|
|||||||
// a simple lockless thread-safe,
|
// a simple lockless thread-safe,
|
||||||
// single reader, single writer queue
|
// single reader, single writer queue
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include "common/common_types.h"
|
#include <utility>
|
||||||
|
|
||||||
namespace Common {
|
namespace Common {
|
||||||
template <typename T, bool NeedSize = true>
|
template <typename T>
|
||||||
class SPSCQueue {
|
class SPSCQueue {
|
||||||
public:
|
public:
|
||||||
SPSCQueue() : size(0) {
|
SPSCQueue() {
|
||||||
write_ptr = read_ptr = new ElementPtr();
|
write_ptr = read_ptr = new ElementPtr();
|
||||||
}
|
}
|
||||||
~SPSCQueue() {
|
~SPSCQueue() {
|
||||||
@ -26,13 +25,12 @@ public:
|
|||||||
delete read_ptr;
|
delete read_ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 Size() const {
|
std::size_t Size() const {
|
||||||
static_assert(NeedSize, "using Size() on FifoQueue without NeedSize");
|
|
||||||
return size.load();
|
return size.load();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Empty() const {
|
bool Empty() const {
|
||||||
return !read_ptr->next.load();
|
return Size() == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
T& Front() const {
|
T& Front() const {
|
||||||
@ -48,14 +46,14 @@ public:
|
|||||||
ElementPtr* new_ptr = new ElementPtr();
|
ElementPtr* new_ptr = new ElementPtr();
|
||||||
write_ptr->next.store(new_ptr, std::memory_order_release);
|
write_ptr->next.store(new_ptr, std::memory_order_release);
|
||||||
write_ptr = new_ptr;
|
write_ptr = new_ptr;
|
||||||
if (NeedSize)
|
++size;
|
||||||
size++;
|
|
||||||
cv.notify_one();
|
cv.notify_one();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Pop() {
|
void Pop() {
|
||||||
if (NeedSize)
|
--size;
|
||||||
size--;
|
|
||||||
ElementPtr* tmpptr = read_ptr;
|
ElementPtr* tmpptr = read_ptr;
|
||||||
// advance the read pointer
|
// advance the read pointer
|
||||||
read_ptr = tmpptr->next.load();
|
read_ptr = tmpptr->next.load();
|
||||||
@ -68,8 +66,7 @@ public:
|
|||||||
if (Empty())
|
if (Empty())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (NeedSize)
|
--size;
|
||||||
size--;
|
|
||||||
|
|
||||||
ElementPtr* tmpptr = read_ptr;
|
ElementPtr* tmpptr = read_ptr;
|
||||||
read_ptr = tmpptr->next.load(std::memory_order_acquire);
|
read_ptr = tmpptr->next.load(std::memory_order_acquire);
|
||||||
@ -101,7 +98,7 @@ private:
|
|||||||
// and a pointer to the next ElementPtr
|
// and a pointer to the next ElementPtr
|
||||||
class ElementPtr {
|
class ElementPtr {
|
||||||
public:
|
public:
|
||||||
ElementPtr() : next(nullptr) {}
|
ElementPtr() = default;
|
||||||
~ElementPtr() {
|
~ElementPtr() {
|
||||||
ElementPtr* next_ptr = next.load();
|
ElementPtr* next_ptr = next.load();
|
||||||
|
|
||||||
@ -110,12 +107,12 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
T current;
|
T current;
|
||||||
std::atomic<ElementPtr*> next;
|
std::atomic<ElementPtr*> next{nullptr};
|
||||||
};
|
};
|
||||||
|
|
||||||
ElementPtr* write_ptr;
|
ElementPtr* write_ptr;
|
||||||
ElementPtr* read_ptr;
|
ElementPtr* read_ptr;
|
||||||
std::atomic<u32> size;
|
std::atomic_size_t size{0};
|
||||||
std::mutex cv_mutex;
|
std::mutex cv_mutex;
|
||||||
std::condition_variable cv;
|
std::condition_variable cv;
|
||||||
};
|
};
|
||||||
@ -123,10 +120,10 @@ private:
|
|||||||
// a simple thread-safe,
|
// a simple thread-safe,
|
||||||
// single reader, multiple writer queue
|
// single reader, multiple writer queue
|
||||||
|
|
||||||
template <typename T, bool NeedSize = true>
|
template <typename T>
|
||||||
class MPSCQueue {
|
class MPSCQueue {
|
||||||
public:
|
public:
|
||||||
u32 Size() const {
|
std::size_t Size() const {
|
||||||
return spsc_queue.Size();
|
return spsc_queue.Size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,7 +159,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SPSCQueue<T, NeedSize> spsc_queue;
|
SPSCQueue<T> spsc_queue;
|
||||||
std::mutex write_lock;
|
std::mutex write_lock;
|
||||||
};
|
};
|
||||||
} // namespace Common
|
} // namespace Common
|
||||||
|
@ -218,7 +218,7 @@ private:
|
|||||||
u64 event_fifo_id = 0;
|
u64 event_fifo_id = 0;
|
||||||
// the queue for storing the events from other threads threadsafe until they will be added
|
// the queue for storing the events from other threads threadsafe until they will be added
|
||||||
// to the event_queue by the emu thread
|
// to the event_queue by the emu thread
|
||||||
Common::MPSCQueue<Event, false> ts_queue;
|
Common::MPSCQueue<Event> ts_queue;
|
||||||
s64 idled_cycles = 0;
|
s64 idled_cycles = 0;
|
||||||
|
|
||||||
// Are we in a function that has been called from Advance()
|
// Are we in a function that has been called from Advance()
|
||||||
|
Loading…
Reference in New Issue
Block a user