1#ifndef SBL_HW_UTIL_RING_BUFFER_HPP_
2#define SBL_HW_UTIL_RING_BUFFER_HPP_
63#if defined(__arm__) && !defined(__linux__)
64inline void ring_acquire_fence() { std::atomic_signal_fence(std::memory_order_acquire); }
65inline void ring_release_fence() { std::atomic_signal_fence(std::memory_order_release); }
73template<
typename T, u
int32_t Size>
75 static_assert((Size & (Size - 1)) == 0,
"Size must be power of 2");
76 static_assert(Size > 1,
"Size must be greater than 1");
77 static_assert(Size <= 65536,
"Size must fit in practical memory constraints");
78 static_assert(std::atomic<uint32_t>::is_always_lock_free,
79 "RingBuffer needs lock-free 32-bit atomics");
90 const uint32_t head = head_.load(std::memory_order_relaxed);
91 const uint32_t next = (head + 1) & kMask;
92 if (next == tail_.load(std::memory_order_relaxed)) {
98 head_.store(next, std::memory_order_relaxed);
108 const uint32_t tail = tail_.load(std::memory_order_relaxed);
109 if (tail == head_.load(std::memory_order_relaxed)) {
113 item = buffer_[tail];
115 tail_.store((tail + 1) & kMask, std::memory_order_relaxed);
120 return head_.load(std::memory_order_relaxed) ==
121 tail_.load(std::memory_order_relaxed);
125 return ((head_.load(std::memory_order_relaxed) + 1) & kMask) ==
126 tail_.load(std::memory_order_relaxed);
131 return (head_.load(std::memory_order_relaxed) -
132 tail_.load(std::memory_order_relaxed)) & kMask;
135 static constexpr uint32_t
capacity() {
return Size - 1; }
139 head_.store(0, std::memory_order_relaxed);
140 tail_.store(0, std::memory_order_relaxed);
144 static constexpr uint32_t kMask = Size - 1;
146 T buffer_[Size] = {};
147 std::atomic<uint32_t> head_{0};
148 std::atomic<uint32_t> tail_{0};
void clear()
Empty the buffer. Only while neither producer nor consumer runs.
uint32_t size() const
Element count — a snapshot, may change immediately after the call.
bool push(const T &item)
Push an element (producer side)
bool pop(T &item)
Pop the oldest element (consumer side)
static constexpr uint32_t capacity()
void ring_acquire_fence()
void ring_release_fence()
Root namespace for all Sound Byte Libs code.