53lock_free_spsc_ring_buffer<T>::lock_free_spsc_ring_buffer(
size_t capacity):
57 ring_size_(capacity + 1)
59#if defined(__cpp_lib_atomic_is_always_lock_free)
60 static_assert(std::atomic<size_t>::is_always_lock_free);
62 LASS_ENFORCE(head_.is_lock_free());
63 LASS_ENFORCE(tail_.is_lock_free());
70lock_free_spsc_ring_buffer<T>::~lock_free_spsc_ring_buffer()
80bool lock_free_spsc_ring_buffer<T>::try_push(
const value_type& x)
82 const size_t head = head_.load(std::memory_order_relaxed);
83 const size_t tail = tail_.load(std::memory_order_acquire);
84 const size_t new_head = next_index(head);
90 head_.store(new_head, std::memory_order_release);
100bool lock_free_spsc_ring_buffer<T>::try_push(value_type&& x)
102 const size_t head = head_.load(std::memory_order_relaxed);
103 const size_t tail = tail_.load(std::memory_order_acquire);
104 const size_t new_head = next_index(head);
105 if (new_head == tail)
109 ring_[head] = std::move(x);
110 head_.store(new_head, std::memory_order_release);
120template <
typename... Args>
121bool lock_free_spsc_ring_buffer<T>::try_emplace(Args&&... args)
123 const size_t head = head_.load(std::memory_order_relaxed);
124 const size_t tail = tail_.load(std::memory_order_acquire);
125 const size_t new_head = next_index(head);
126 if (new_head == tail)
130 ring_[head] = value_type(std::forward<Args...>(args...));
131 head_.store(new_head, std::memory_order_release);
141bool lock_free_spsc_ring_buffer<T>::try_pop(value_type& x)
143 const size_t head = head_.load(std::memory_order_acquire);
144 const size_t tail = tail_.load(std::memory_order_relaxed);
149 x = std::move(ring_[tail]);
150 tail_.store(next_index(tail), std::memory_order_release);
159bool lock_free_spsc_ring_buffer<T>::empty()
const
161 const size_t head = head_.load(std::memory_order_acquire);
162 const size_t tail = tail_.load(std::memory_order_acquire);
171bool lock_free_spsc_ring_buffer<T>::full()
const
173 const size_t head = head_.load(std::memory_order_acquire);
174 const size_t tail = tail_.load(std::memory_order_acquire);
175 return next_index(head) == tail;
182size_t lock_free_spsc_ring_buffer<T>::next_index(
size_t index)
const
185 return index < ring_size_ ? index : 0;
lass extensions to the standard library
Library for Assembled Shared Sources.