4#ifndef GREM_CORE_DATA_MPMC_QUEUE_HPP
5#define GREM_CORE_DATA_MPMC_QUEUE_HPP
11#ifdef GREM_USE_MULTITHREADING
23template <
typename T, std::
size_t N>
26 static_assert(N >= 2 && (N & (N - 1)) == 0,
"MPMCQueue buffer size must be a positive power of 2!");
28 template <
typename Function>
30#ifdef GREM_USE_MULTITHREADING
32 const std::size_t writeGeneration = (currentWriteIndex / N) * 2;
33 Storage& storage = ringBuffer[currentWriteIndex & (N - 1)];
36 std::forward<Function>(function)(storage.value);
40 const std::size_t currentWriteIndex = writeIndex++;
41 T& value = ringBuffer[currentWriteIndex & (N - 1)];
42 std::forward<Function>(function)(value);
47 void waitEnqueue(U&& value)
requires(std::is_nothrow_assignable_v<T&, decltype(std::forward<U>(value))>) {
48 waitEnqueueWith([&value](T& output)
noexcept ->
void { output = std::forward<U>(value); });
51 template <
typename Function>
53#ifdef GREM_USE_MULTITHREADING
56 const std::size_t writeGeneration = (currentWriteIndex / N) * 2;
57 Storage& storage = ringBuffer[currentWriteIndex & (N - 1)];
59 if (writeIndex.compare_exchange_strong(currentWriteIndex, currentWriteIndex + 1,
MemoryOrder::ACQUIRE)) {
60 std::forward<Function>(function)(storage.value);
66 if (nextWriteIndex == currentWriteIndex) {
69 currentWriteIndex = nextWriteIndex;
74 if (writeIndex - readIndex >= N) {
77 std::size_t currentWriteIndex = writeIndex++;
78 T& value = ringBuffer[currentWriteIndex & (N - 1)];
79 std::forward<Function>(function)(value);
85 [[nodiscard]]
bool tryEnqueue(U&& value)
requires(std::is_nothrow_assignable_v<T&, decltype(std::forward<U>(value))>) {
86 return tryEnqueueWith([&value](T& output)
noexcept ->
void { output = std::forward<U>(value); });
90 void waitDequeue(U& value)
requires(std::is_nothrow_assignable_v<U&, T &&>) {
91#ifdef GREM_USE_MULTITHREADING
93 const std::size_t readGeneration = (currentReadIndex / N) * 2 + 1;
94 Storage& storage = ringBuffer[currentReadIndex & (N - 1)];
97 value = std::move(storage.value);
101 const std::size_t currentReadIndex = readIndex++;
102 value = std::move(ringBuffer[currentReadIndex & (N - 1)]);
106 template <
typename U>
107 [[nodiscard]]
bool tryDequeue(U& value)
requires(std::is_nothrow_assignable_v<U&, T &&>) {
108#ifdef GREM_USE_MULTITHREADING
111 const std::size_t readGeneration = (currentReadIndex / N) * 2 + 1;
112 Storage& storage = ringBuffer[currentReadIndex & (N - 1)];
114 if (readIndex.compare_exchange_strong(currentReadIndex, currentReadIndex + 1,
MemoryOrder::ACQUIRE)) {
115 value = std::move(storage.value);
121 if (nextReadIndex == currentReadIndex) {
124 currentReadIndex = nextReadIndex;
128 if (readIndex >= writeIndex) {
131 std::size_t currentReadIndex = readIndex++;
132 value = std::move(ringBuffer[currentReadIndex & (N - 1)]);
138#ifdef GREM_USE_MULTITHREADING
141 return writeIndex - readIndex;
146#ifdef GREM_USE_MULTITHREADING
147 struct alignas(std::max(std::size_t{64}, alignof(T))) Storage {
153 alignas(64) Atomic<std::size_t> readIndex{};
154 alignas(64) Atomic<std::size_t> writeIndex{};
157 std::size_t readIndex = 0;
158 std::size_t writeIndex = 0;
#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
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