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