diff options
| author | 2022-06-14 08:57:19 -0400 | |
|---|---|---|
| committer | 2022-06-15 16:59:13 -0400 | |
| commit | 25429998e373c12287ae8da2a1c9c1bbe7bd7047 (patch) | |
| tree | 0f3e3afac950b0bd5ada9f5a143a38a1ea530b84 /src/common/bounded_threadsafe_queue.h | |
| parent | Merge pull request #8458 from lat9nq/no-constexpr-flow-block (diff) | |
| download | yuzu-25429998e373c12287ae8da2a1c9c1bbe7bd7047.tar.gz yuzu-25429998e373c12287ae8da2a1c9c1bbe7bd7047.tar.xz yuzu-25429998e373c12287ae8da2a1c9c1bbe7bd7047.zip | |
bounded_threadsafe_queue: Use constexpr capacity and mask
While this is the primary change, we also:
- Remove the mpsc namespace and rename Queue to MPSCQueue
- Make Slot a private struct within MPSCQueue
- Remove the AlignedAllocator template argument, as we use std::allocator
- Replace instances of mask + 1 with capacity, and mask + 2 with capacity + 1
Diffstat (limited to 'src/common/bounded_threadsafe_queue.h')
| -rw-r--r-- | src/common/bounded_threadsafe_queue.h | 159 |
1 files changed, 73 insertions, 86 deletions
diff --git a/src/common/bounded_threadsafe_queue.h b/src/common/bounded_threadsafe_queue.h index e83064c7f..7e465549b 100644 --- a/src/common/bounded_threadsafe_queue.h +++ b/src/common/bounded_threadsafe_queue.h | |||
| @@ -1,10 +1,7 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright (c) 2020 Erik Rigtorp <erik@rigtorp.se> | 1 | // SPDX-FileCopyrightText: Copyright (c) 2020 Erik Rigtorp <erik@rigtorp.se> |
| 2 | // SPDX-License-Identifier: MIT | 2 | // SPDX-License-Identifier: MIT |
| 3 | |||
| 3 | #pragma once | 4 | #pragma once |
| 4 | #ifdef _MSC_VER | ||
| 5 | #pragma warning(push) | ||
| 6 | #pragma warning(disable : 4324) | ||
| 7 | #endif | ||
| 8 | 5 | ||
| 9 | #include <atomic> | 6 | #include <atomic> |
| 10 | #include <bit> | 7 | #include <bit> |
| @@ -12,105 +9,63 @@ | |||
| 12 | #include <memory> | 9 | #include <memory> |
| 13 | #include <mutex> | 10 | #include <mutex> |
| 14 | #include <new> | 11 | #include <new> |
| 15 | #include <stdexcept> | ||
| 16 | #include <stop_token> | 12 | #include <stop_token> |
| 17 | #include <type_traits> | 13 | #include <type_traits> |
| 18 | #include <utility> | 14 | #include <utility> |
| 19 | 15 | ||
| 20 | namespace Common { | 16 | namespace Common { |
| 21 | namespace mpsc { | 17 | |
| 22 | #if defined(__cpp_lib_hardware_interference_size) | 18 | #if defined(__cpp_lib_hardware_interference_size) |
| 23 | constexpr size_t hardware_interference_size = std::hardware_destructive_interference_size; | 19 | constexpr size_t hardware_interference_size = std::hardware_destructive_interference_size; |
| 24 | #else | 20 | #else |
| 25 | constexpr size_t hardware_interference_size = 64; | 21 | constexpr size_t hardware_interference_size = 64; |
| 26 | #endif | 22 | #endif |
| 27 | 23 | ||
| 28 | template <typename T> | 24 | #ifdef _MSC_VER |
| 29 | using AlignedAllocator = std::allocator<T>; | 25 | #pragma warning(push) |
| 30 | 26 | #pragma warning(disable : 4324) | |
| 31 | template <typename T> | 27 | #endif |
| 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 | 28 | ||
| 65 | template <typename T, typename Allocator = AlignedAllocator<Slot<T>>> | 29 | template <typename T, size_t capacity = 0x400> |
| 66 | class Queue { | 30 | class MPSCQueue { |
| 67 | public: | 31 | public: |
| 68 | explicit Queue(const size_t capacity, const Allocator& allocator = Allocator()) | 32 | explicit MPSCQueue() : allocator{std::allocator<Slot<T>>()} { |
| 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 | 33 | // Allocate one extra slot to prevent false sharing on the last slot |
| 83 | slots_ = allocator_.allocate(mask_ + 2); | 34 | slots = allocator.allocate(capacity + 1); |
| 84 | // Allocators are not required to honor alignment for over-aligned types | 35 | // Allocators are not required to honor alignment for over-aligned types |
| 85 | // (see http://eel.is/c++draft/allocator.requirements#10) so we verify | 36 | // (see http://eel.is/c++draft/allocator.requirements#10) so we verify |
| 86 | // alignment here | 37 | // alignment here |
| 87 | if (reinterpret_cast<uintptr_t>(slots_) % alignof(Slot<T>) != 0) { | 38 | if (reinterpret_cast<uintptr_t>(slots) % alignof(Slot<T>) != 0) { |
| 88 | allocator_.deallocate(slots_, mask_ + 2); | 39 | allocator.deallocate(slots, capacity + 1); |
| 89 | throw std::bad_alloc(); | 40 | throw std::bad_alloc(); |
| 90 | } | 41 | } |
| 91 | for (size_t i = 0; i < mask_ + 1; ++i) { | 42 | for (size_t i = 0; i < capacity; ++i) { |
| 92 | std::construct_at(&slots_[i]); | 43 | std::construct_at(&slots[i]); |
| 93 | } | 44 | } |
| 45 | static_assert(std::has_single_bit(capacity), "capacity must be an integer power of 2"); | ||
| 94 | static_assert(alignof(Slot<T>) == hardware_interference_size, | 46 | static_assert(alignof(Slot<T>) == hardware_interference_size, |
| 95 | "Slot must be aligned to cache line boundary to prevent false sharing"); | 47 | "Slot must be aligned to cache line boundary to prevent false sharing"); |
| 96 | static_assert(sizeof(Slot<T>) % hardware_interference_size == 0, | 48 | static_assert(sizeof(Slot<T>) % hardware_interference_size == 0, |
| 97 | "Slot size must be a multiple of cache line size to prevent " | 49 | "Slot size must be a multiple of cache line size to prevent " |
| 98 | "false sharing between adjacent slots"); | 50 | "false sharing between adjacent slots"); |
| 99 | static_assert(sizeof(Queue) % hardware_interference_size == 0, | 51 | static_assert(sizeof(MPSCQueue) % hardware_interference_size == 0, |
| 100 | "Queue size must be a multiple of cache line size to " | 52 | "Queue size must be a multiple of cache line size to " |
| 101 | "prevent false sharing between adjacent queues"); | 53 | "prevent false sharing between adjacent queues"); |
| 102 | } | 54 | } |
| 103 | 55 | ||
| 104 | ~Queue() noexcept { | 56 | ~MPSCQueue() noexcept { |
| 105 | for (size_t i = 0; i < mask_ + 1; ++i) { | 57 | for (size_t i = 0; i < capacity; ++i) { |
| 106 | slots_[i].~Slot(); | 58 | std::destroy_at(&slots[i]); |
| 107 | } | 59 | } |
| 108 | allocator_.deallocate(slots_, mask_ + 2); | 60 | allocator.deallocate(slots, capacity + 1); |
| 109 | } | 61 | } |
| 110 | 62 | ||
| 111 | // non-copyable and non-movable | 63 | // The queue must be both non-copyable and non-movable |
| 112 | Queue(const Queue&) = delete; | 64 | MPSCQueue(const MPSCQueue&) = delete; |
| 113 | Queue& operator=(const Queue&) = delete; | 65 | MPSCQueue& operator=(const MPSCQueue&) = delete; |
| 66 | |||
| 67 | MPSCQueue(MPSCQueue&&) = delete; | ||
| 68 | MPSCQueue& operator=(MPSCQueue&&) = delete; | ||
| 114 | 69 | ||
| 115 | void Push(const T& v) noexcept { | 70 | void Push(const T& v) noexcept { |
| 116 | static_assert(std::is_nothrow_copy_constructible_v<T>, | 71 | static_assert(std::is_nothrow_copy_constructible_v<T>, |
| @@ -125,8 +80,8 @@ public: | |||
| 125 | 80 | ||
| 126 | void Pop(T& v, std::stop_token stop) noexcept { | 81 | void Pop(T& v, std::stop_token stop) noexcept { |
| 127 | auto const tail = tail_.fetch_add(1); | 82 | auto const tail = tail_.fetch_add(1); |
| 128 | auto& slot = slots_[idx(tail)]; | 83 | auto& slot = slots[idx(tail)]; |
| 129 | if (false == slot.turn.test()) { | 84 | if (!slot.turn.test()) { |
| 130 | std::unique_lock lock{cv_mutex}; | 85 | std::unique_lock lock{cv_mutex}; |
| 131 | cv.wait(lock, stop, [&slot] { return slot.turn.test(); }); | 86 | cv.wait(lock, stop, [&slot] { return slot.turn.test(); }); |
| 132 | } | 87 | } |
| @@ -137,12 +92,46 @@ public: | |||
| 137 | } | 92 | } |
| 138 | 93 | ||
| 139 | private: | 94 | private: |
| 95 | template <typename U = T> | ||
| 96 | struct Slot { | ||
| 97 | ~Slot() noexcept { | ||
| 98 | if (turn.test()) { | ||
| 99 | destroy(); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | template <typename... Args> | ||
| 104 | void construct(Args&&... args) noexcept { | ||
| 105 | static_assert(std::is_nothrow_constructible_v<U, Args&&...>, | ||
| 106 | "T must be nothrow constructible with Args&&..."); | ||
| 107 | std::construct_at(reinterpret_cast<U*>(&storage), std::forward<Args>(args)...); | ||
| 108 | } | ||
| 109 | |||
| 110 | void destroy() noexcept { | ||
| 111 | static_assert(std::is_nothrow_destructible_v<U>, "T must be nothrow destructible"); | ||
| 112 | std::destroy_at(reinterpret_cast<U*>(&storage)); | ||
| 113 | } | ||
| 114 | |||
| 115 | U&& move() noexcept { | ||
| 116 | return reinterpret_cast<U&&>(storage); | ||
| 117 | } | ||
| 118 | |||
| 119 | // Align to avoid false sharing between adjacent slots | ||
| 120 | alignas(hardware_interference_size) std::atomic_flag turn{}; | ||
| 121 | struct aligned_store { | ||
| 122 | struct type { | ||
| 123 | alignas(U) unsigned char data[sizeof(U)]; | ||
| 124 | }; | ||
| 125 | }; | ||
| 126 | typename aligned_store::type storage; | ||
| 127 | }; | ||
| 128 | |||
| 140 | template <typename... Args> | 129 | template <typename... Args> |
| 141 | void emplace(Args&&... args) noexcept { | 130 | void emplace(Args&&... args) noexcept { |
| 142 | static_assert(std::is_nothrow_constructible_v<T, Args&&...>, | 131 | static_assert(std::is_nothrow_constructible_v<T, Args&&...>, |
| 143 | "T must be nothrow constructible with Args&&..."); | 132 | "T must be nothrow constructible with Args&&..."); |
| 144 | auto const head = head_.fetch_add(1); | 133 | auto const head = head_.fetch_add(1); |
| 145 | auto& slot = slots_[idx(head)]; | 134 | auto& slot = slots[idx(head)]; |
| 146 | slot.turn.wait(true); | 135 | slot.turn.wait(true); |
| 147 | slot.construct(std::forward<Args>(args)...); | 136 | slot.construct(std::forward<Args>(args)...); |
| 148 | slot.turn.test_and_set(); | 137 | slot.turn.test_and_set(); |
| @@ -150,31 +139,29 @@ private: | |||
| 150 | } | 139 | } |
| 151 | 140 | ||
| 152 | constexpr size_t idx(size_t i) const noexcept { | 141 | constexpr size_t idx(size_t i) const noexcept { |
| 153 | return i & mask_; | 142 | return i & mask; |
| 154 | } | 143 | } |
| 155 | 144 | ||
| 156 | std::conditional_t<true, std::condition_variable_any, std::condition_variable> cv; | 145 | static constexpr size_t mask = capacity - 1; |
| 157 | std::mutex cv_mutex; | ||
| 158 | size_t mask_; | ||
| 159 | Slot<T>* slots_; | ||
| 160 | [[no_unique_address]] Allocator allocator_; | ||
| 161 | 146 | ||
| 162 | // Align to avoid false sharing between head_ and tail_ | 147 | // Align to avoid false sharing between head_ and tail_ |
| 163 | alignas(hardware_interference_size) std::atomic<size_t> head_{0}; | 148 | alignas(hardware_interference_size) std::atomic<size_t> head_{0}; |
| 164 | alignas(hardware_interference_size) std::atomic<size_t> tail_{0}; | 149 | alignas(hardware_interference_size) std::atomic<size_t> tail_{0}; |
| 165 | 150 | ||
| 151 | std::mutex cv_mutex; | ||
| 152 | std::condition_variable_any cv; | ||
| 153 | |||
| 154 | Slot<T>* slots; | ||
| 155 | [[no_unique_address]] std::allocator<Slot<T>> allocator; | ||
| 156 | |||
| 166 | static_assert(std::is_nothrow_copy_assignable_v<T> || std::is_nothrow_move_assignable_v<T>, | 157 | 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"); | 158 | "T must be nothrow copy or move assignable"); |
| 168 | 159 | ||
| 169 | static_assert(std::is_nothrow_destructible_v<T>, "T must be nothrow destructible"); | 160 | static_assert(std::is_nothrow_destructible_v<T>, "T must be nothrow destructible"); |
| 170 | }; | 161 | }; |
| 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 | 162 | ||
| 178 | #ifdef _MSC_VER | 163 | #ifdef _MSC_VER |
| 179 | #pragma warning(pop) | 164 | #pragma warning(pop) |
| 180 | #endif | 165 | #endif |
| 166 | |||
| 167 | } // namespace Common | ||