Library of Assembled Shared Sources
 
Loading...
Searching...
No Matches
lock_free_spsc_ring_buffer.inl
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2026 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
44
45namespace lass
46{
47namespace stde
48{
49
50// --- public --------------------------------------------------------------------------------------
51
52template <typename T>
53lock_free_spsc_ring_buffer<T>::lock_free_spsc_ring_buffer(size_t capacity):
54 head_(0),
55 tail_(0),
56 ring_(capacity + 1),
57 ring_size_(capacity + 1)
58{
59 static_assert(std::atomic<size_t>::is_always_lock_free);
60}
61
62
63
64template <typename T>
65lock_free_spsc_ring_buffer<T>::~lock_free_spsc_ring_buffer()
66{
67}
68
69
70
71/** Try to push a value x on the front.
72 * @return false if buffer was full and x could not be pushed.
73 */
74template <typename T>
75bool lock_free_spsc_ring_buffer<T>::try_push(const value_type& x)
76{
77 const size_t head = head_.load(std::memory_order_relaxed);
78 const size_t tail = tail_.load(std::memory_order_acquire);
79 const size_t new_head = next_index(head);
80 if (new_head == tail)
81 {
82 return false;
83 }
84 ring_[head] = x;
85 head_.store(new_head, std::memory_order_release);
86 return true;
87}
88
89
90
91/** Try to push a value x on the front.
92 * @return false if buffer was full and x could not be pushed.
93 */
94template <typename T>
95bool lock_free_spsc_ring_buffer<T>::try_push(value_type&& x)
96{
97 const size_t head = head_.load(std::memory_order_relaxed);
98 const size_t tail = tail_.load(std::memory_order_acquire);
99 const size_t new_head = next_index(head);
100 if (new_head == tail)
101 {
102 return false;
103 }
104 ring_[head] = std::move(x);
105 head_.store(new_head, std::memory_order_release);
106 return true;
107}
108
109
110
111/** Try to push a value x on the front.
112 * @return false if buffer was full and x could not be pushed.
113 */
114template <typename T>
115template <typename... Args>
116bool lock_free_spsc_ring_buffer<T>::try_emplace(Args&&... args)
117{
118 const size_t head = head_.load(std::memory_order_relaxed);
119 const size_t tail = tail_.load(std::memory_order_acquire);
120 const size_t new_head = next_index(head);
121 if (new_head == tail)
122 {
123 return false;
124 }
125 ring_[head] = value_type(std::forward<Args...>(args...));
126 head_.store(new_head, std::memory_order_release);
127 return true;
128}
129
130
131
132/** Try to pop a value from the back and store it in x.
133 * @return false if buffer was empty and no element could be popped.
134 */
135template <typename T>
136bool lock_free_spsc_ring_buffer<T>::try_pop(value_type& x)
137{
138 const size_t head = head_.load(std::memory_order_acquire);
139 const size_t tail = tail_.load(std::memory_order_relaxed);
140 if (tail == head)
141 {
142 return false;
143 }
144 x = std::move(ring_[tail]);
145 tail_.store(next_index(tail), std::memory_order_release);
146 return true;
147}
148
149
150
151/** Return true if ring buffer is empty
152 */
153template <typename T>
154bool lock_free_spsc_ring_buffer<T>::empty() const
155{
156 const size_t head = head_.load(std::memory_order_acquire);
157 const size_t tail = tail_.load(std::memory_order_acquire);
158 return head == tail;
159}
160
161
162
163/** Return true if ring buffer is empty
164 */
165template <typename T>
166bool lock_free_spsc_ring_buffer<T>::full() const
167{
168 const size_t head = head_.load(std::memory_order_acquire);
169 const size_t tail = tail_.load(std::memory_order_acquire);
170 return next_index(head) == tail;
171}
172
173
174// --- private -------------------------------------------------------------------------------------
175
176template <typename T>
177size_t lock_free_spsc_ring_buffer<T>::next_index(size_t index) const
178{
179 ++index;
180 return index < ring_size_ ? index : 0;
181}
182
183
184}
185
186}
187
188// EOF
lass extensions to the standard library
Library for Assembled Shared Sources.
Definition config.h:53