[SM91] Update to Spidermonkey 91.1.3 APIs
[0ad.git] / libraries / source / spidermonkey / include-win32-release / mozilla / SPSCQueue.h
bloba4592c0b0f5a77b11845c8c5c79d7df85c4e4323
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"
15 #include <algorithm>
16 #include <atomic>
17 #include <cstddef>
18 #include <limits>
19 #include <memory>
20 #include <thread>
21 #include <type_traits>
23 namespace mozilla {
25 namespace detail {
26 template <typename T, bool IsPod = std::is_trivial<T>::value>
27 struct MemoryOperations {
28 /**
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);
33 /**
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);
42 template <typename T>
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);
52 template <typename T>
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);
63 } // namespace detail
65 /**
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
71 * another thread.
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
85 * other thread.
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.
93 template <typename T>
94 class SPSCRingBufferBase {
95 public:
96 /**
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)
105 : mReadIndex(0),
106 mWriteIndex(0)
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::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) {
151 #ifdef DEBUG
152 AssertCorrectThread(mProducerId);
153 #endif
155 int rdIdx = mReadIndex.load(std::memory_order::memory_order_acquire);
156 int wrIdx = mWriteIndex.load(std::memory_order::memory_order_relaxed);
158 if (IsFull(rdIdx, wrIdx)) {
159 return 0;
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;
169 if (aElements) {
170 detail::MemoryOperations<T>::MoveOrCopy(mData.get() + wrIdx, aElements,
171 firstPart);
172 detail::MemoryOperations<T>::MoveOrCopy(
173 mData.get(), aElements + firstPart, secondPart);
174 } else {
175 detail::MemoryOperations<T>::ConstructDefault(mData.get() + wrIdx,
176 firstPart);
177 detail::MemoryOperations<T>::ConstructDefault(mData.get(), secondPart);
180 mWriteIndex.store(IncrementIndex(wrIdx, toWrite),
181 std::memory_order::memory_order_release);
183 return toWrite;
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) {
197 #ifdef DEBUG
198 AssertCorrectThread(mConsumerId);
199 #endif
201 int wrIdx = mWriteIndex.load(std::memory_order::memory_order_acquire);
202 int rdIdx = mReadIndex.load(std::memory_order::memory_order_relaxed);
204 if (IsEmpty(rdIdx, wrIdx)) {
205 return 0;
208 int toRead = std::min(AvailableReadInternal(rdIdx, wrIdx), count);
210 int firstPart = std::min(StorageCapacity() - rdIdx, toRead);
211 int secondPart = toRead - firstPart;
213 if (elements) {
214 detail::MemoryOperations<T>::MoveOrCopy(elements, mData.get() + rdIdx,
215 firstPart);
216 detail::MemoryOperations<T>::MoveOrCopy(elements + firstPart, mData.get(),
217 secondPart);
220 mReadIndex.store(IncrementIndex(rdIdx, toRead),
221 std::memory_order::memory_order_release);
223 return toRead;
226 * Get the number of available elements for consuming.
228 * Only safely called on the consumer thread. This can be less than the actual
229 * number of elements in the queue, since the mWriteIndex is updated at the
230 * very end of the Enqueue method on the producer thread, but consequently
231 * always returns a number of elements such that a call to Dequeue return this
232 * number of elements.
234 * @return The number of available elements for reading.
236 int AvailableRead() const {
237 #ifdef DEBUG
238 AssertCorrectThread(mConsumerId);
239 #endif
240 return AvailableReadInternal(
241 mReadIndex.load(std::memory_order::memory_order_relaxed),
242 mWriteIndex.load(std::memory_order::memory_order_relaxed));
245 * Get the number of available elements for writing.
247 * Only safely called on the producer thread. This can be less than than the
248 * actual number of slots that are available, because mReadIndex is update at
249 * the very end of the Deque method. It always returns a number such that a
250 * call to Enqueue with this number will succeed in enqueuing this number of
251 * elements.
253 * @return The number of empty slots in the buffer, available for writing.
255 int AvailableWrite() const {
256 #ifdef DEBUG
257 AssertCorrectThread(mProducerId);
258 #endif
259 return AvailableWriteInternal(
260 mReadIndex.load(std::memory_order::memory_order_relaxed),
261 mWriteIndex.load(std::memory_order::memory_order_relaxed));
264 * Get the total Capacity, for this ring buffer.
266 * Can be called safely on any thread.
268 * @return The maximum Capacity of this ring buffer.
270 int Capacity() const { return StorageCapacity() - 1; }
272 * Reset the consumer and producer thread identifier, in case the threads are
273 * being changed. This has to be externally synchronized. This is no-op when
274 * asserts are disabled.
276 void ResetThreadIds() {
277 #ifdef DEBUG
278 mConsumerId = mProducerId = std::thread::id();
279 #endif
282 private:
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
297 * index.
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;
328 } else {
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();
347 return rv;
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.
373 #ifdef DEBUG
374 static void AssertCorrectThread(std::thread::id& aId) {
375 if (aId == std::thread::id()) {
376 aId = std::this_thread::get_id();
377 return;
379 MOZ_ASSERT(aId == std::this_thread::get_id());
381 #endif
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. */
388 const int mCapacity;
389 /** Data storage, of size `mCapacity + 1` */
390 std::unique_ptr<T[]> mData;
391 #ifdef DEBUG
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;
396 #endif
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