GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
SPSCSlidingWindowQueue.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_SLIDING_WINDOW_QUEUE_HPP
5#define GREM_CORE_DATA_SPSC_SLIDING_WINDOW_QUEUE_HPP
6
8
10
11#ifdef GREM_USE_MULTITHREADING
13#else
15#endif
16#include <cstddef> // std::size_t, std::ptrdiff_t
17#include <iterator> // std::random_access_iterator_tag
18#include <type_traits> // std::is_..._v
19#include <utility> // std::move, std::forward, std::declval
20
21namespace grem {
22
23namespace detail {
24
25template <typename T, std::size_t N>
26class SPSCSlidingWindowQueueIterator {
27public:
28 static_assert(N >= 2 && (N & (N - 1)) == 0, "SPSCSlidingWindowQueue buffer size must be a positive power of 2!");
29
30 using difference_type = std::size_t;
31 using value_type = T;
32 using pointer = T*;
33 using reference = T&;
34 using iterator_category = std::random_access_iterator_tag;
35
36 SPSCSlidingWindowQueueIterator() = default;
37
38 constexpr SPSCSlidingWindowQueueIterator(pointer ringBuffer, std::size_t index) noexcept
39 : ringBuffer(ringBuffer)
40 , index(index) {}
41
42 constexpr operator SPSCSlidingWindowQueueIterator<const T, N>() const noexcept requires(!std::is_const_v<T>) {
43 return SPSCSlidingWindowQueueIterator<const T, N>{ringBuffer, index};
44 }
45
46 [[nodiscard]] constexpr reference operator*() const {
47 return ringBuffer[index];
48 }
49
50 [[nodiscard]] constexpr pointer operator->() const {
51 return &ringBuffer[index];
52 }
53
54 [[nodiscard]] constexpr reference operator[](difference_type n) const {
55 return ringBuffer[(index + n) & (N - 1)];
56 }
57
58 constexpr SPSCSlidingWindowQueueIterator& operator++() {
59 index = (index - 1) & (N - 1);
60 return *this;
61 }
62
63 constexpr SPSCSlidingWindowQueueIterator& operator--() {
64 index = (index + 1) & (N - 1);
65 return *this;
66 }
67
68 constexpr SPSCSlidingWindowQueueIterator operator++(int) {
69 SPSCSlidingWindowQueueIterator old = *this;
70 ++*this;
71 return old;
72 }
73
74 constexpr SPSCSlidingWindowQueueIterator operator--(int) {
75 SPSCSlidingWindowQueueIterator old = *this;
76 --*this;
77 return old;
78 }
79
80 constexpr SPSCSlidingWindowQueueIterator& operator+=(difference_type n) {
81 index = (index + n) & (N - 1);
82 return *this;
83 }
84
85 constexpr SPSCSlidingWindowQueueIterator& operator-=(difference_type n) {
86 index = (index - n) & (N - 1);
87 return *this;
88 }
89
90 [[nodiscard]] friend constexpr SPSCSlidingWindowQueueIterator operator+(SPSCSlidingWindowQueueIterator a, difference_type b) {
91 return SPSCSlidingWindowQueueIterator{a.ringBuffer, (a.index + b) & (N - 1)};
92 }
93
94 [[nodiscard]] friend constexpr SPSCSlidingWindowQueueIterator operator+(difference_type a, SPSCSlidingWindowQueueIterator b) {
95 return SPSCSlidingWindowQueueIterator{b.ringBuffer, (a + b.index) & (N - 1)};
96 }
97
98 [[nodiscard]] friend constexpr SPSCSlidingWindowQueueIterator operator-(SPSCSlidingWindowQueueIterator a, difference_type b) {
99 return SPSCSlidingWindowQueueIterator{a.ringBuffer, (a.index - b) & (N - 1)};
100 }
101
102 [[nodiscard]] friend constexpr difference_type operator-(SPSCSlidingWindowQueueIterator a, SPSCSlidingWindowQueueIterator b) {
103 GREM_ASSERT(a.ringBuffer = b.ringBuffer);
104 return (a.index - b.index) & (N - 1);
105 }
106
107 [[nodiscard]] friend constexpr bool operator==(SPSCSlidingWindowQueueIterator a, SPSCSlidingWindowQueueIterator b) {
108 GREM_ASSERT(a.ringBuffer == b.ringBuffer);
109 return a.index == b.index;
110 }
111
112 [[nodiscard]] friend constexpr auto operator<=>(SPSCSlidingWindowQueueIterator a, SPSCSlidingWindowQueueIterator b) {
113 GREM_ASSERT(a.ringBuffer == b.ringBuffer);
114 return a.index <=> b.index;
115 }
116
117private:
118 pointer ringBuffer;
119 std::size_t index;
120};
121
122} // namespace detail
123
124template <typename T, std::size_t N, std::size_t W>
126public:
127 static_assert(N >= 2 && (N & (N - 1)) == 0, "SPSCSlidingWindowQueue buffer size must be a positive power of 2!");
128 static_assert(W > 0 && W < N, "SPSCSlidingWindowQueue window size must be positive and smaller than the buffer size!");
129
130 using size_type = std::size_t;
131 using difference_type = std::size_t;
132 using value_type = T;
133 using pointer = T*;
134 using const_pointer = const T*;
135 using reference = T&;
136 using const_reference = const T&;
137 using iterator = detail::SPSCSlidingWindowQueueIterator<T, N>;
138 using const_iterator = detail::SPSCSlidingWindowQueueIterator<const T, N>;
139
140 class WindowView {
141 public:
151
152 [[nodiscard]] constexpr iterator begin() const noexcept {
153 return first;
154 }
155
156 [[nodiscard]] constexpr iterator end() const noexcept {
157 return last;
158 }
159
160 [[nodiscard]] constexpr size_type size() const noexcept {
161 return static_cast<size_type>(last - first);
162 }
163
164 [[nodiscard]] constexpr bool empty() const noexcept {
165 return size() == 0;
166 }
167
168 [[nodiscard]] constexpr reference operator[](size_type n) const {
169 return first[static_cast<difference_type>(n)];
170 }
171
172 [[nodiscard]] constexpr reference front() const {
173 return *first;
174 }
175
176 [[nodiscard]] constexpr reference back() const {
177 return *(last - 1);
178 }
179
180 private:
182
183 constexpr WindowView(const_iterator first, const_iterator last) noexcept
184 : first(first)
185 , last(last) {}
186
187 const_iterator first;
188 const_iterator last;
189 };
190
191 class Producer {
192 public:
193 [[nodiscard]] WindowView lookBehind() const {
194#ifdef GREM_USE_MULTITHREADING
195 const std::size_t currentWriteIndex = queue.writeIndex.load(MemoryOrder::RELAXED);
196#else
197 const std::size_t currentWriteIndex = queue.writeIndex;
198#endif
199 return WindowView{
200 const_iterator{queue.ringBuffer.data(), currentWriteIndex - N},
201 const_iterator{queue.ringBuffer.data(), currentWriteIndex},
202 };
203 }
204
205 template <typename Function>
206 void waitEnqueueWith(Function&& function) requires(noexcept(std::forward<Function>(function)(std::declval<T&>()))) {
207#ifdef GREM_USE_MULTITHREADING
208 const std::size_t currentWriteIndex = queue.writeIndex.load(MemoryOrder::RELAXED);
209 queue.readIndex.wait((currentWriteIndex + W) & (N - 1), MemoryOrder::ACQUIRE);
210 std::forward<Function>(function)(queue.ringBuffer[currentWriteIndex]);
211 const std::size_t nextWriteIndex = (currentWriteIndex + 1) & (N - 1);
212 queue.writeIndex.store(nextWriteIndex, MemoryOrder::RELEASE);
213 queue.writeIndex.notify_all();
214#else
215 const std::size_t currentWriteIndex = queue.writeIndex;
216 GREM_ASSERT(queue.readIndex != ((currentWriteIndex + W) & (N - 1)));
217 std::forward<Function>(function)(queue.ringBuffer[currentWriteIndex]);
218 const std::size_t nextWriteIndex = (currentWriteIndex + 1) & (N - 1);
219 queue.writeIndex = nextWriteIndex;
220#endif
221 }
222
223 template <typename U>
224 void waitEnqueue(U&& value) requires(std::is_nothrow_assignable_v<T&, decltype(std::forward<U>(value))>) { // NOLINT(cppcoreguidelines-missing-std-forward)
225 waitEnqueueWith([&value](T& output) noexcept -> void { output = std::forward<U>(value); });
226 }
227
228 template <typename Function>
229 bool tryEnqueueWith(Function&& function) requires(noexcept(std::forward<Function>(function)(std::declval<T&>()))) {
230#ifdef GREM_USE_MULTITHREADING
231 const std::size_t currentWriteIndex = queue.writeIndex.load(MemoryOrder::RELAXED);
232 if (queue.readIndex.load(MemoryOrder::ACQUIRE) == ((currentWriteIndex + W) & (N - 1))) {
233 return false;
234 }
235 std::forward<Function>(function)(queue.ringBuffer[currentWriteIndex]);
236 const std::size_t nextWriteIndex = (currentWriteIndex + 1) & (N - 1);
237 queue.writeIndex.store(nextWriteIndex, MemoryOrder::RELEASE);
238 queue.writeIndex.notify_all();
239#else
240 const std::size_t currentWriteIndex = queue.writeIndex;
241 if (queue.readIndex == ((currentWriteIndex + W) & (N - 1))) {
242 return false;
243 }
244 std::forward<Function>(function)(queue.ringBuffer[currentWriteIndex]);
245 const std::size_t nextWriteIndex = (currentWriteIndex + 1) & (N - 1);
246 queue.writeIndex = nextWriteIndex;
247#endif
248 return true;
249 }
250
251 template <typename U>
252 [[nodiscard]] bool tryEnqueue(U&& value) requires(std::is_nothrow_assignable_v<T&, decltype(std::forward<U>(value))>) { // NOLINT(cppcoreguidelines-missing-std-forward)
253 return tryEnqueueWith([&value](T& output) noexcept -> void { output = std::forward<U>(value); });
254 }
255
256 private:
258
259 constexpr explicit Producer(SPSCSlidingWindowQueue& queue) noexcept
260 : queue(queue) {}
261
263 };
264
265 class ConstProducer {
266 public:
267 [[nodiscard]] WindowView lookBehind() const {
268#ifdef GREM_USE_MULTITHREADING
269 const std::size_t currentWriteIndex = queue.writeIndex.load(MemoryOrder::RELAXED);
270#else
271 const std::size_t currentWriteIndex = queue.writeIndex;
272#endif
273 return WindowView{
274 const_iterator{queue.ringBuffer.data(), currentWriteIndex - N},
275 const_iterator{queue.ringBuffer.data(), currentWriteIndex},
276 };
277 }
278
279 private:
281
282 constexpr explicit ConstProducer(const SPSCSlidingWindowQueue& queue) noexcept
283 : queue(queue) {}
284
285 const SPSCSlidingWindowQueue& queue;
286 };
287
288 class Consumer {
289 public:
290 [[nodiscard]] WindowView getWindow() const {
291#ifdef GREM_USE_MULTITHREADING
292 const std::size_t currentReadIndex = queue.readIndex.load(MemoryOrder::RELAXED);
293#else
294 const std::size_t currentReadIndex = queue.readIndex;
295#endif
296 return WindowView{
297 const_iterator{queue.ringBuffer.data(), currentReadIndex - W},
298 const_iterator{queue.ringBuffer.data(), currentReadIndex},
299 };
300 }
301
303#ifdef GREM_USE_MULTITHREADING
304 const std::size_t currentReadIndex = queue.readIndex.load(MemoryOrder::RELAXED);
305 const std::size_t currentWriteIndex = queue.writeIndex.load(MemoryOrder::ACQUIRE);
306 queue.readIndex.store(currentWriteIndex, MemoryOrder::RELEASE);
307 queue.readIndex.notify_all();
308#else
309 const std::size_t currentReadIndex = queue.readIndex;
310 const std::size_t currentWriteIndex = queue.writeIndex;
311 queue.readIndex = currentWriteIndex;
312#endif
313 return (currentWriteIndex - currentReadIndex) & (N - 1);
314 }
315
317#ifdef GREM_USE_MULTITHREADING
318 const std::size_t currentReadIndex = queue.readIndex.load(MemoryOrder::RELAXED);
319 queue.writeIndex.wait(currentReadIndex, MemoryOrder::ACQUIRE);
320 const std::size_t nextReadIndex = (currentReadIndex + 1) & (N - 1);
321 queue.readIndex.store(nextReadIndex, MemoryOrder::RELEASE);
322 queue.readIndex.notify_all();
323#else
324 const std::size_t currentReadIndex = queue.readIndex;
325 GREM_ASSERT(queue.writeIndex != currentReadIndex);
326 const std::size_t nextReadIndex = (currentReadIndex + 1) & (N - 1);
327 queue.readIndex = nextReadIndex;
328#endif
329 }
330
331 [[nodiscard]] bool tryAdvanceWindow() {
332#ifdef GREM_USE_MULTITHREADING
333 const std::size_t currentReadIndex = queue.readIndex.load(MemoryOrder::RELAXED);
334 if (queue.writeIndex.load(MemoryOrder::ACQUIRE) == currentReadIndex) {
335 return false;
336 }
337 const std::size_t nextReadIndex = (currentReadIndex + 1) & (N - 1);
338 queue.readIndex.store(nextReadIndex, MemoryOrder::RELEASE);
339 queue.readIndex.notify_all();
340#else
341 const std::size_t currentReadIndex = queue.readIndex;
342 if (queue.writeIndex == currentReadIndex) {
343 return false;
344 }
345 const std::size_t nextReadIndex = (currentReadIndex + 1) & (N - 1);
346 queue.readIndex = nextReadIndex;
347#endif
348 return true;
349 }
350
351 private:
353
354 constexpr explicit Consumer(SPSCSlidingWindowQueue& queue) noexcept
355 : queue(queue) {}
356
358 };
359
360 class ConstConsumer {
361 public:
362 [[nodiscard]] WindowView getWindow() const {
363#ifdef GREM_USE_MULTITHREADING
364 const std::size_t currentReadIndex = queue.readIndex.load(MemoryOrder::RELAXED);
365#else
366 const std::size_t currentReadIndex = queue.readIndex;
367#endif
368 return WindowView{
369 const_iterator{queue.ringBuffer.data(), currentReadIndex - W},
370 const_iterator{queue.ringBuffer.data(), currentReadIndex},
371 };
372 }
373
374 private:
376
377 constexpr explicit ConstConsumer(const SPSCSlidingWindowQueue& queue) noexcept
378 : queue(queue) {}
379
380 const SPSCSlidingWindowQueue& queue;
381 };
382
383 [[nodiscard]] Producer producer() {
384 return Producer{*this};
385 }
386
387 [[nodiscard]] ConstProducer producer() const {
388 return ConstProducer{*this};
389 }
390
391 [[nodiscard]] Consumer consumer() {
392 return Consumer{*this};
393 }
394
395 [[nodiscard]] ConstConsumer consumer() const {
396 return ConstConsumer{*this};
397 }
398
399 [[nodiscard]] constexpr size_type getWindowSize() const noexcept {
400 return W;
401 }
402
403 template <typename Function>
404 void unsynchronizedFillWith(Function&& function) requires(noexcept(function(std::declval<T&>()))) { // NOLINT(cppcoreguidelines-missing-std-forward)
405#ifdef GREM_USE_MULTITHREADING
406 const std::size_t currentWriteIndex = writeIndex.load(MemoryOrder::ACQUIRE);
407 readIndex.store(currentWriteIndex, MemoryOrder::RELEASE);
408 for (std::size_t i = W; i-- > 0;) {
409 function(ringBuffer[(currentWriteIndex - 1 - i) & (N - 1)]);
410 }
411#else
412 const std::size_t currentWriteIndex = writeIndex;
413 readIndex = currentWriteIndex;
414 for (std::size_t i = W; i-- > 0;) {
415 function(ringBuffer[(currentWriteIndex - 1 - i) & (N - 1)]);
416 }
417#endif
418 }
419
420 template <typename U>
421 void unsynchronizedFill(const U& value) requires(std::is_nothrow_assignable_v<T&, const U&>) {
422 unsynchronizedFillWith([&value](T& output) noexcept -> void { output = value; });
423 }
424
425private:
426 Array<T, N> ringBuffer;
427#ifdef GREM_USE_MULTITHREADING
428 alignas(64) Atomic<std::size_t> readIndex{};
429 alignas(64) Atomic<std::size_t> writeIndex{};
430#else
431 std::size_t readIndex = 0;
432 std::size_t writeIndex = 0;
433#endif
434};
435
436} // namespace grem
437
438#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition synchronization.hpp:90
Definition SPSCSlidingWindowQueue.hpp:360
WindowView getWindow() const
Definition SPSCSlidingWindowQueue.hpp:362
Definition SPSCSlidingWindowQueue.hpp:265
WindowView lookBehind() const
Definition SPSCSlidingWindowQueue.hpp:267
Definition SPSCSlidingWindowQueue.hpp:288
bool tryAdvanceWindow()
Definition SPSCSlidingWindowQueue.hpp:331
WindowView getWindow() const
Definition SPSCSlidingWindowQueue.hpp:290
size_type advanceWindowToMostRecent()
Definition SPSCSlidingWindowQueue.hpp:302
void waitAdvanceWindow()
Definition SPSCSlidingWindowQueue.hpp:316
Definition SPSCSlidingWindowQueue.hpp:191
bool tryEnqueue(U &&value)
Definition SPSCSlidingWindowQueue.hpp:252
void waitEnqueue(U &&value)
Definition SPSCSlidingWindowQueue.hpp:224
void waitEnqueueWith(Function &&function)
Definition SPSCSlidingWindowQueue.hpp:206
bool tryEnqueueWith(Function &&function)
Definition SPSCSlidingWindowQueue.hpp:229
WindowView lookBehind() const
Definition SPSCSlidingWindowQueue.hpp:193
Definition SPSCSlidingWindowQueue.hpp:140
SPSCSlidingWindowQueue::size_type size_type
Definition SPSCSlidingWindowQueue.hpp:142
SPSCSlidingWindowQueue::const_iterator iterator
Definition SPSCSlidingWindowQueue.hpp:149
constexpr size_type size() const noexcept
Definition SPSCSlidingWindowQueue.hpp:160
constexpr bool empty() const noexcept
Definition SPSCSlidingWindowQueue.hpp:164
SPSCSlidingWindowQueue::const_reference const_reference
Definition SPSCSlidingWindowQueue.hpp:148
constexpr iterator end() const noexcept
Definition SPSCSlidingWindowQueue.hpp:156
SPSCSlidingWindowQueue::const_pointer const_pointer
Definition SPSCSlidingWindowQueue.hpp:146
SPSCSlidingWindowQueue::const_iterator const_iterator
Definition SPSCSlidingWindowQueue.hpp:150
SPSCSlidingWindowQueue::value_type value_type
Definition SPSCSlidingWindowQueue.hpp:144
SPSCSlidingWindowQueue::difference_type difference_type
Definition SPSCSlidingWindowQueue.hpp:143
constexpr reference operator[](size_type n) const
Definition SPSCSlidingWindowQueue.hpp:168
SPSCSlidingWindowQueue::const_reference reference
Definition SPSCSlidingWindowQueue.hpp:147
constexpr iterator begin() const noexcept
Definition SPSCSlidingWindowQueue.hpp:152
constexpr reference front() const
Definition SPSCSlidingWindowQueue.hpp:172
constexpr reference back() const
Definition SPSCSlidingWindowQueue.hpp:176
Definition SPSCSlidingWindowQueue.hpp:125
T value_type
Definition SPSCSlidingWindowQueue.hpp:132
ConstConsumer consumer() const
Definition SPSCSlidingWindowQueue.hpp:395
std::size_t size_type
Definition SPSCSlidingWindowQueue.hpp:130
constexpr size_type getWindowSize() const noexcept
Definition SPSCSlidingWindowQueue.hpp:399
ConstProducer producer() const
Definition SPSCSlidingWindowQueue.hpp:387
void unsynchronizedFill(const U &value)
Definition SPSCSlidingWindowQueue.hpp:421
detail::SPSCSlidingWindowQueueIterator< T, N > iterator
Definition SPSCSlidingWindowQueue.hpp:137
const T * const_pointer
Definition SPSCSlidingWindowQueue.hpp:134
const T & const_reference
Definition SPSCSlidingWindowQueue.hpp:136
std::size_t difference_type
Definition SPSCSlidingWindowQueue.hpp:131
Producer producer()
Definition SPSCSlidingWindowQueue.hpp:383
detail::SPSCSlidingWindowQueueIterator< const T, N > const_iterator
Definition SPSCSlidingWindowQueue.hpp:138
T & reference
Definition SPSCSlidingWindowQueue.hpp:135
Consumer consumer()
Definition SPSCSlidingWindowQueue.hpp:391
void unsynchronizedFillWith(Function &&function)
Definition SPSCSlidingWindowQueue.hpp:404
Definition concepts.hpp:106
@ N
'n'
Definition keyboard.hpp:84
@ T
't'
Definition keyboard.hpp:90
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