36 throw std::invalid_argument{
"SpscQueue: the capacity must be at least 1"};
50 template <
typename... Args>
52 const std::size_t tail = tail_.load(std::memory_order_relaxed);
54 if (tail - head_.load(std::memory_order_acquire) == slots_.size()) {
57 slots_.at(tail % slots_.size()).emplace(std::forward<Args>(args)...);
59 tail_.store(tail + 1, std::memory_order_release);
75 [[nodiscard]] std::optional<T>
try_pop() {
76 const std::size_t head = head_.load(std::memory_order_relaxed);
78 if (head == tail_.load(std::memory_order_acquire)) {
81 std::optional<T>& slot = slots_.at(head % slots_.size());
82 std::optional<T> value{std::move(slot)};
85 head_.store(head + 1, std::memory_order_release);
96 template <
typename Consume>
97 std::size_t
drain(Consume consume,
98 std::size_t limit = std::numeric_limits<std::size_t>::max()) {
99 std::size_t taken = 0;
100 while (taken < limit) {
101 std::optional<T> value =
try_pop();
106 consume(std::move(*value));
113 [[nodiscard]] std::size_t
capacity() const noexcept {
return slots_.size(); }
117 [[nodiscard]] std::size_t
size() const noexcept {
118 const std::size_t head = head_.load(std::memory_order_acquire);
119 return tail_.load(std::memory_order_acquire) - head;
124 [[nodiscard]]
bool empty() const noexcept {
return size() == 0; }
129 static constexpr std::size_t cache_line = 64;
131 std::vector<std::optional<T>> slots_;
134 alignas(cache_line) std::atomic<std::size_t> head_{0};
135 alignas(cache_line) std::atomic<std::size_t> tail_{0};
Fixed-capacity FIFO queue passing values from one thread to another without locks.
Definition spsc_queue.hpp:29
SpscQueue & operator=(const SpscQueue &)=delete
SpscQueue(const SpscQueue &)=delete
bool try_emplace(Args &&... args)
Builds a value at the back of the queue. Producer only.
Definition spsc_queue.hpp:51
bool try_push(const T &value)
Adds a copy of a value at the back of the queue. Producer only.
Definition spsc_queue.hpp:71
SpscQueue(SpscQueue &&)=delete
~SpscQueue()=default
Destroys the values still queued.
std::optional< T > try_pop()
Takes the value at the front of the queue. Consumer only.
Definition spsc_queue.hpp:75
SpscQueue & operator=(SpscQueue &&)=delete
std::size_t size() const noexcept
Number of values queued, as seen at the time of the call.
Definition spsc_queue.hpp:117
SpscQueue(std::size_t capacity)
Creates an empty queue.
Definition spsc_queue.hpp:34
bool empty() const noexcept
Tells whether the queue was empty at the time of the call.
Definition spsc_queue.hpp:124
std::size_t capacity() const noexcept
Largest number of values the queue holds at once.
Definition spsc_queue.hpp:113
std::size_t drain(Consume consume, std::size_t limit=std::numeric_limits< std::size_t >::max())
Takes values from the front of the queue and hands each to consume. Consumer only.
Definition spsc_queue.hpp:97
bool try_push(T &&value)
Adds a value at the back of the queue. Producer only.
Definition spsc_queue.hpp:66
Logging, time, main loop, events and configuration shared by client and server.
Definition clock.hpp:11