2022-04-28 12:24:11 -04:00
|
|
|
// SPDX-FileCopyrightText: 2010 Dolphin Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-11-25 08:56:57 -05:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// a simple lockless thread-safe,
|
|
|
|
// single reader, single writer queue
|
|
|
|
|
|
|
|
#include <atomic>
|
2018-09-09 07:08:57 -04:00
|
|
|
#include <condition_variable>
|
2017-11-25 08:56:57 -05:00
|
|
|
#include <cstddef>
|
|
|
|
#include <mutex>
|
2019-02-12 22:12:23 -05:00
|
|
|
#include <utility>
|
2017-11-25 08:56:57 -05:00
|
|
|
|
2022-11-21 11:31:18 -05:00
|
|
|
#include "common/polyfill_thread.h"
|
|
|
|
|
2017-11-25 08:56:57 -05:00
|
|
|
namespace Common {
|
2021-09-15 20:18:36 -04:00
|
|
|
template <typename T, bool with_stop_token = false>
|
2017-11-25 08:56:57 -05:00
|
|
|
class SPSCQueue {
|
|
|
|
public:
|
2019-02-12 22:03:48 -05:00
|
|
|
SPSCQueue() {
|
2017-11-25 08:56:57 -05:00
|
|
|
write_ptr = read_ptr = new ElementPtr();
|
|
|
|
}
|
|
|
|
~SPSCQueue() {
|
|
|
|
// this will empty out the whole queue
|
|
|
|
delete read_ptr;
|
|
|
|
}
|
|
|
|
|
2020-08-14 09:38:45 -04:00
|
|
|
[[nodiscard]] std::size_t Size() const {
|
2017-11-25 08:56:57 -05:00
|
|
|
return size.load();
|
|
|
|
}
|
|
|
|
|
2020-08-14 09:38:45 -04:00
|
|
|
[[nodiscard]] bool Empty() const {
|
2019-02-12 22:03:48 -05:00
|
|
|
return Size() == 0;
|
2017-11-25 08:56:57 -05:00
|
|
|
}
|
2018-07-21 10:16:21 -04:00
|
|
|
|
2020-08-14 09:38:45 -04:00
|
|
|
[[nodiscard]] T& Front() const {
|
2017-11-25 08:56:57 -05:00
|
|
|
return read_ptr->current;
|
|
|
|
}
|
2018-07-21 10:16:21 -04:00
|
|
|
|
2017-11-25 08:56:57 -05:00
|
|
|
template <typename Arg>
|
|
|
|
void Push(Arg&& t) {
|
|
|
|
// create the element, add it to the queue
|
2022-07-28 19:47:39 -04:00
|
|
|
write_ptr->current = std::move(t);
|
2017-11-25 08:56:57 -05:00
|
|
|
// 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;
|
2021-08-13 15:22:51 -04:00
|
|
|
++size;
|
2019-02-12 22:03:48 -05:00
|
|
|
|
2021-08-13 15:22:51 -04:00
|
|
|
// cv_mutex must be held or else there will be a missed wakeup if the other thread is in the
|
|
|
|
// line before cv.wait
|
2019-12-16 20:39:53 -05:00
|
|
|
// 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-07 14:30:55 -04:00
|
|
|
std::scoped_lock lock{cv_mutex};
|
2019-12-16 20:39:53 -05:00
|
|
|
cv.notify_one();
|
2017-11-25 08:56:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Pop() {
|
2019-02-12 22:03:48 -05:00
|
|
|
--size;
|
|
|
|
|
2017-11-25 08:56:57 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Pop(T& t) {
|
|
|
|
if (Empty())
|
|
|
|
return false;
|
|
|
|
|
2019-02-12 22:03:48 -05:00
|
|
|
--size;
|
2017-11-25 08:56:57 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-04-06 14:30:22 -04:00
|
|
|
void Wait() {
|
2018-09-09 07:08:57 -04:00
|
|
|
if (Empty()) {
|
2019-04-01 12:29:59 -04:00
|
|
|
std::unique_lock lock{cv_mutex};
|
2021-09-15 20:18:36 -04:00
|
|
|
cv.wait(lock, [this] { return !Empty(); });
|
2018-09-09 07:08:57 -04:00
|
|
|
}
|
2021-04-06 14:30:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
T PopWait() {
|
|
|
|
Wait();
|
2018-10-08 17:28:54 -04:00
|
|
|
T t;
|
|
|
|
Pop(t);
|
|
|
|
return t;
|
2018-09-09 07:08:57 -04:00
|
|
|
}
|
|
|
|
|
2021-09-15 20:18:36 -04:00
|
|
|
T PopWait(std::stop_token stop_token) {
|
|
|
|
if (Empty()) {
|
|
|
|
std::unique_lock lock{cv_mutex};
|
2022-11-21 11:31:18 -05:00
|
|
|
Common::CondvarWait(cv, lock, stop_token, [this] { return !Empty(); });
|
2021-09-15 20:18:36 -04:00
|
|
|
}
|
|
|
|
if (stop_token.stop_requested()) {
|
|
|
|
return T{};
|
|
|
|
}
|
|
|
|
T t;
|
|
|
|
Pop(t);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2017-11-25 08:56:57 -05:00
|
|
|
// not thread-safe
|
|
|
|
void Clear() {
|
|
|
|
size.store(0);
|
|
|
|
delete read_ptr;
|
|
|
|
write_ptr = read_ptr = new ElementPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// stores a pointer to element
|
|
|
|
// and a pointer to the next ElementPtr
|
|
|
|
class ElementPtr {
|
|
|
|
public:
|
2019-02-12 22:12:23 -05:00
|
|
|
ElementPtr() {}
|
2017-11-25 08:56:57 -05:00
|
|
|
~ElementPtr() {
|
|
|
|
ElementPtr* next_ptr = next.load();
|
|
|
|
|
|
|
|
if (next_ptr)
|
|
|
|
delete next_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
T current;
|
2019-02-12 22:12:23 -05:00
|
|
|
std::atomic<ElementPtr*> next{nullptr};
|
2017-11-25 08:56:57 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
ElementPtr* write_ptr;
|
|
|
|
ElementPtr* read_ptr;
|
2019-02-12 22:12:23 -05:00
|
|
|
std::atomic_size_t size{0};
|
2018-09-09 07:08:57 -04:00
|
|
|
std::mutex cv_mutex;
|
2021-09-15 20:18:36 -04:00
|
|
|
std::conditional_t<with_stop_token, std::condition_variable_any, std::condition_variable> cv;
|
2017-11-25 08:56:57 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// a simple thread-safe,
|
|
|
|
// single reader, multiple writer queue
|
|
|
|
|
2021-09-15 20:18:36 -04:00
|
|
|
template <typename T, bool with_stop_token = false>
|
2018-07-21 10:16:21 -04:00
|
|
|
class MPSCQueue {
|
2017-11-25 08:56:57 -05:00
|
|
|
public:
|
2020-08-14 09:38:45 -04:00
|
|
|
[[nodiscard]] std::size_t Size() const {
|
2018-07-21 10:16:21 -04:00
|
|
|
return spsc_queue.Size();
|
|
|
|
}
|
|
|
|
|
2020-08-14 09:38:45 -04:00
|
|
|
[[nodiscard]] bool Empty() const {
|
2018-07-21 10:16:21 -04:00
|
|
|
return spsc_queue.Empty();
|
|
|
|
}
|
|
|
|
|
2020-08-14 09:38:45 -04:00
|
|
|
[[nodiscard]] T& Front() const {
|
2018-07-21 10:16:21 -04:00
|
|
|
return spsc_queue.Front();
|
|
|
|
}
|
|
|
|
|
2017-11-25 08:56:57 -05:00
|
|
|
template <typename Arg>
|
|
|
|
void Push(Arg&& t) {
|
2022-04-07 14:30:55 -04:00
|
|
|
std::scoped_lock lock{write_lock};
|
2018-07-21 10:16:21 -04:00
|
|
|
spsc_queue.Push(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Pop() {
|
|
|
|
return spsc_queue.Pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Pop(T& t) {
|
|
|
|
return spsc_queue.Pop(t);
|
|
|
|
}
|
|
|
|
|
2021-04-06 14:30:22 -04:00
|
|
|
void Wait() {
|
|
|
|
spsc_queue.Wait();
|
|
|
|
}
|
|
|
|
|
2018-10-08 17:28:54 -04:00
|
|
|
T PopWait() {
|
|
|
|
return spsc_queue.PopWait();
|
2018-09-09 07:08:57 -04:00
|
|
|
}
|
|
|
|
|
2021-09-15 20:18:36 -04:00
|
|
|
T PopWait(std::stop_token stop_token) {
|
|
|
|
return spsc_queue.PopWait(stop_token);
|
|
|
|
}
|
|
|
|
|
2018-07-21 10:16:21 -04:00
|
|
|
// not thread-safe
|
|
|
|
void Clear() {
|
|
|
|
spsc_queue.Clear();
|
2017-11-25 08:56:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-09-15 20:18:36 -04:00
|
|
|
SPSCQueue<T, with_stop_token> spsc_queue;
|
2017-11-25 08:56:57 -05:00
|
|
|
std::mutex write_lock;
|
|
|
|
};
|
|
|
|
} // namespace Common
|