pineapple/src/common/threadsafe_queue.h

196 lines
4.5 KiB
C
Raw Normal View History

2020-12-28 16:15:37 +01:00
// Copyright 2010 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#pragma once
2021-08-20 02:44:09 +02:00
// a simple lockless thread-safe,
// single reader, single writer queue
2020-12-28 16:15:37 +01:00
#include <atomic>
#include <condition_variable>
2021-08-20 02:44:09 +02:00
#include <cstddef>
2020-12-28 16:15:37 +01:00
#include <mutex>
2021-08-20 02:44:09 +02:00
#include <utility>
2020-12-28 16:15:37 +01:00
namespace Common {
2021-09-16 06:04:40 +02:00
template <typename T, bool with_stop_token = false>
2021-08-20 02:44:09 +02:00
class SPSCQueue {
2020-12-28 16:15:37 +01:00
public:
2021-08-20 02:44:09 +02:00
SPSCQueue() {
write_ptr = read_ptr = new ElementPtr();
}
~SPSCQueue() {
// this will empty out the whole queue
delete read_ptr;
}
[[nodiscard]] std::size_t Size() const {
return size.load();
}
[[nodiscard]] bool Empty() const {
return Size() == 0;
}
[[nodiscard]] T& Front() const {
return read_ptr->current;
2020-12-28 16:15:37 +01:00
}
template <typename Arg>
void Push(Arg&& t) {
2021-08-20 02:44:09 +02:00
// create the element, add it to the queue
write_ptr->current = std::forward<Arg>(t);
// set the next pointer to a new element ptr
// then advance the write pointer
ElementPtr* new_ptr = new ElementPtr();
write_ptr->next.store(new_ptr, std::memory_order_release);
write_ptr = new_ptr;
++size;
// cv_mutex must be held or else there will be a missed wakeup if the other thread is in the
// line before cv.wait
// TODO(bunnei): This can be replaced with C++20 waitable atomics when properly supported.
// See discussion on https://github.com/yuzu-emu/yuzu/pull/3173 for details.
std::lock_guard lock{cv_mutex};
cv.notify_one();
2020-12-28 16:15:37 +01:00
}
2021-08-20 02:44:09 +02:00
void Pop() {
--size;
ElementPtr* tmpptr = read_ptr;
// advance the read pointer
read_ptr = tmpptr->next.load();
// set the next element to nullptr to stop the recursive deletion
tmpptr->next.store(nullptr);
delete tmpptr; // this also deletes the element
2021-04-07 20:28:12 +02:00
}
2021-08-20 02:44:09 +02:00
bool Pop(T& t) {
if (Empty())
return false;
--size;
ElementPtr* tmpptr = read_ptr;
read_ptr = tmpptr->next.load(std::memory_order_acquire);
t = std::move(tmpptr->current);
tmpptr->next.store(nullptr);
delete tmpptr;
return true;
2020-12-28 16:15:37 +01:00
}
2021-08-16 13:42:12 +02:00
void Wait() {
2021-08-20 02:44:09 +02:00
if (Empty()) {
std::unique_lock lock{cv_mutex};
2021-09-16 06:04:40 +02:00
cv.wait(lock, [this] { return !Empty(); });
2021-08-16 13:42:12 +02:00
}
}
2021-08-20 02:44:09 +02:00
T PopWait() {
Wait();
T t;
Pop(t);
return t;
}
2021-09-16 06:04:40 +02:00
T PopWait(std::stop_token stop_token) {
if (Empty()) {
std::unique_lock lock{cv_mutex};
cv.wait(lock, stop_token, [this] { return !Empty(); });
}
if (stop_token.stop_requested()) {
return T{};
}
T t;
Pop(t);
return t;
}
2021-08-20 02:44:09 +02:00
// not thread-safe
2020-12-28 16:15:37 +01:00
void Clear() {
2021-08-20 02:44:09 +02:00
size.store(0);
delete read_ptr;
write_ptr = read_ptr = new ElementPtr();
2020-12-28 16:15:37 +01:00
}
private:
2021-08-20 02:44:09 +02:00
// stores a pointer to element
// and a pointer to the next ElementPtr
class ElementPtr {
public:
ElementPtr() {}
~ElementPtr() {
ElementPtr* next_ptr = next.load();
if (next_ptr)
delete next_ptr;
2020-12-28 16:15:37 +01:00
}
2021-08-20 02:44:09 +02:00
T current;
std::atomic<ElementPtr*> next{nullptr};
2020-12-28 16:15:37 +01:00
};
2021-08-20 02:44:09 +02:00
ElementPtr* write_ptr;
ElementPtr* read_ptr;
std::atomic_size_t size{0};
std::mutex cv_mutex;
2021-09-16 06:04:40 +02:00
std::conditional_t<with_stop_token, std::condition_variable_any, std::condition_variable> cv;
2021-08-16 13:42:12 +02:00
};
2020-12-28 16:15:37 +01:00
2021-08-20 02:44:09 +02:00
// a simple thread-safe,
// single reader, multiple writer queue
2021-09-16 06:04:40 +02:00
template <typename T, bool with_stop_token = false>
2021-08-20 02:44:09 +02:00
class MPSCQueue {
2020-12-28 16:15:37 +01:00
public:
2021-08-20 02:44:09 +02:00
[[nodiscard]] std::size_t Size() const {
return spsc_queue.Size();
2020-12-28 16:15:37 +01:00
}
2021-08-20 02:44:09 +02:00
[[nodiscard]] bool Empty() const {
return spsc_queue.Empty();
2020-12-28 16:15:37 +01:00
}
2021-08-20 02:44:09 +02:00
[[nodiscard]] T& Front() const {
return spsc_queue.Front();
2020-12-28 16:15:37 +01:00
}
2021-08-20 02:44:09 +02:00
template <typename Arg>
void Push(Arg&& t) {
std::lock_guard lock{write_lock};
spsc_queue.Push(t);
2020-12-28 16:15:37 +01:00
}
2021-08-20 02:44:09 +02:00
void Pop() {
return spsc_queue.Pop();
2020-12-28 16:15:37 +01:00
}
2021-08-20 02:44:09 +02:00
bool Pop(T& t) {
return spsc_queue.Pop(t);
2021-04-07 20:28:12 +02:00
}
2021-08-20 02:44:09 +02:00
void Wait() {
spsc_queue.Wait();
2020-12-28 16:15:37 +01:00
}
2021-08-16 13:42:12 +02:00
T PopWait() {
2021-08-20 02:44:09 +02:00
return spsc_queue.PopWait();
}
2021-09-16 06:04:40 +02:00
T PopWait(std::stop_token stop_token) {
return spsc_queue.PopWait(stop_token);
}
2021-08-20 02:44:09 +02:00
// not thread-safe
void Clear() {
spsc_queue.Clear();
2020-12-28 16:15:37 +01:00
}
private:
2021-09-16 06:04:40 +02:00
SPSCQueue<T, with_stop_token> spsc_queue;
2021-08-20 02:44:09 +02:00
std::mutex write_lock;
2020-12-28 16:15:37 +01:00
};
} // namespace Common