GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
SPSCQueue.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_DATA_SPSC_QUEUE_HPP
5#define GREM_CORE_DATA_SPSC_QUEUE_HPP
6
8
10
11#ifdef GREM_USE_MULTITHREADING
13#else
15#endif
16#include <cstddef> // std::size_t
17#include <type_traits> // std::is_nothrow_assignable_v
18#include <utility> // std::move, std::forward, std::declval
19
20namespace grem {
21
22template <typename T, std::size_t N>
23class SPSCQueue {
24public:
25 static_assert(N >= 2 && (N & (N - 1)) == 0, "SPSCQueue buffer size must be a positive power of 2!");
26
27 class Producer {
28 public:
29 template <typename Function>
30 void waitEnqueueWith(Function&& function) requires(noexcept(std::forward<Function>(function)(std::declval<T&>()))) {
31#ifdef GREM_USE_MULTITHREADING
32 const std::size_t currentWriteIndex = queue.writeIndex.load(MemoryOrder::RELAXED);
33 const std::size_t nextWriteIndex = (currentWriteIndex + 1) & (N - 1);
34 queue.readIndex.wait(nextWriteIndex, MemoryOrder::ACQUIRE);
35 std::forward<Function>(function)(queue.ringBuffer[currentWriteIndex]);
36 queue.writeIndex.store(nextWriteIndex, MemoryOrder::RELEASE);
37 queue.writeIndex.notify_all();
38#else
39 const std::size_t currentWriteIndex = queue.writeIndex;
40 const std::size_t nextWriteIndex = (currentWriteIndex + 1) & (N - 1);
41 GREM_ASSERT(queue.readIndex != nextWriteIndex);
42 std::forward<Function>(function)(queue.ringBuffer[currentWriteIndex]);
43 queue.writeIndex = nextWriteIndex;
44#endif
45 }
46
47 template <typename U>
48 void waitEnqueue(U&& value) requires(std::is_nothrow_assignable_v<T&, decltype(std::forward<U>(value))>) { // NOLINT(cppcoreguidelines-missing-std-forward)
49 waitEnqueueWith([&value](T& output) noexcept -> void { output = std::forward<U>(value); });
50 }
51
52 template <typename Function>
53 bool tryEnqueueWith(Function&& function) requires(noexcept(std::forward<Function>(function)(std::declval<T&>()))) {
54#ifdef GREM_USE_MULTITHREADING
55 const std::size_t currentWriteIndex = queue.writeIndex.load(MemoryOrder::RELAXED);
56 const std::size_t nextWriteIndex = (currentWriteIndex + 1) & (N - 1);
57 if (queue.readIndex.load(MemoryOrder::ACQUIRE) == nextWriteIndex) {
58 return false;
59 }
60 std::forward<Function>(function)(queue.ringBuffer[currentWriteIndex]);
61 queue.writeIndex.store(nextWriteIndex, MemoryOrder::RELEASE);
62 queue.writeIndex.notify_all();
63#else
64 const std::size_t currentWriteIndex = queue.writeIndex;
65 const std::size_t nextWriteIndex = (currentWriteIndex + 1) & (N - 1);
66 if (queue.readIndex == nextWriteIndex) {
67 return false;
68 }
69 std::forward<Function>(function)(queue.ringBuffer[currentWriteIndex]);
70 queue.writeIndex = nextWriteIndex;
71#endif
72 return true;
73 }
74
75 template <typename U>
76 [[nodiscard]] bool tryEnqueue(U&& value) requires(std::is_nothrow_assignable_v<T&, decltype(std::forward<U>(value))>) { // NOLINT(cppcoreguidelines-missing-std-forward)
77 return tryEnqueueWith([&value](T& output) noexcept -> void { output = std::forward<U>(value); });
78 }
79
80 private:
81 friend SPSCQueue;
82
83 constexpr explicit Producer(SPSCQueue& queue) noexcept
84 : queue(queue) {}
85
86 SPSCQueue& queue;
87 };
88
89 class Consumer {
90 public:
91 template <typename U>
92 void waitDequeue(U& value) requires(std::is_nothrow_assignable_v<U&, T &&>) {
93#ifdef GREM_USE_MULTITHREADING
94 const std::size_t currentReadIndex = queue.readIndex.load(MemoryOrder::RELAXED);
95 queue.writeIndex.wait(currentReadIndex, MemoryOrder::ACQUIRE);
96 value = std::move(queue.ringBuffer[currentReadIndex]);
97 const std::size_t nextReadIndex = (currentReadIndex + 1) & (N - 1);
98 queue.readIndex.store(nextReadIndex, MemoryOrder::RELEASE);
99 queue.readIndex.notify_all();
100#else
101 const std::size_t currentReadIndex = queue.readIndex;
102 GREM_ASSERT(queue.writeIndex != currentReadIndex);
103 value = std::move(queue.ringBuffer[currentReadIndex]);
104 const std::size_t nextReadIndex = (currentReadIndex + 1) & (N - 1);
105 queue.readIndex = nextReadIndex;
106#endif
107 }
108
109 template <typename U>
110 [[nodiscard]] bool tryDequeue(U& value) requires(std::is_nothrow_assignable_v<U&, T &&>) {
111#ifdef GREM_USE_MULTITHREADING
112 const std::size_t currentReadIndex = queue.readIndex.load(MemoryOrder::RELAXED);
113 if (queue.writeIndex.load(MemoryOrder::ACQUIRE) == currentReadIndex) {
114 return false;
115 }
116 value = std::move(queue.ringBuffer[currentReadIndex]);
117 const std::size_t nextReadIndex = (currentReadIndex + 1) & (N - 1);
118 queue.readIndex.store(nextReadIndex, MemoryOrder::RELEASE);
119 queue.readIndex.notify_all();
120#else
121 const std::size_t currentReadIndex = queue.readIndex;
122 if (queue.writeIndex == currentReadIndex) {
123 return false;
124 }
125 value = std::move(queue.ringBuffer[currentReadIndex]);
126 const std::size_t nextReadIndex = (currentReadIndex + 1) & (N - 1);
127 queue.readIndex = nextReadIndex;
128#endif
129 return true;
130 }
131
132 private:
133 friend SPSCQueue;
134
135 constexpr explicit Consumer(SPSCQueue& queue) noexcept
136 : queue(queue) {}
137
138 SPSCQueue& queue;
139 };
140
141 [[nodiscard]] Producer producer() {
142 return Producer{*this};
143 }
144
145 [[nodiscard]] Consumer consumer() {
146 return Consumer{*this};
147 }
148
149 void clear() {
150#ifdef GREM_USE_MULTITHREADING
151 readIndex.store(writeIndex.load(MemoryOrder::ACQUIRE), MemoryOrder::RELEASE);
152 readIndex.notify_all();
153#else
154 readIndex = writeIndex;
155#endif
156 }
157
158private:
159 Array<T, N> ringBuffer;
160#ifdef GREM_USE_MULTITHREADING
161 alignas(64) Atomic<std::size_t> readIndex{};
162 alignas(64) Atomic<std::size_t> writeIndex{};
163#else
164 std::size_t readIndex = 0;
165 std::size_t writeIndex = 0;
166#endif
167};
168
169} // namespace grem
170
171#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition synchronization.hpp:90
Definition SPSCQueue.hpp:89
void waitDequeue(U &value)
Definition SPSCQueue.hpp:92
bool tryDequeue(U &value)
Definition SPSCQueue.hpp:110
Definition SPSCQueue.hpp:27
bool tryEnqueueWith(Function &&function)
Definition SPSCQueue.hpp:53
bool tryEnqueue(U &&value)
Definition SPSCQueue.hpp:76
void waitEnqueueWith(Function &&function)
Definition SPSCQueue.hpp:30
void waitEnqueue(U &&value)
Definition SPSCQueue.hpp:48
Definition SPSCQueue.hpp:23
Consumer consumer()
Definition SPSCQueue.hpp:145
Producer producer()
Definition SPSCQueue.hpp:141
void clear()
Definition SPSCQueue.hpp:149
Definition aliases.hpp:9
std::function< Signature > Function
Definition Function.hpp:14
@ ACQUIRE
Definition synchronization.hpp:42
@ RELAXED
Definition synchronization.hpp:40
@ RELEASE
Definition synchronization.hpp:43
Definition Array.hpp:20