bounded_threadsafe_queue.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <atomic>
  5. #include <condition_variable>
  6. #include <cstddef>
  7. #include <mutex>
  8. #include <new>
  9. #include "common/polyfill_thread.h"
  10. namespace Common {
  11. namespace detail {
  12. constexpr size_t DefaultCapacity = 0x1000;
  13. } // namespace detail
  14. template <typename T, size_t Capacity = detail::DefaultCapacity>
  15. class SPSCQueue {
  16. static_assert((Capacity & (Capacity - 1)) == 0, "Capacity must be a power of two.");
  17. public:
  18. template <typename... Args>
  19. bool TryEmplace(Args&&... args) {
  20. return Emplace<PushMode::Try>(std::forward<Args>(args)...);
  21. }
  22. template <typename... Args>
  23. void EmplaceWait(Args&&... args) {
  24. Emplace<PushMode::Wait>(std::forward<Args>(args)...);
  25. }
  26. bool TryPop(T& t) {
  27. return Pop<PopMode::Try>(t);
  28. }
  29. void PopWait(T& t) {
  30. Pop<PopMode::Wait>(t);
  31. }
  32. void PopWait(T& t, std::stop_token stop_token) {
  33. Pop<PopMode::WaitWithStopToken>(t, stop_token);
  34. }
  35. T PopWait() {
  36. T t{};
  37. Pop<PopMode::Wait>(t);
  38. return t;
  39. }
  40. T PopWait(std::stop_token stop_token) {
  41. T t{};
  42. Pop<PopMode::WaitWithStopToken>(t, stop_token);
  43. return t;
  44. }
  45. private:
  46. enum class PushMode {
  47. Try,
  48. Wait,
  49. Count,
  50. };
  51. enum class PopMode {
  52. Try,
  53. Wait,
  54. WaitWithStopToken,
  55. Count,
  56. };
  57. template <PushMode Mode, typename... Args>
  58. bool Emplace(Args&&... args) {
  59. const size_t write_index = m_write_index.load(std::memory_order::relaxed);
  60. if constexpr (Mode == PushMode::Try) {
  61. // Check if we have free slots to write to.
  62. if ((write_index - m_read_index.load(std::memory_order::acquire)) == Capacity) {
  63. return false;
  64. }
  65. } else if constexpr (Mode == PushMode::Wait) {
  66. // Wait until we have free slots to write to.
  67. std::unique_lock lock{producer_cv_mutex};
  68. producer_cv.wait(lock, [this, write_index] {
  69. return (write_index - m_read_index.load(std::memory_order::acquire)) < Capacity;
  70. });
  71. } else {
  72. static_assert(Mode < PushMode::Count, "Invalid PushMode.");
  73. }
  74. // Determine the position to write to.
  75. const size_t pos = write_index % Capacity;
  76. // Emplace into the queue.
  77. std::construct_at(std::addressof(m_data[pos]), std::forward<Args>(args)...);
  78. // Increment the write index.
  79. ++m_write_index;
  80. // Notify the consumer that we have pushed into the queue.
  81. std::scoped_lock lock{consumer_cv_mutex};
  82. consumer_cv.notify_one();
  83. return true;
  84. }
  85. template <PopMode Mode>
  86. bool Pop(T& t, [[maybe_unused]] std::stop_token stop_token = {}) {
  87. const size_t read_index = m_read_index.load(std::memory_order::relaxed);
  88. if constexpr (Mode == PopMode::Try) {
  89. // Check if the queue is empty.
  90. if (read_index == m_write_index.load(std::memory_order::acquire)) {
  91. return false;
  92. }
  93. } else if constexpr (Mode == PopMode::Wait) {
  94. // Wait until the queue is not empty.
  95. std::unique_lock lock{consumer_cv_mutex};
  96. consumer_cv.wait(lock, [this, read_index] {
  97. return read_index != m_write_index.load(std::memory_order::acquire);
  98. });
  99. } else if constexpr (Mode == PopMode::WaitWithStopToken) {
  100. // Wait until the queue is not empty.
  101. std::unique_lock lock{consumer_cv_mutex};
  102. Common::CondvarWait(consumer_cv, lock, stop_token, [this, read_index] {
  103. return read_index != m_write_index.load(std::memory_order::acquire);
  104. });
  105. if (stop_token.stop_requested()) {
  106. return false;
  107. }
  108. } else {
  109. static_assert(Mode < PopMode::Count, "Invalid PopMode.");
  110. }
  111. // Determine the position to read from.
  112. const size_t pos = read_index % Capacity;
  113. // Pop the data off the queue, moving it.
  114. t = std::move(m_data[pos]);
  115. // Increment the read index.
  116. ++m_read_index;
  117. // Notify the producer that we have popped off the queue.
  118. std::scoped_lock lock{producer_cv_mutex};
  119. producer_cv.notify_one();
  120. return true;
  121. }
  122. alignas(128) std::atomic_size_t m_read_index{0};
  123. alignas(128) std::atomic_size_t m_write_index{0};
  124. std::array<T, Capacity> m_data;
  125. std::condition_variable_any producer_cv;
  126. std::mutex producer_cv_mutex;
  127. std::condition_variable_any consumer_cv;
  128. std::mutex consumer_cv_mutex;
  129. };
  130. template <typename T, size_t Capacity = detail::DefaultCapacity>
  131. class MPSCQueue {
  132. public:
  133. template <typename... Args>
  134. bool TryEmplace(Args&&... args) {
  135. std::scoped_lock lock{write_mutex};
  136. return spsc_queue.TryEmplace(std::forward<Args>(args)...);
  137. }
  138. template <typename... Args>
  139. void EmplaceWait(Args&&... args) {
  140. std::scoped_lock lock{write_mutex};
  141. spsc_queue.EmplaceWait(std::forward<Args>(args)...);
  142. }
  143. bool TryPop(T& t) {
  144. return spsc_queue.TryPop(t);
  145. }
  146. void PopWait(T& t) {
  147. spsc_queue.PopWait(t);
  148. }
  149. void PopWait(T& t, std::stop_token stop_token) {
  150. spsc_queue.PopWait(t, stop_token);
  151. }
  152. T PopWait() {
  153. return spsc_queue.PopWait();
  154. }
  155. T PopWait(std::stop_token stop_token) {
  156. return spsc_queue.PopWait(stop_token);
  157. }
  158. private:
  159. SPSCQueue<T, Capacity> spsc_queue;
  160. std::mutex write_mutex;
  161. };
  162. template <typename T, size_t Capacity = detail::DefaultCapacity>
  163. class MPMCQueue {
  164. public:
  165. template <typename... Args>
  166. bool TryEmplace(Args&&... args) {
  167. std::scoped_lock lock{write_mutex};
  168. return spsc_queue.TryEmplace(std::forward<Args>(args)...);
  169. }
  170. template <typename... Args>
  171. void EmplaceWait(Args&&... args) {
  172. std::scoped_lock lock{write_mutex};
  173. spsc_queue.EmplaceWait(std::forward<Args>(args)...);
  174. }
  175. bool TryPop(T& t) {
  176. std::scoped_lock lock{read_mutex};
  177. return spsc_queue.TryPop(t);
  178. }
  179. void PopWait(T& t) {
  180. std::scoped_lock lock{read_mutex};
  181. spsc_queue.PopWait(t);
  182. }
  183. void PopWait(T& t, std::stop_token stop_token) {
  184. std::scoped_lock lock{read_mutex};
  185. spsc_queue.PopWait(t, stop_token);
  186. }
  187. T PopWait() {
  188. std::scoped_lock lock{read_mutex};
  189. return spsc_queue.PopWait();
  190. }
  191. T PopWait(std::stop_token stop_token) {
  192. std::scoped_lock lock{read_mutex};
  193. return spsc_queue.PopWait(stop_token);
  194. }
  195. private:
  196. SPSCQueue<T, Capacity> spsc_queue;
  197. std::mutex write_mutex;
  198. std::mutex read_mutex;
  199. };
  200. } // namespace Common