1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Single producer single consumer lock-free and wait-free queue. */
9 #ifndef mozilla_LockFreeQueue_h
10 #define mozilla_LockFreeQueue_h
12 #include "mozilla/Assertions.h"
13 #include "mozilla/Attributes.h"
14 #include "mozilla/PodOperations.h"
21 #include <type_traits>
26 template <typename T
, bool IsPod
= std::is_trivial
<T
>::value
>
27 struct MemoryOperations
{
29 * This allows zeroing (using memset) or default-constructing a number of
30 * elements calling the constructors if necessary.
32 static void ConstructDefault(T
* aDestination
, size_t aCount
);
34 * This allows either moving (if T supports it) or copying a number of
35 * elements from a `aSource` pointer to a `aDestination` pointer.
36 * If it is safe to do so and this call copies, this uses PodCopy. Otherwise,
37 * constructors and destructors are called in a loop.
39 static void MoveOrCopy(T
* aDestination
, T
* aSource
, size_t aCount
);
43 struct MemoryOperations
<T
, true> {
44 static void ConstructDefault(T
* aDestination
, size_t aCount
) {
45 PodZero(aDestination
, aCount
);
47 static void MoveOrCopy(T
* aDestination
, T
* aSource
, size_t aCount
) {
48 PodCopy(aDestination
, aSource
, aCount
);
53 struct MemoryOperations
<T
, false> {
54 static void ConstructDefault(T
* aDestination
, size_t aCount
) {
55 for (size_t i
= 0; i
< aCount
; i
++) {
56 aDestination
[i
] = T();
59 static void MoveOrCopy(T
* aDestination
, T
* aSource
, size_t aCount
) {
60 std::move(aSource
, aSource
+ aCount
, aDestination
);
66 * This data structure allows producing data from one thread, and consuming it
67 * on another thread, safely and without explicit synchronization.
69 * The role for the producer and the consumer must be constant, i.e., the
70 * producer should always be on one thread and the consumer should always be on
73 * Some words about the inner workings of this class:
74 * - Capacity is fixed. Only one allocation is performed, in the constructor.
75 * When reading and writing, the return value of the method allows checking if
76 * the ring buffer is empty or full.
77 * - We always keep the read index at least one element ahead of the write
78 * index, so we can distinguish between an empty and a full ring buffer: an
79 * empty ring buffer is when the write index is at the same position as the
80 * read index. A full buffer is when the write index is exactly one position
81 * before the read index.
82 * - We synchronize updates to the read index after having read the data, and
83 * the write index after having written the data. This means that the each
84 * thread can only touch a portion of the buffer that is not touched by the
86 * - Callers are expected to provide buffers. When writing to the queue,
87 * elements are copied into the internal storage from the buffer passed in.
88 * When reading from the queue, the user is expected to provide a buffer.
89 * Because this is a ring buffer, data might not be contiguous in memory;
90 * providing an external buffer to copy into is an easy way to have linear
91 * data for further processing.
94 class SPSCRingBufferBase
{
97 * Constructor for a ring buffer.
99 * This performs an allocation on the heap, but is the only allocation that
100 * will happen for the life time of a `SPSCRingBufferBase`.
102 * @param Capacity The maximum number of element this ring buffer will hold.
104 explicit SPSCRingBufferBase(int aCapacity
)
107 /* One more element to distinguish from empty and full buffer. */
109 mCapacity(aCapacity
+ 1) {
110 MOZ_ASSERT(StorageCapacity() < std::numeric_limits
<int>::max() / 2,
111 "buffer too large for the type of index used.");
112 MOZ_ASSERT(mCapacity
> 0 && aCapacity
!= std::numeric_limits
<int>::max());
114 mData
= std::make_unique
<T
[]>(StorageCapacity());
116 std::atomic_thread_fence(std::memory_order_seq_cst
);
119 * Push `aCount` zero or default constructed elements in the array.
121 * Only safely called on the producer thread.
123 * @param count The number of elements to enqueue.
124 * @return The number of element enqueued.
126 [[nodiscard
]] int EnqueueDefault(int aCount
) {
127 return Enqueue(nullptr, aCount
);
130 * @brief Put an element in the queue.
132 * Only safely called on the producer thread.
134 * @param element The element to put in the queue.
136 * @return 1 if the element was inserted, 0 otherwise.
138 [[nodiscard
]] int Enqueue(T
& aElement
) { return Enqueue(&aElement
, 1); }
140 * Push `aCount` elements in the ring buffer.
142 * Only safely called on the producer thread.
144 * @param elements a pointer to a buffer containing at least `count` elements.
145 * If `elements` is nullptr, zero or default constructed elements are enqueud.
146 * @param count The number of elements to read from `elements`
147 * @return The number of elements successfully coped from `elements` and
148 * inserted into the ring buffer.
150 [[nodiscard
]] int Enqueue(T
* aElements
, int aCount
) {
152 AssertCorrectThread(mProducerId
);
155 int rdIdx
= mReadIndex
.load(std::memory_order_acquire
);
156 int wrIdx
= mWriteIndex
.load(std::memory_order_relaxed
);
158 if (IsFull(rdIdx
, wrIdx
)) {
162 int toWrite
= std::min(AvailableWriteInternal(rdIdx
, wrIdx
), aCount
);
164 /* First part, from the write index to the end of the array. */
165 int firstPart
= std::min(StorageCapacity() - wrIdx
, toWrite
);
166 /* Second part, from the beginning of the array */
167 int secondPart
= toWrite
- firstPart
;
170 detail::MemoryOperations
<T
>::MoveOrCopy(mData
.get() + wrIdx
, aElements
,
172 detail::MemoryOperations
<T
>::MoveOrCopy(
173 mData
.get(), aElements
+ firstPart
, secondPart
);
175 detail::MemoryOperations
<T
>::ConstructDefault(mData
.get() + wrIdx
,
177 detail::MemoryOperations
<T
>::ConstructDefault(mData
.get(), secondPart
);
180 mWriteIndex
.store(IncrementIndex(wrIdx
, toWrite
),
181 std::memory_order_release
);
186 * Retrieve at most `count` elements from the ring buffer, and copy them to
187 * `elements`, if non-null.
189 * Only safely called on the consumer side.
191 * @param elements A pointer to a buffer with space for at least `count`
192 * elements. If `elements` is `nullptr`, `count` element will be discarded.
193 * @param count The maximum number of elements to Dequeue.
194 * @return The number of elements written to `elements`.
196 [[nodiscard
]] int Dequeue(T
* elements
, int count
) {
198 AssertCorrectThread(mConsumerId
);
201 int wrIdx
= mWriteIndex
.load(std::memory_order_acquire
);
202 int rdIdx
= mReadIndex
.load(std::memory_order_relaxed
);
204 if (IsEmpty(rdIdx
, wrIdx
)) {
208 int toRead
= std::min(AvailableReadInternal(rdIdx
, wrIdx
), count
);
210 int firstPart
= std::min(StorageCapacity() - rdIdx
, toRead
);
211 int secondPart
= toRead
- firstPart
;
214 detail::MemoryOperations
<T
>::MoveOrCopy(elements
, mData
.get() + rdIdx
,
216 detail::MemoryOperations
<T
>::MoveOrCopy(elements
+ firstPart
, mData
.get(),
220 mReadIndex
.store(IncrementIndex(rdIdx
, toRead
), std::memory_order_release
);
225 * Get the number of available elements for consuming.
227 * This can be less than the actual number of elements in the queue, since the
228 * mWriteIndex is updated at the very end of the Enqueue method on the
229 * producer thread, but consequently always returns a number of elements such
230 * that a call to Dequeue return this number of elements.
232 * @return The number of available elements for reading.
234 int AvailableRead() const {
235 return AvailableReadInternal(mReadIndex
.load(std::memory_order_relaxed
),
236 mWriteIndex
.load(std::memory_order_relaxed
));
239 * Get the number of available elements for writing.
241 * This can be less than than the actual number of slots that are available,
242 * because mReadIndex is updated at the very end of the Deque method. It
243 * always returns a number such that a call to Enqueue with this number will
244 * succeed in enqueuing this number of elements.
246 * @return The number of empty slots in the buffer, available for writing.
248 int AvailableWrite() const {
249 return AvailableWriteInternal(mReadIndex
.load(std::memory_order_relaxed
),
250 mWriteIndex
.load(std::memory_order_relaxed
));
253 * Get the total Capacity, for this ring buffer.
255 * Can be called safely on any thread.
257 * @return The maximum Capacity of this ring buffer.
259 int Capacity() const { return StorageCapacity() - 1; }
261 * Reset the consumer and producer thread identifier, in case the threads are
262 * being changed. This has to be externally synchronized. This is no-op when
263 * asserts are disabled.
265 void ResetThreadIds() {
266 ResetProducerThreadId();
267 ResetConsumerThreadId();
270 void ResetConsumerThreadId() {
272 mConsumerId
= std::thread::id();
276 void ResetProducerThreadId() {
278 mProducerId
= std::thread::id();
283 /** Return true if the ring buffer is empty.
285 * This can be called from the consumer or the producer thread.
287 * @param aReadIndex the read index to consider
288 * @param writeIndex the write index to consider
289 * @return true if the ring buffer is empty, false otherwise.
291 bool IsEmpty(int aReadIndex
, int aWriteIndex
) const {
292 return aWriteIndex
== aReadIndex
;
294 /** Return true if the ring buffer is full.
296 * This happens if the write index is exactly one element behind the read
299 * This can be called from the consummer or the producer thread.
301 * @param aReadIndex the read index to consider
302 * @param writeIndex the write index to consider
303 * @return true if the ring buffer is full, false otherwise.
305 bool IsFull(int aReadIndex
, int aWriteIndex
) const {
306 return (aWriteIndex
+ 1) % StorageCapacity() == aReadIndex
;
309 * Return the size of the storage. It is one more than the number of elements
310 * that can be stored in the buffer.
312 * This can be called from any thread.
314 * @return the number of elements that can be stored in the buffer.
316 int StorageCapacity() const { return mCapacity
; }
318 * Returns the number of elements available for reading.
320 * This can be called from the consummer or producer thread, but see the
321 * comment in `AvailableRead`.
323 * @return the number of available elements for reading.
325 int AvailableReadInternal(int aReadIndex
, int aWriteIndex
) const {
326 if (aWriteIndex
>= aReadIndex
) {
327 return aWriteIndex
- aReadIndex
;
329 return aWriteIndex
+ StorageCapacity() - aReadIndex
;
333 * Returns the number of empty elements, available for writing.
335 * This can be called from the consummer or producer thread, but see the
336 * comment in `AvailableWrite`.
338 * @return the number of elements that can be written into the array.
340 int AvailableWriteInternal(int aReadIndex
, int aWriteIndex
) const {
341 /* We subtract one element here to always keep at least one sample
342 * free in the buffer, to distinguish between full and empty array. */
343 int rv
= aReadIndex
- aWriteIndex
- 1;
344 if (aWriteIndex
>= aReadIndex
) {
345 rv
+= StorageCapacity();
350 * Increments an index, wrapping it around the storage.
352 * Incrementing `mWriteIndex` can be done on the producer thread.
353 * Incrementing `mReadIndex` can be done on the consummer thread.
355 * @param index a reference to the index to increment.
356 * @param increment the number by which `index` is incremented.
357 * @return the new index.
359 int IncrementIndex(int aIndex
, int aIncrement
) const {
360 MOZ_ASSERT(aIncrement
>= 0 && aIncrement
< StorageCapacity() &&
361 aIndex
< StorageCapacity());
362 return (aIndex
+ aIncrement
) % StorageCapacity();
365 * @brief This allows checking that Enqueue (resp. Dequeue) are always
366 * called by the right thread.
368 * The role of the thread are assigned the first time they call Enqueue or
369 * Dequeue, and cannot change, except when ResetThreadIds is called..
371 * @param id the id of the thread that has called the calling method first.
374 static void AssertCorrectThread(std::thread::id
& aId
) {
375 if (aId
== std::thread::id()) {
376 aId
= std::this_thread::get_id();
379 MOZ_ASSERT(aId
== std::this_thread::get_id());
382 /** Index at which the oldest element is. */
383 std::atomic
<int> mReadIndex
;
384 /** Index at which to write new elements. `mWriteIndex` is always at
385 * least one element ahead of `mReadIndex`. */
386 std::atomic
<int> mWriteIndex
;
387 /** Maximum number of elements that can be stored in the ring buffer. */
389 /** Data storage, of size `mCapacity + 1` */
390 std::unique_ptr
<T
[]> mData
;
392 /** The id of the only thread that is allowed to read from the queue. */
393 mutable std::thread::id mConsumerId
;
394 /** The id of the only thread that is allowed to write from the queue. */
395 mutable std::thread::id mProducerId
;
400 * Instantiation of the `SPSCRingBufferBase` type. This is safe to use
401 * from two threads, one producer, one consumer (that never change role),
402 * without explicit synchronization.
404 template <typename T
>
405 using SPSCQueue
= SPSCRingBufferBase
<T
>;
407 } // namespace mozilla
409 #endif // mozilla_LockFreeQueue_h