[video_core] reduce SPSC/MPSC queue contention for commands
All checks were successful
eden-license / license-header (pull_request) Successful in 20s

Signed-off-by: lizzie <lizzie@eden-emu.dev>
This commit is contained in:
lizzie 2025-08-30 08:22:03 +00:00 committed by crueter
parent c725641f13
commit 035482c1b7

View file

@ -1,3 +1,5 @@
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
@ -23,34 +25,34 @@ class SPSCQueue {
public: public:
template <typename... Args> template <typename... Args>
bool TryEmplace(Args&&... args) { bool TryEmplace(Args&&... args) noexcept {
return Emplace<PushMode::Try>(std::forward<Args>(args)...); return Emplace<PushMode::Try>(std::forward<Args>(args)...);
} }
template <typename... Args> template <typename... Args>
void EmplaceWait(Args&&... args) { void EmplaceWait(Args&&... args) noexcept {
Emplace<PushMode::Wait>(std::forward<Args>(args)...); Emplace<PushMode::Wait>(std::forward<Args>(args)...);
} }
bool TryPop(T& t) { bool TryPop(T& t) noexcept {
return Pop<PopMode::Try>(t); return Pop<PopMode::Try>(t);
} }
void PopWait(T& t) { void PopWait(T& t) noexcept {
Pop<PopMode::Wait>(t); Pop<PopMode::Wait>(t);
} }
void PopWait(T& t, std::stop_token stop_token) { void PopWait(T& t, const std::stop_token stop_token) noexcept {
Pop<PopMode::WaitWithStopToken>(t, stop_token); Pop<PopMode::WaitWithStopToken>(t, stop_token);
} }
T PopWait() { T PopWait() noexcept {
T t{}; T t{};
Pop<PopMode::Wait>(t); Pop<PopMode::Wait>(t);
return t; return t;
} }
T PopWait(std::stop_token stop_token) { T PopWait(const std::stop_token stop_token) noexcept {
T t{}; T t{};
Pop<PopMode::WaitWithStopToken>(t, stop_token); Pop<PopMode::WaitWithStopToken>(t, stop_token);
return t; return t;
@ -71,60 +73,53 @@ private:
}; };
template <PushMode Mode, typename... Args> template <PushMode Mode, typename... Args>
bool Emplace(Args&&... args) { bool Emplace(Args&&... args) noexcept {
const size_t write_index = m_write_index.load(std::memory_order::relaxed); const std::size_t write_index = producer.index.load(std::memory_order::relaxed);
if constexpr (Mode == PushMode::Try) { if constexpr (Mode == PushMode::Try) {
// Check if we have free slots to write to. // Check if we have free slots to write to.
if ((write_index - m_read_index.load(std::memory_order::acquire)) == Capacity) { if ((write_index - consumer.index.load(std::memory_order::acquire)) == Capacity) {
return false; return false;
} }
} else if constexpr (Mode == PushMode::Wait) { } else if constexpr (Mode == PushMode::Wait) {
// Wait until we have free slots to write to. // Wait until we have free slots to write to.
std::unique_lock lock{producer_cv_mutex}; std::unique_lock lock{producer.cv_mutex};
producer_cv.wait(lock, [this, write_index] { producer.cv.wait(lock, [this, write_index] {
return (write_index - m_read_index.load(std::memory_order::acquire)) < Capacity; return (write_index - consumer.index.load(std::memory_order::acquire)) < Capacity;
}); });
} else { } else {
static_assert(Mode < PushMode::Count, "Invalid PushMode."); static_assert(Mode < PushMode::Count, "Invalid PushMode.");
} }
// Determine the position to write to. // Determine the position to write to.
const size_t pos = write_index % Capacity; const std::size_t pos = write_index % Capacity;
// Emplace into the queue. // Emplace into the queue.
std::construct_at(std::addressof(m_data[pos]), std::forward<Args>(args)...); std::construct_at(std::addressof(m_data[pos]), std::forward<Args>(args)...);
// Increment the write index. // Increment the write index.
++m_write_index; ++producer.index;
// Notify the consumer that we have pushed into the queue. // Notify the consumer that we have pushed into the queue.
std::scoped_lock lock{consumer_cv_mutex}; std::scoped_lock lock{consumer.cv_mutex};
consumer_cv.notify_one(); consumer.cv.notify_one();
return true; return true;
} }
template <PopMode Mode> template <PopMode Mode>
bool Pop(T& t, [[maybe_unused]] std::stop_token stop_token = {}) { bool Pop(T& t, [[maybe_unused]] std::stop_token stop_token = {}) noexcept {
const size_t read_index = m_read_index.load(std::memory_order::relaxed); const std::size_t read_index = consumer.index.load(std::memory_order::relaxed);
if constexpr (Mode == PopMode::Try) { if constexpr (Mode == PopMode::Try) {
// Check if the queue is empty. // Check if the queue is empty.
if (read_index == m_write_index.load(std::memory_order::acquire)) { if (read_index == producer.index.load(std::memory_order::acquire)) {
return false; return false;
} }
} else if constexpr (Mode == PopMode::Wait) { } else if constexpr (Mode == PopMode::Wait) {
// Wait until the queue is not empty. // Wait until the queue is not empty.
std::unique_lock lock{consumer_cv_mutex}; std::unique_lock lock{consumer.cv_mutex};
consumer_cv.wait(lock, [this, read_index] { consumer.cv.wait(lock, [this, read_index] {
return read_index != m_write_index.load(std::memory_order::acquire); return read_index != producer.index.load(std::memory_order::acquire);
}); });
} else if constexpr (Mode == PopMode::WaitWithStopToken) { } else if constexpr (Mode == PopMode::WaitWithStopToken) {
// Wait until the queue is not empty. // Wait until the queue is not empty.
std::unique_lock lock{consumer_cv_mutex}; std::unique_lock lock{consumer.cv_mutex};
Common::CondvarWait(consumer_cv, lock, stop_token, [this, read_index] { Common::CondvarWait(consumer.cv, lock, stop_token, [this, read_index] {
return read_index != m_write_index.load(std::memory_order::acquire); return read_index != producer.index.load(std::memory_order::acquire);
}); });
if (stop_token.stop_requested()) { if (stop_token.stop_requested()) {
return false; return false;
@ -132,32 +127,29 @@ private:
} else { } else {
static_assert(Mode < PopMode::Count, "Invalid PopMode."); static_assert(Mode < PopMode::Count, "Invalid PopMode.");
} }
// Determine the position to read from. // Determine the position to read from.
const size_t pos = read_index % Capacity; const std::size_t pos = read_index % Capacity;
// Pop the data off the queue, moving it. // Pop the data off the queue, moving it.
t = std::move(m_data[pos]); t = std::move(m_data[pos]);
// Increment the read index. // Increment the read index.
++m_read_index; ++consumer.index;
// Notify the producer that we have popped off the queue. // Notify the producer that we have popped off the queue.
std::scoped_lock lock{producer_cv_mutex}; std::scoped_lock lock{producer.cv_mutex};
producer_cv.notify_one(); producer.cv.notify_one();
return true; return true;
} }
alignas(128) std::atomic_size_t m_read_index{0};
alignas(128) std::atomic_size_t m_write_index{0};
std::array<T, Capacity> m_data; std::array<T, Capacity> m_data;
alignas(64) struct {
std::condition_variable_any producer_cv; std::atomic_size_t index{0};
std::mutex producer_cv_mutex; std::condition_variable_any cv;
std::condition_variable_any consumer_cv; std::mutex cv_mutex;
std::mutex consumer_cv_mutex; } producer;
alignas(64) struct {
std::atomic_size_t index{0};
std::condition_variable_any cv;
std::mutex cv_mutex;
} consumer;
}; };
template <typename T, size_t Capacity = detail::DefaultCapacity> template <typename T, size_t Capacity = detail::DefaultCapacity>
@ -242,8 +234,8 @@ public:
private: private:
SPSCQueue<T, Capacity> spsc_queue; SPSCQueue<T, Capacity> spsc_queue;
std::mutex write_mutex; alignas(64) std::mutex write_mutex;
std::mutex read_mutex; alignas(64) std::mutex read_mutex;
}; };
} // namespace Common } // namespace Common