pineapple/src/common/bounded_threadsafe_queue.h

324 lines
8.2 KiB
C
Raw Normal View History

2023-03-19 11:35:25 +01:00
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2022-11-05 13:58:44 +01:00
#pragma once
#include <atomic>
#include <condition_variable>
2023-03-19 11:35:25 +01:00
#include <cstddef>
2022-11-05 13:58:44 +01:00
#include <mutex>
#include <new>
2023-03-19 11:35:25 +01:00
#include <version>
2022-11-05 13:58:44 +01:00
2023-03-18 19:22:01 +01:00
#include "common/polyfill_thread.h"
2022-11-05 13:58:44 +01:00
namespace Common {
2023-03-19 11:35:25 +01:00
namespace detail {
constexpr size_t DefaultCapacity = 0x1000;
} // namespace detail
template <typename T, size_t Capacity = detail::DefaultCapacity>
class SPSCQueue {
static_assert((Capacity & (Capacity - 1)) == 0, "Capacity must be a power of two.");
2022-11-05 13:58:44 +01:00
public:
2023-03-19 11:35:25 +01:00
bool TryPush(T&& t) {
const size_t write_index = m_write_index.load();
// Check if we have free slots to write to.
if ((write_index - m_read_index.load()) == Capacity) {
return false;
2022-11-05 13:58:44 +01:00
}
2023-03-19 11:35:25 +01:00
// Determine the position to write to.
const size_t pos = write_index % Capacity;
// Push into the queue.
m_data[pos] = std::move(t);
// Increment the write index.
++m_write_index;
// Notify the consumer that we have pushed into the queue.
std::scoped_lock lock{cv_mutex};
cv.notify_one();
return true;
}
template <typename... Args>
bool TryPush(Args&&... args) {
const size_t write_index = m_write_index.load();
// Check if we have free slots to write to.
if ((write_index - m_read_index.load()) == Capacity) {
return false;
2022-11-05 13:58:44 +01:00
}
2023-03-19 11:35:25 +01:00
// Determine the position to write to.
const size_t pos = write_index % Capacity;
// Emplace into the queue.
std::construct_at(std::addressof(m_data[pos]), std::forward<Args>(args)...);
// Increment the write index.
++m_write_index;
// Notify the consumer that we have pushed into the queue.
std::scoped_lock lock{cv_mutex};
cv.notify_one();
return true;
}
void Push(T&& t) {
const size_t write_index = m_write_index.load();
// Wait until we have free slots to write to.
while ((write_index - m_read_index.load()) == Capacity) {
std::this_thread::yield();
2022-11-05 13:58:44 +01:00
}
2023-03-19 11:35:25 +01:00
// Determine the position to write to.
const size_t pos = write_index % Capacity;
// Push into the queue.
m_data[pos] = std::move(t);
// Increment the write index.
++m_write_index;
// Notify the consumer that we have pushed into the queue.
std::scoped_lock lock{cv_mutex};
cv.notify_one();
2022-11-05 13:58:44 +01:00
}
2023-03-19 11:35:25 +01:00
template <typename... Args>
void Push(Args&&... args) {
const size_t write_index = m_write_index.load();
// Wait until we have free slots to write to.
while ((write_index - m_read_index.load()) == Capacity) {
std::this_thread::yield();
}
// Determine the position to write to.
const size_t pos = write_index % Capacity;
// Emplace into the queue.
std::construct_at(std::addressof(m_data[pos]), std::forward<Args>(args)...);
2022-11-05 13:58:44 +01:00
2023-03-19 11:35:25 +01:00
// Increment the write index.
++m_write_index;
2022-11-05 13:58:44 +01:00
2023-03-19 11:35:25 +01:00
// Notify the consumer that we have pushed into the queue.
std::scoped_lock lock{cv_mutex};
cv.notify_one();
}
bool TryPop(T& t) {
return Pop(t);
2022-11-05 13:58:44 +01:00
}
2023-03-19 11:35:25 +01:00
void PopWait(T& t, std::stop_token stop_token) {
Wait(stop_token);
Pop(t);
2022-11-05 13:58:44 +01:00
}
2023-03-19 11:35:25 +01:00
T PopWait(std::stop_token stop_token) {
Wait(stop_token);
T t;
Pop(t);
return t;
}
void Clear() {
while (!Empty()) {
Pop();
2022-11-05 13:58:44 +01:00
}
2023-03-19 11:35:25 +01:00
}
bool Empty() const {
return m_read_index.load() == m_write_index.load();
}
size_t Size() const {
return m_write_index.load() - m_read_index.load();
2022-11-05 13:58:44 +01:00
}
private:
2023-03-19 11:35:25 +01:00
void Pop() {
const size_t read_index = m_read_index.load();
2022-11-05 13:58:44 +01:00
2023-03-19 11:35:25 +01:00
// Check if the queue is empty.
if (read_index == m_write_index.load()) {
return;
2022-11-05 13:58:44 +01:00
}
2023-03-19 11:35:25 +01:00
// Determine the position to read from.
const size_t pos = read_index % Capacity;
// Pop the data off the queue, deleting it.
std::destroy_at(std::addressof(m_data[pos]));
// Increment the read index.
++m_read_index;
}
2022-11-05 13:58:44 +01:00
2023-03-19 11:35:25 +01:00
bool Pop(T& t) {
const size_t read_index = m_read_index.load();
// Check if the queue is empty.
if (read_index == m_write_index.load()) {
return false;
2022-11-05 13:58:44 +01:00
}
2023-03-19 11:35:25 +01:00
// Determine the position to read from.
const size_t pos = read_index % Capacity;
2022-11-05 13:58:44 +01:00
2023-03-19 11:35:25 +01:00
// Pop the data off the queue, moving it.
t = std::move(m_data[pos]);
// Increment the read index.
++m_read_index;
return true;
2022-11-05 13:58:44 +01:00
}
2023-03-19 11:35:25 +01:00
void Wait(std::stop_token stop_token) {
std::unique_lock lock{cv_mutex};
Common::CondvarWait(cv, lock, stop_token, [this] { return !Empty(); });
2022-11-05 13:58:44 +01:00
}
2023-03-19 11:35:25 +01:00
#ifdef __cpp_lib_hardware_interference_size
alignas(std::hardware_destructive_interference_size) std::atomic_size_t m_read_index{0};
alignas(std::hardware_destructive_interference_size) std::atomic_size_t m_write_index{0};
#else
alignas(64) std::atomic_size_t m_read_index{0};
alignas(64) std::atomic_size_t m_write_index{0};
#endif
2022-11-05 13:58:44 +01:00
2023-03-19 11:35:25 +01:00
std::array<T, Capacity> m_data;
2022-11-05 13:58:44 +01:00
std::condition_variable_any cv;
2023-03-19 11:35:25 +01:00
std::mutex cv_mutex;
};
template <typename T, size_t Capacity = detail::DefaultCapacity>
class MPSCQueue {
public:
void TryPush(T&& t) {
std::scoped_lock lock{write_mutex};
spsc_queue.TryPush(std::move(t));
}
2022-11-05 13:58:44 +01:00
2023-03-19 11:35:25 +01:00
template <typename... Args>
void TryPush(Args&&... args) {
std::scoped_lock lock{write_mutex};
spsc_queue.TryPush(std::forward<Args>(args)...);
}
2022-11-05 13:58:44 +01:00
2023-03-19 11:35:25 +01:00
void Push(T&& t) {
std::scoped_lock lock{write_mutex};
spsc_queue.Push(std::move(t));
}
template <typename... Args>
void Push(Args&&... args) {
std::scoped_lock lock{write_mutex};
spsc_queue.Push(std::forward<Args>(args)...);
}
2022-11-05 13:58:44 +01:00
2023-03-19 11:35:25 +01:00
bool TryPop(T& t) {
return spsc_queue.TryPop(t);
}
void PopWait(T& t, std::stop_token stop_token) {
spsc_queue.PopWait(t, stop_token);
}
T PopWait(std::stop_token stop_token) {
return spsc_queue.PopWait(stop_token);
}
void Clear() {
spsc_queue.Clear();
}
bool Empty() {
return spsc_queue.Empty();
}
size_t Size() {
return spsc_queue.Size();
}
private:
SPSCQueue<T, Capacity> spsc_queue;
std::mutex write_mutex;
};
template <typename T, size_t Capacity = detail::DefaultCapacity>
class MPMCQueue {
public:
void TryPush(T&& t) {
std::scoped_lock lock{write_mutex};
spsc_queue.TryPush(std::move(t));
}
template <typename... Args>
void TryPush(Args&&... args) {
std::scoped_lock lock{write_mutex};
spsc_queue.TryPush(std::forward<Args>(args)...);
}
void Push(T&& t) {
std::scoped_lock lock{write_mutex};
spsc_queue.Push(std::move(t));
}
template <typename... Args>
void Push(Args&&... args) {
std::scoped_lock lock{write_mutex};
spsc_queue.Push(std::forward<Args>(args)...);
}
bool TryPop(T& t) {
std::scoped_lock lock{read_mutex};
return spsc_queue.TryPop(t);
}
void PopWait(T& t, std::stop_token stop_token) {
std::scoped_lock lock{read_mutex};
spsc_queue.PopWait(t, stop_token);
}
T PopWait(std::stop_token stop_token) {
std::scoped_lock lock{read_mutex};
return spsc_queue.PopWait(stop_token);
}
void Clear() {
std::scoped_lock lock{read_mutex};
spsc_queue.Clear();
}
bool Empty() {
std::scoped_lock lock{read_mutex};
return spsc_queue.Empty();
}
size_t Size() {
std::scoped_lock lock{read_mutex};
return spsc_queue.Size();
}
private:
SPSCQueue<T, Capacity> spsc_queue;
std::mutex write_mutex;
std::mutex read_mutex;
2022-11-05 13:58:44 +01:00
};
} // namespace Common