Library of Assembled Shared Sources
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-2021 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#if defined(__cpp_lib_atomic_is_always_lock_free)
60 static_assert(std::atomic<size_t>::is_always_lock_free);
61#else
62 LASS_ENFORCE(head_.is_lock_free());
63 LASS_ENFORCE(tail_.is_lock_free());
64#endif
65}
66
67
68
69template <typename T>
70lock_free_spsc_ring_buffer<T>::~lock_free_spsc_ring_buffer()
71{
72}
73
74
75
76/** Try to push a value x on the front.
77 * @return false if buffer was full and x could not be pushed.
78 */
79template <typename T>
80bool lock_free_spsc_ring_buffer<T>::try_push(const value_type& x)
81{
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);
85 if (new_head == tail)
86 {
87 return false;
88 }
89 ring_[head] = x;
90 head_.store(new_head, std::memory_order_release);
91 return true;
92}
93
94
95
96/** Try to push a value x on the front.
97 * @return false if buffer was full and x could not be pushed.
98 */
99template <typename T>
100bool lock_free_spsc_ring_buffer<T>::try_push(value_type&& x)
101{
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)
106 {
107 return false;
108 }
109 ring_[head] = std::move(x);
110 head_.store(new_head, std::memory_order_release);
111 return true;
112}
113
114
115
116/** Try to push a value x on the front.
117 * @return false if buffer was full and x could not be pushed.
118 */
119template <typename T>
120template <typename... Args>
121bool lock_free_spsc_ring_buffer<T>::try_emplace(Args&&... args)
122{
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)
127 {
128 return false;
129 }
130 ring_[head] = value_type(std::forward<Args...>(args...));
131 head_.store(new_head, std::memory_order_release);
132 return true;
133}
134
135
136
137/** Try to pop a value from the back and store it in x.
138 * @return false if buffer was empty and no element could be popped.
139 */
140template <typename T>
141bool lock_free_spsc_ring_buffer<T>::try_pop(value_type& x)
142{
143 const size_t head = head_.load(std::memory_order_acquire);
144 const size_t tail = tail_.load(std::memory_order_relaxed);
145 if (tail == head)
146 {
147 return false;
148 }
149 x = std::move(ring_[tail]);
150 tail_.store(next_index(tail), std::memory_order_release);
151 return true;
152}
153
154
155
156/** Return true if ring buffer is empty
157 */
158template <typename T>
159bool lock_free_spsc_ring_buffer<T>::empty() const
160{
161 const size_t head = head_.load(std::memory_order_acquire);
162 const size_t tail = tail_.load(std::memory_order_acquire);
163 return head == tail;
164}
165
166
167
168/** Return true if ring buffer is empty
169 */
170template <typename T>
171bool lock_free_spsc_ring_buffer<T>::full() const
172{
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;
176}
177
178
179// --- private -------------------------------------------------------------------------------------
180
181template <typename T>
182size_t lock_free_spsc_ring_buffer<T>::next_index(size_t index) const
183{
184 ++index;
185 return index < ring_size_ ? index : 0;
186}
187
188
189}
190
191}
192
193// EOF
lass extensions to the standard library
Library for Assembled Shared Sources.
Definition config.h:53