4#ifndef GREM_CORE_DATA_SPSC_SLIDING_WINDOW_QUEUE_HPP
5#define GREM_CORE_DATA_SPSC_SLIDING_WINDOW_QUEUE_HPP
11#ifdef GREM_USE_MULTITHREADING
25template <
typename T, std::
size_t N>
26class SPSCSlidingWindowQueueIterator {
28 static_assert(
N >= 2 && (
N & (
N - 1)) == 0,
"SPSCSlidingWindowQueue buffer size must be a positive power of 2!");
30 using difference_type = std::size_t;
34 using iterator_category = std::random_access_iterator_tag;
36 SPSCSlidingWindowQueueIterator() =
default;
38 constexpr SPSCSlidingWindowQueueIterator(pointer ringBuffer, std::size_t index) noexcept
39 : ringBuffer(ringBuffer)
42 constexpr operator SPSCSlidingWindowQueueIterator<const T, N>() const noexcept requires(!std::is_const_v<T>) {
43 return SPSCSlidingWindowQueueIterator<const T, N>{ringBuffer, index};
46 [[nodiscard]]
constexpr reference operator*()
const {
47 return ringBuffer[index];
50 [[nodiscard]]
constexpr pointer operator->()
const {
51 return &ringBuffer[index];
54 [[nodiscard]]
constexpr reference operator[](difference_type n)
const {
55 return ringBuffer[(index + n) & (N - 1)];
58 constexpr SPSCSlidingWindowQueueIterator& operator++() {
59 index = (index - 1) & (N - 1);
63 constexpr SPSCSlidingWindowQueueIterator& operator--() {
64 index = (index + 1) & (N - 1);
68 constexpr SPSCSlidingWindowQueueIterator operator++(
int) {
69 SPSCSlidingWindowQueueIterator old = *
this;
74 constexpr SPSCSlidingWindowQueueIterator operator--(
int) {
75 SPSCSlidingWindowQueueIterator old = *
this;
80 constexpr SPSCSlidingWindowQueueIterator& operator+=(difference_type n) {
81 index = (index + n) & (N - 1);
85 constexpr SPSCSlidingWindowQueueIterator& operator-=(difference_type n) {
86 index = (index - n) & (N - 1);
90 [[nodiscard]]
friend constexpr SPSCSlidingWindowQueueIterator operator+(SPSCSlidingWindowQueueIterator a, difference_type b) {
91 return SPSCSlidingWindowQueueIterator{a.ringBuffer, (a.index + b) & (N - 1)};
94 [[nodiscard]]
friend constexpr SPSCSlidingWindowQueueIterator operator+(difference_type a, SPSCSlidingWindowQueueIterator b) {
95 return SPSCSlidingWindowQueueIterator{b.ringBuffer, (a + b.index) & (N - 1)};
98 [[nodiscard]]
friend constexpr SPSCSlidingWindowQueueIterator operator-(SPSCSlidingWindowQueueIterator a, difference_type b) {
99 return SPSCSlidingWindowQueueIterator{a.ringBuffer, (a.index - b) & (N - 1)};
102 [[nodiscard]]
friend constexpr difference_type operator-(SPSCSlidingWindowQueueIterator a, SPSCSlidingWindowQueueIterator b) {
104 return (a.index - b.index) & (
N - 1);
107 [[nodiscard]]
friend constexpr bool operator==(SPSCSlidingWindowQueueIterator a, SPSCSlidingWindowQueueIterator b) {
109 return a.index == b.index;
112 [[nodiscard]]
friend constexpr auto operator<=>(SPSCSlidingWindowQueueIterator a, SPSCSlidingWindowQueueIterator b) {
114 return a.index <=> b.index;
124template <
typename T, std::
size_t N, std::
size_t W>
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!");
137 using iterator = detail::SPSCSlidingWindowQueueIterator<T, N>;
161 return static_cast<size_type>(last - first);
164 [[nodiscard]]
constexpr bool empty() const noexcept {
194#ifdef GREM_USE_MULTITHREADING
197 const std::size_t currentWriteIndex = queue.writeIndex;
205 template <
typename Function>
207#ifdef GREM_USE_MULTITHREADING
210 std::forward<Function>(function)(queue.ringBuffer[currentWriteIndex]);
211 const std::size_t nextWriteIndex = (currentWriteIndex + 1) & (N - 1);
213 queue.writeIndex.notify_all();
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;
223 template <
typename U>
224 void waitEnqueue(U&& value)
requires(std::is_nothrow_assignable_v<T&, decltype(std::forward<U>(value))>) {
225 waitEnqueueWith([&value](T& output)
noexcept ->
void { output = std::forward<U>(value); });
228 template <
typename Function>
230#ifdef GREM_USE_MULTITHREADING
235 std::forward<Function>(function)(queue.ringBuffer[currentWriteIndex]);
236 const std::size_t nextWriteIndex = (currentWriteIndex + 1) & (N - 1);
238 queue.writeIndex.notify_all();
240 const std::size_t currentWriteIndex = queue.writeIndex;
241 if (queue.readIndex == ((currentWriteIndex + W) & (N - 1))) {
244 std::forward<Function>(function)(queue.ringBuffer[currentWriteIndex]);
245 const std::size_t nextWriteIndex = (currentWriteIndex + 1) & (N - 1);
246 queue.writeIndex = nextWriteIndex;
251 template <
typename U>
252 [[nodiscard]]
bool tryEnqueue(U&& value)
requires(std::is_nothrow_assignable_v<T&, decltype(std::forward<U>(value))>) {
253 return tryEnqueueWith([&value](T& output)
noexcept ->
void { output = std::forward<U>(value); });
265 class ConstProducer {
268#ifdef GREM_USE_MULTITHREADING
271 const std::size_t currentWriteIndex = queue.writeIndex;
291#ifdef GREM_USE_MULTITHREADING
294 const std::size_t currentReadIndex = queue.readIndex;
303#ifdef GREM_USE_MULTITHREADING
307 queue.readIndex.notify_all();
309 const std::size_t currentReadIndex = queue.readIndex;
310 const std::size_t currentWriteIndex = queue.writeIndex;
311 queue.readIndex = currentWriteIndex;
313 return (currentWriteIndex - currentReadIndex) & (N - 1);
317#ifdef GREM_USE_MULTITHREADING
320 const std::size_t nextReadIndex = (currentReadIndex + 1) & (N - 1);
322 queue.readIndex.notify_all();
324 const std::size_t currentReadIndex = queue.readIndex;
326 const std::size_t nextReadIndex = (currentReadIndex + 1) & (N - 1);
327 queue.readIndex = nextReadIndex;
332#ifdef GREM_USE_MULTITHREADING
337 const std::size_t nextReadIndex = (currentReadIndex + 1) & (N - 1);
339 queue.readIndex.notify_all();
341 const std::size_t currentReadIndex = queue.readIndex;
342 if (queue.writeIndex == currentReadIndex) {
345 const std::size_t nextReadIndex = (currentReadIndex + 1) & (N - 1);
346 queue.readIndex = nextReadIndex;
360 class ConstConsumer {
363#ifdef GREM_USE_MULTITHREADING
366 const std::size_t currentReadIndex = queue.readIndex;
403 template <
typename Function>
405#ifdef GREM_USE_MULTITHREADING
408 for (std::size_t i = W; i-- > 0;) {
409 function(ringBuffer[(currentWriteIndex - 1 - i) & (N - 1)]);
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)]);
420 template <
typename U>
427#ifdef GREM_USE_MULTITHREADING
429 alignas(64) Atomic<std::size_t> writeIndex{};
431 std::size_t readIndex = 0;
432 std::size_t writeIndex = 0;
#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
std::function< Signature > Function
Definition Function.hpp:14
@ ACQUIRE
Definition synchronization.hpp:42
@ RELAXED
Definition synchronization.hpp:40
@ RELEASE
Definition synchronization.hpp:43