49 bool Init(uint32_t min_capacity) noexcept {
55 while (cap < min_capacity) {
58 slots_.reset(
new (std::nothrow) T[cap]);
72 head_.store(0U, std::memory_order_relaxed);
73 tail_.store(0U, std::memory_order_relaxed);
74 overruns_.store(0U, std::memory_order_relaxed);
75 peak_.store(0U, std::memory_order_relaxed);
97 const uint32_t head =
head_.load(std::memory_order_relaxed);
98 const uint32_t tail =
tail_.load(std::memory_order_acquire);
99 const uint32_t used = head - tail;
101 overruns_.fetch_add(1U, std::memory_order_relaxed);
105 head_.store(head + 1U, std::memory_order_release);
106 const uint32_t now_used = used + 1U;
107 if (now_used >
peak_.load(std::memory_order_relaxed)) {
108 peak_.store(now_used, std::memory_order_relaxed);
121 const uint32_t tail =
tail_.load(std::memory_order_relaxed);
122 const uint32_t head =
head_.load(std::memory_order_acquire);
127 tail_.store(tail + 1U, std::memory_order_release);
135 tail_.store(
head_.load(std::memory_order_acquire), std::memory_order_release);
139 uint32_t
Size() const noexcept {
140 return head_.load(std::memory_order_acquire) -
tail_.load(std::memory_order_acquire);
150 return overruns_.load(std::memory_order_relaxed);
154 uint32_t
Peak() const noexcept {
155 return peak_.load(std::memory_order_relaxed);
160 overruns_.store(0U, std::memory_order_relaxed);
161 peak_.store(0U, std::memory_order_relaxed);
Definition HfSpscRing.h:30
bool Empty() const noexcept
True when nothing is queued (snapshot).
Definition HfSpscRing.h:144
bool TryPush(const T &item) noexcept
Producer side: append a copy of item.
Definition HfSpscRing.h:93
void Release() noexcept
Free the storage. Not thread-safe.
Definition HfSpscRing.h:68
std::atomic< uint32_t > peak_
highest fill level
Definition HfSpscRing.h:171
uint32_t capacity_
Definition HfSpscRing.h:166
std::atomic< uint32_t > head_
next slot to write (producer owned)
Definition HfSpscRing.h:168
uint32_t mask_
Definition HfSpscRing.h:167
uint32_t Peak() const noexcept
Highest fill level seen, since Init() or ResetCounters().
Definition HfSpscRing.h:154
bool Init(uint32_t min_capacity) noexcept
Allocate storage for at least min_capacity elements.
Definition HfSpscRing.h:49
uint32_t Size() const noexcept
Elements queued now (a snapshot when the other side is running).
Definition HfSpscRing.h:139
std::unique_ptr< T[]> slots_
Definition HfSpscRing.h:165
void ResetCounters() noexcept
Zero the overrun and peak counters (the queued elements stay).
Definition HfSpscRing.h:159
uint32_t Overruns() const noexcept
Pushes refused because the ring was full, since Init() or ResetCounters().
Definition HfSpscRing.h:149
bool TryPop(T &out) noexcept
Consumer side: remove the oldest element into out.
Definition HfSpscRing.h:117
void Clear() noexcept
Consumer side: drop every queued element.
Definition HfSpscRing.h:134
static constexpr uint32_t kMaxCapacity
Largest capacity Init() accepts.
Definition HfSpscRing.h:33
std::atomic< uint32_t > tail_
next slot to read (consumer owned)
Definition HfSpscRing.h:169
uint32_t Capacity() const noexcept
Number of elements the ring can hold (a power of two, 0 before Init()).
Definition HfSpscRing.h:84
std::atomic< uint32_t > overruns_
refused pushes
Definition HfSpscRing.h:170
HfSpscRing() noexcept=default
bool IsReady() const noexcept
True after a successful Init().
Definition HfSpscRing.h:79