| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include <atomic> |
| 10 |
#include <cassert> |
| 11 |
|
| 12 |
#include "Common/TypeUtils.h" |
| 13 |
|
| 14 |
namespace Common |
| 15 |
{ |
| 16 |
|
| 17 |
namespace detail |
| 18 |
{ |
| 19 |
template <typename T, bool IncludeWaitFunctionality> |
| 20 |
class SPSCQueueBase final |
| 21 |
{ |
| 22 |
public: |
| 23 |
SPSCQueueBase() = default; |
| 24 |
~SPSCQueueBase() |
| 25 |
{ |
| 26 |
Clear(); |
| 27 |
delete m_read_ptr; |
| 28 |
} |
| 29 |
|
| 30 |
SPSCQueueBase(const SPSCQueueBase&) = delete; |
| 31 |
SPSCQueueBase& operator=(const SPSCQueueBase&) = delete; |
| 32 |
|
| 33 |
std::size_t Size() const { return m_size.load(std::memory_order_acquire); } |
| 34 |
bool Empty() const { return Size() == 0; } |
| 35 |
|
| 36 |
|
| 37 |
void Push(const T& arg) { Emplace(arg); } |
| 38 |
void Push(T&& arg) { Emplace(std::move(arg)); } |
| 39 |
template <typename... Args> |
| 40 |
void Emplace(Args&&... args) |
| 41 |
{ |
| 42 |
m_write_ptr->value.Construct(std::forward<Args>(args)...); |
| 43 |
|
| 44 |
Node* const new_ptr = new Node; |
| 45 |
m_write_ptr->next = new_ptr; |
| 46 |
m_write_ptr = new_ptr; |
| 47 |
|
| 48 |
AdjustSize(1); |
| 49 |
} |
| 50 |
|
| 51 |
void WaitForEmpty() requires(IncludeWaitFunctionality) |
| 52 |
{ |
| 53 |
while (const std::size_t old_size = Size()) |
| 54 |
m_size.wait(old_size, std::memory_order_acquire); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
T& Front() { return m_read_ptr->value.Ref(); } |
| 59 |
const T& Front() const { return m_read_ptr->value.Ref(); } |
| 60 |
|
| 61 |
void Pop() |
| 62 |
{ |
| 63 |
assert(!Empty()); |
| 64 |
|
| 65 |
m_read_ptr->value.Destroy(); |
| 66 |
|
| 67 |
Node* const old_node = m_read_ptr; |
| 68 |
m_read_ptr = old_node->next; |
| 69 |
delete old_node; |
| 70 |
|
| 71 |
AdjustSize(-1); |
| 72 |
} |
| 73 |
|
| 74 |
bool Pop(T& result) |
| 75 |
{ |
| 76 |
if (Empty()) |
| 77 |
return false; |
| 78 |
|
| 79 |
result = std::move(Front()); |
| 80 |
Pop(); |
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
void WaitForData() requires(IncludeWaitFunctionality) |
| 85 |
{ |
| 86 |
m_size.wait(0, std::memory_order_acquire); |
| 87 |
} |
| 88 |
|
| 89 |
void Clear() |
| 90 |
{ |
| 91 |
while (!Empty()) |
| 92 |
Pop(); |
| 93 |
} |
| 94 |
|
| 95 |
private: |
| 96 |
struct Node |
| 97 |
{ |
| 98 |
ManuallyConstructedValue<T> value; |
| 99 |
Node* next; |
| 100 |
}; |
| 101 |
|
| 102 |
Node* m_write_ptr = new Node; |
| 103 |
Node* m_read_ptr = m_write_ptr; |
| 104 |
|
| 105 |
void AdjustSize(std::size_t value) |
| 106 |
{ |
| 107 |
m_size.fetch_add(value, std::memory_order_release); |
| 108 |
if constexpr (IncludeWaitFunctionality) |
| 109 |
m_size.notify_one(); |
| 110 |
} |
| 111 |
|
| 112 |
std::atomic<std::size_t> m_size = 0; |
| 113 |
}; |
| 114 |
} |
| 115 |
|
| 116 |
template <typename T> |
| 117 |
using SPSCQueue = detail::SPSCQueueBase<T, false>; |
| 118 |
|
| 119 |
template <typename T> |
| 120 |
using WaitableSPSCQueue = detail::SPSCQueueBase<T, true>; |
| 121 |
|
| 122 |
} |