Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
spsc_queue.hpp
Go to the documentation of this file.
1
3#pragma once
4
5#include <atomic>
6#include <cstddef>
7#include <limits>
8#include <optional>
9#include <stdexcept>
10#include <utility>
11#include <vector>
12
13namespace aegis2d::core {
14
27template <typename T>
28// head_ and tail_ are padded to their own cache lines on purpose, against false sharing.
29class SpscQueue { // NOLINT(clang-analyzer-optin.performance.Padding)
30public:
34 explicit SpscQueue(std::size_t capacity) : slots_(capacity) {
35 if (capacity == 0) {
36 throw std::invalid_argument{"SpscQueue: the capacity must be at least 1"};
37 }
38 }
39
40 SpscQueue(const SpscQueue&) = delete;
41 SpscQueue& operator=(const SpscQueue&) = delete;
42 SpscQueue(SpscQueue&&) = delete;
45 ~SpscQueue() = default;
46
50 template <typename... Args>
51 [[nodiscard]] bool try_emplace(Args&&... args) {
52 const std::size_t tail = tail_.load(std::memory_order_relaxed);
53 // Acquire: the consumer has finished moving out of the slot it released.
54 if (tail - head_.load(std::memory_order_acquire) == slots_.size()) {
55 return false;
56 }
57 slots_.at(tail % slots_.size()).emplace(std::forward<Args>(args)...);
58 // Release: the consumer sees the value before it sees the new tail.
59 tail_.store(tail + 1, std::memory_order_release);
60 return true;
61 }
62
66 [[nodiscard]] bool try_push(T&& value) { return try_emplace(std::move(value)); }
67
71 [[nodiscard]] bool try_push(const T& value) { return try_emplace(value); }
72
75 [[nodiscard]] std::optional<T> try_pop() {
76 const std::size_t head = head_.load(std::memory_order_relaxed);
77 // Acquire: the value written by the producer is visible.
78 if (head == tail_.load(std::memory_order_acquire)) {
79 return std::nullopt;
80 }
81 std::optional<T>& slot = slots_.at(head % slots_.size());
82 std::optional<T> value{std::move(slot)};
83 slot.reset();
84 // Release: the producer may reuse the slot once it sees the new head.
85 head_.store(head + 1, std::memory_order_release);
86 return value;
87 }
88
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();
102 if (!value) {
103 break;
104 }
105 ++taken;
106 consume(std::move(*value));
107 }
108 return taken;
109 }
110
113 [[nodiscard]] std::size_t capacity() const noexcept { return slots_.size(); }
114
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;
120 }
121
124 [[nodiscard]] bool empty() const noexcept { return size() == 0; }
125
126private:
127 // Separate cache lines: the producer writes tail_, the consumer head_, and neither
128 // slows the other down by invalidating its line (false sharing).
129 static constexpr std::size_t cache_line = 64;
130
131 std::vector<std::optional<T>> slots_;
132 // Counters that only grow; a slot is counter % capacity. On 64 bits they never wrap
133 // in practice, and the subtraction stays right even if they did.
134 alignas(cache_line) std::atomic<std::size_t> head_{0};
135 alignas(cache_line) std::atomic<std::size_t> tail_{0};
136};
137
138} // namespace aegis2d::core
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