yuzu/src/common/threadsafe_queue.h

195 lines
4.5 KiB
C
Raw Normal View History

2022-04-29 09:46:31 +04:00
// SPDX-FileCopyrightText: 2010 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2020-12-28 19:15:37 +04:00
#pragma once
2021-08-20 04:44:09 +04:00
// a simple lockless thread-safe,
// single reader, single writer queue
2020-12-28 19:15:37 +04:00
#include <atomic>
#include <condition_variable>
2021-08-20 04:44:09 +04:00
#include <cstddef>
2020-12-28 19:15:37 +04:00
#include <mutex>
2021-08-20 04:44:09 +04:00
#include <utility>
2020-12-28 19:15:37 +04:00
namespace Common {
2021-09-16 08:04:40 +04:00
template <typename T, bool with_stop_token = false>
2021-08-20 04:44:09 +04:00
class SPSCQueue {
2020-12-28 19:15:37 +04:00
public:
2021-08-20 04:44:09 +04: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 19:15:37 +04:00
}
template <typename Arg>
void Push(Arg&& t) {
2021-08-20 04:44:09 +04: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.
2022-04-09 13:30:20 +04:00
std::scoped_lock lock{cv_mutex};
2021-08-20 04:44:09 +04:00
cv.notify_one();
2020-12-28 19:15:37 +04:00
}
2021-08-20 04:44:09 +04: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 22:28:12 +04:00
}
2021-08-20 04:44:09 +04: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 19:15:37 +04:00
}
2021-08-16 15:42:12 +04:00
void Wait() {
2021-08-20 04:44:09 +04:00
if (Empty()) {
std::unique_lock lock{cv_mutex};
2021-09-16 08:04:40 +04:00
cv.wait(lock, [this] { return !Empty(); });
2021-08-16 15:42:12 +04:00
}
}
2021-08-20 04:44:09 +04:00
T PopWait() {
Wait();
T t;
Pop(t);
return t;
}
2021-09-16 08:04:40 +04: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 04:44:09 +04:00
// not thread-safe
2020-12-28 19:15:37 +04:00
void Clear() {
2021-08-20 04:44:09 +04:00
size.store(0);
delete read_ptr;
write_ptr = read_ptr = new ElementPtr();
2020-12-28 19:15:37 +04:00
}
private:
2021-08-20 04:44:09 +04: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 19:15:37 +04:00
}
2021-08-20 04:44:09 +04:00
T current;
std::atomic<ElementPtr*> next{nullptr};
2020-12-28 19:15:37 +04:00
};
2021-08-20 04:44:09 +04:00
ElementPtr* write_ptr;
ElementPtr* read_ptr;
std::atomic_size_t size{0};
std::mutex cv_mutex;
2021-09-16 08:04:40 +04:00
std::conditional_t<with_stop_token, std::condition_variable_any, std::condition_variable> cv;
2021-08-16 15:42:12 +04:00
};
2020-12-28 19:15:37 +04:00
2021-08-20 04:44:09 +04:00
// a simple thread-safe,
// single reader, multiple writer queue
2021-09-16 08:04:40 +04:00
template <typename T, bool with_stop_token = false>
2021-08-20 04:44:09 +04:00
class MPSCQueue {
2020-12-28 19:15:37 +04:00
public:
2021-08-20 04:44:09 +04:00
[[nodiscard]] std::size_t Size() const {
return spsc_queue.Size();
2020-12-28 19:15:37 +04:00
}
2021-08-20 04:44:09 +04:00
[[nodiscard]] bool Empty() const {
return spsc_queue.Empty();
2020-12-28 19:15:37 +04:00
}
2021-08-20 04:44:09 +04:00
[[nodiscard]] T& Front() const {
return spsc_queue.Front();
2020-12-28 19:15:37 +04:00
}
2021-08-20 04:44:09 +04:00
template <typename Arg>
void Push(Arg&& t) {
2022-04-09 13:30:20 +04:00
std::scoped_lock lock{write_lock};
2021-08-20 04:44:09 +04:00
spsc_queue.Push(t);
2020-12-28 19:15:37 +04:00
}
2021-08-20 04:44:09 +04:00
void Pop() {
return spsc_queue.Pop();
2020-12-28 19:15:37 +04:00
}
2021-08-20 04:44:09 +04:00
bool Pop(T& t) {
return spsc_queue.Pop(t);
2021-04-07 22:28:12 +04:00
}
2021-08-20 04:44:09 +04:00
void Wait() {
spsc_queue.Wait();
2020-12-28 19:15:37 +04:00
}
2021-08-16 15:42:12 +04:00
T PopWait() {
2021-08-20 04:44:09 +04:00
return spsc_queue.PopWait();
}
2021-09-16 08:04:40 +04:00
T PopWait(std::stop_token stop_token) {
return spsc_queue.PopWait(stop_token);
}
2021-08-20 04:44:09 +04:00
// not thread-safe
void Clear() {
spsc_queue.Clear();
2020-12-28 19:15:37 +04:00
}
private:
2021-09-16 08:04:40 +04:00
SPSCQueue<T, with_stop_token> spsc_queue;
2021-08-20 04:44:09 +04:00
std::mutex write_lock;
2020-12-28 19:15:37 +04:00
};
} // namespace Common