diff options
| author | 2022-06-02 01:08:18 -0600 | |
|---|---|---|
| committer | 2022-06-02 19:37:46 -0600 | |
| commit | 4dd6bcd2063909359b08771465c3641876ddf138 (patch) | |
| tree | f7a52e974aae7f50131acf233613d8e9d43757d0 /src | |
| parent | Merge pull request #8409 from liamwhite/tdesc-fix (diff) | |
| download | yuzu-4dd6bcd2063909359b08771465c3641876ddf138.tar.gz yuzu-4dd6bcd2063909359b08771465c3641876ddf138.tar.xz yuzu-4dd6bcd2063909359b08771465c3641876ddf138.zip | |
gpu_thread: Move to bounded queue
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/bounded_threadsafe_queue.h | 180 | ||||
| -rw-r--r-- | src/video_core/gpu_thread.cpp | 3 | ||||
| -rw-r--r-- | src/video_core/gpu_thread.h | 6 |
3 files changed, 185 insertions, 4 deletions
diff --git a/src/common/bounded_threadsafe_queue.h b/src/common/bounded_threadsafe_queue.h new file mode 100644 index 000000000..e83064c7f --- /dev/null +++ b/src/common/bounded_threadsafe_queue.h | |||
| @@ -0,0 +1,180 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright (c) 2020 Erik Rigtorp <erik@rigtorp.se> | ||
| 2 | // SPDX-License-Identifier: MIT | ||
| 3 | #pragma once | ||
| 4 | #ifdef _MSC_VER | ||
| 5 | #pragma warning(push) | ||
| 6 | #pragma warning(disable : 4324) | ||
| 7 | #endif | ||
| 8 | |||
| 9 | #include <atomic> | ||
| 10 | #include <bit> | ||
| 11 | #include <condition_variable> | ||
| 12 | #include <memory> | ||
| 13 | #include <mutex> | ||
| 14 | #include <new> | ||
| 15 | #include <stdexcept> | ||
| 16 | #include <stop_token> | ||
| 17 | #include <type_traits> | ||
| 18 | #include <utility> | ||
| 19 | |||
| 20 | namespace Common { | ||
| 21 | namespace mpsc { | ||
| 22 | #if defined(__cpp_lib_hardware_interference_size) | ||
| 23 | constexpr size_t hardware_interference_size = std::hardware_destructive_interference_size; | ||
| 24 | #else | ||
| 25 | constexpr size_t hardware_interference_size = 64; | ||
| 26 | #endif | ||
| 27 | |||
| 28 | template <typename T> | ||
| 29 | using AlignedAllocator = std::allocator<T>; | ||
| 30 | |||
| 31 | template <typename T> | ||
| 32 | struct Slot { | ||
| 33 | ~Slot() noexcept { | ||
| 34 | if (turn.test()) { | ||
| 35 | destroy(); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | template <typename... Args> | ||
| 40 | void construct(Args&&... args) noexcept { | ||
| 41 | static_assert(std::is_nothrow_constructible_v<T, Args&&...>, | ||
| 42 | "T must be nothrow constructible with Args&&..."); | ||
| 43 | std::construct_at(reinterpret_cast<T*>(&storage), std::forward<Args>(args)...); | ||
| 44 | } | ||
| 45 | |||
| 46 | void destroy() noexcept { | ||
| 47 | static_assert(std::is_nothrow_destructible_v<T>, "T must be nothrow destructible"); | ||
| 48 | std::destroy_at(reinterpret_cast<T*>(&storage)); | ||
| 49 | } | ||
| 50 | |||
| 51 | T&& move() noexcept { | ||
| 52 | return reinterpret_cast<T&&>(storage); | ||
| 53 | } | ||
| 54 | |||
| 55 | // Align to avoid false sharing between adjacent slots | ||
| 56 | alignas(hardware_interference_size) std::atomic_flag turn{}; | ||
| 57 | struct aligned_store { | ||
| 58 | struct type { | ||
| 59 | alignas(T) unsigned char data[sizeof(T)]; | ||
| 60 | }; | ||
| 61 | }; | ||
| 62 | typename aligned_store::type storage; | ||
| 63 | }; | ||
| 64 | |||
| 65 | template <typename T, typename Allocator = AlignedAllocator<Slot<T>>> | ||
| 66 | class Queue { | ||
| 67 | public: | ||
| 68 | explicit Queue(const size_t capacity, const Allocator& allocator = Allocator()) | ||
| 69 | : allocator_(allocator) { | ||
| 70 | if (capacity < 1) { | ||
| 71 | throw std::invalid_argument("capacity < 1"); | ||
| 72 | } | ||
| 73 | // Ensure that the queue length is an integer power of 2 | ||
| 74 | // This is so that idx(i) can be a simple i & mask_ insted of i % capacity | ||
| 75 | // https://github.com/rigtorp/MPMCQueue/pull/36 | ||
| 76 | if (!std::has_single_bit(capacity)) { | ||
| 77 | throw std::invalid_argument("capacity must be an integer power of 2"); | ||
| 78 | } | ||
| 79 | |||
| 80 | mask_ = capacity - 1; | ||
| 81 | |||
| 82 | // Allocate one extra slot to prevent false sharing on the last slot | ||
| 83 | slots_ = allocator_.allocate(mask_ + 2); | ||
| 84 | // Allocators are not required to honor alignment for over-aligned types | ||
| 85 | // (see http://eel.is/c++draft/allocator.requirements#10) so we verify | ||
| 86 | // alignment here | ||
| 87 | if (reinterpret_cast<uintptr_t>(slots_) % alignof(Slot<T>) != 0) { | ||
| 88 | allocator_.deallocate(slots_, mask_ + 2); | ||
| 89 | throw std::bad_alloc(); | ||
| 90 | } | ||
| 91 | for (size_t i = 0; i < mask_ + 1; ++i) { | ||
| 92 | std::construct_at(&slots_[i]); | ||
| 93 | } | ||
| 94 | static_assert(alignof(Slot<T>) == hardware_interference_size, | ||
| 95 | "Slot must be aligned to cache line boundary to prevent false sharing"); | ||
| 96 | static_assert(sizeof(Slot<T>) % hardware_interference_size == 0, | ||
| 97 | "Slot size must be a multiple of cache line size to prevent " | ||
| 98 | "false sharing between adjacent slots"); | ||
| 99 | static_assert(sizeof(Queue) % hardware_interference_size == 0, | ||
| 100 | "Queue size must be a multiple of cache line size to " | ||
| 101 | "prevent false sharing between adjacent queues"); | ||
| 102 | } | ||
| 103 | |||
| 104 | ~Queue() noexcept { | ||
| 105 | for (size_t i = 0; i < mask_ + 1; ++i) { | ||
| 106 | slots_[i].~Slot(); | ||
| 107 | } | ||
| 108 | allocator_.deallocate(slots_, mask_ + 2); | ||
| 109 | } | ||
| 110 | |||
| 111 | // non-copyable and non-movable | ||
| 112 | Queue(const Queue&) = delete; | ||
| 113 | Queue& operator=(const Queue&) = delete; | ||
| 114 | |||
| 115 | void Push(const T& v) noexcept { | ||
| 116 | static_assert(std::is_nothrow_copy_constructible_v<T>, | ||
| 117 | "T must be nothrow copy constructible"); | ||
| 118 | emplace(v); | ||
| 119 | } | ||
| 120 | |||
| 121 | template <typename P, typename = std::enable_if_t<std::is_nothrow_constructible_v<T, P&&>>> | ||
| 122 | void Push(P&& v) noexcept { | ||
| 123 | emplace(std::forward<P>(v)); | ||
| 124 | } | ||
| 125 | |||
| 126 | void Pop(T& v, std::stop_token stop) noexcept { | ||
| 127 | auto const tail = tail_.fetch_add(1); | ||
| 128 | auto& slot = slots_[idx(tail)]; | ||
| 129 | if (false == slot.turn.test()) { | ||
| 130 | std::unique_lock lock{cv_mutex}; | ||
| 131 | cv.wait(lock, stop, [&slot] { return slot.turn.test(); }); | ||
| 132 | } | ||
| 133 | v = slot.move(); | ||
| 134 | slot.destroy(); | ||
| 135 | slot.turn.clear(); | ||
| 136 | slot.turn.notify_one(); | ||
| 137 | } | ||
| 138 | |||
| 139 | private: | ||
| 140 | template <typename... Args> | ||
| 141 | void emplace(Args&&... args) noexcept { | ||
| 142 | static_assert(std::is_nothrow_constructible_v<T, Args&&...>, | ||
| 143 | "T must be nothrow constructible with Args&&..."); | ||
| 144 | auto const head = head_.fetch_add(1); | ||
| 145 | auto& slot = slots_[idx(head)]; | ||
| 146 | slot.turn.wait(true); | ||
| 147 | slot.construct(std::forward<Args>(args)...); | ||
| 148 | slot.turn.test_and_set(); | ||
| 149 | cv.notify_one(); | ||
| 150 | } | ||
| 151 | |||
| 152 | constexpr size_t idx(size_t i) const noexcept { | ||
| 153 | return i & mask_; | ||
| 154 | } | ||
| 155 | |||
| 156 | std::conditional_t<true, std::condition_variable_any, std::condition_variable> cv; | ||
| 157 | std::mutex cv_mutex; | ||
| 158 | size_t mask_; | ||
| 159 | Slot<T>* slots_; | ||
| 160 | [[no_unique_address]] Allocator allocator_; | ||
| 161 | |||
| 162 | // Align to avoid false sharing between head_ and tail_ | ||
| 163 | alignas(hardware_interference_size) std::atomic<size_t> head_{0}; | ||
| 164 | alignas(hardware_interference_size) std::atomic<size_t> tail_{0}; | ||
| 165 | |||
| 166 | static_assert(std::is_nothrow_copy_assignable_v<T> || std::is_nothrow_move_assignable_v<T>, | ||
| 167 | "T must be nothrow copy or move assignable"); | ||
| 168 | |||
| 169 | static_assert(std::is_nothrow_destructible_v<T>, "T must be nothrow destructible"); | ||
| 170 | }; | ||
| 171 | } // namespace mpsc | ||
| 172 | |||
| 173 | template <typename T, typename Allocator = mpsc::AlignedAllocator<mpsc::Slot<T>>> | ||
| 174 | using MPSCQueue = mpsc::Queue<T, Allocator>; | ||
| 175 | |||
| 176 | } // namespace Common | ||
| 177 | |||
| 178 | #ifdef _MSC_VER | ||
| 179 | #pragma warning(pop) | ||
| 180 | #endif | ||
diff --git a/src/video_core/gpu_thread.cpp b/src/video_core/gpu_thread.cpp index b79a73132..8479dc6d2 100644 --- a/src/video_core/gpu_thread.cpp +++ b/src/video_core/gpu_thread.cpp | |||
| @@ -31,7 +31,8 @@ static void RunThread(std::stop_token stop_token, Core::System& system, | |||
| 31 | VideoCore::RasterizerInterface* const rasterizer = renderer.ReadRasterizer(); | 31 | VideoCore::RasterizerInterface* const rasterizer = renderer.ReadRasterizer(); |
| 32 | 32 | ||
| 33 | while (!stop_token.stop_requested()) { | 33 | while (!stop_token.stop_requested()) { |
| 34 | CommandDataContainer next = state.queue.PopWait(stop_token); | 34 | CommandDataContainer next; |
| 35 | state.queue.Pop(next, stop_token); | ||
| 35 | if (stop_token.stop_requested()) { | 36 | if (stop_token.stop_requested()) { |
| 36 | break; | 37 | break; |
| 37 | } | 38 | } |
diff --git a/src/video_core/gpu_thread.h b/src/video_core/gpu_thread.h index 71cd35756..ad9fd5eff 100644 --- a/src/video_core/gpu_thread.h +++ b/src/video_core/gpu_thread.h | |||
| @@ -10,7 +10,7 @@ | |||
| 10 | #include <thread> | 10 | #include <thread> |
| 11 | #include <variant> | 11 | #include <variant> |
| 12 | 12 | ||
| 13 | #include "common/threadsafe_queue.h" | 13 | #include "common/bounded_threadsafe_queue.h" |
| 14 | #include "video_core/framebuffer_config.h" | 14 | #include "video_core/framebuffer_config.h" |
| 15 | 15 | ||
| 16 | namespace Tegra { | 16 | namespace Tegra { |
| @@ -96,9 +96,9 @@ struct CommandDataContainer { | |||
| 96 | 96 | ||
| 97 | /// Struct used to synchronize the GPU thread | 97 | /// Struct used to synchronize the GPU thread |
| 98 | struct SynchState final { | 98 | struct SynchState final { |
| 99 | using CommandQueue = Common::SPSCQueue<CommandDataContainer, true>; | 99 | using CommandQueue = Common::MPSCQueue<CommandDataContainer>; |
| 100 | std::mutex write_lock; | 100 | std::mutex write_lock; |
| 101 | CommandQueue queue; | 101 | CommandQueue queue{512}; // size must be 2^n |
| 102 | u64 last_fence{}; | 102 | u64 last_fence{}; |
| 103 | std::atomic<u64> signaled_fence{}; | 103 | std::atomic<u64> signaled_fence{}; |
| 104 | std::condition_variable_any cv; | 104 | std::condition_variable_any cv; |