Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TAtomicQuasiQueue.h
Go to the documentation of this file.
1#pragma once
2
3#include <Common/config.h>
4
5#include <moodycamel/concurrentqueue.h>
6
7#include <cstddef>
8#include <iterator>
9
10namespace ph
11{
12
22template<typename T>
24{
25public:
27 explicit TAtomicQuasiQueue(std::size_t initialCapacity);
28
35 template<typename U>
36 void enqueue(U&& item);
37
41 template<std::input_iterator Iterator>
42 void enqueueBulk(Iterator firstItem, std::size_t numItems);
43
48 template<typename U>
49 bool tryEnqueue(U&& item);
50
59 bool tryDequeue(T* out_item);
60
61 template<std::output_iterator<T> Iterator>
62 std::size_t tryDequeueBulk(Iterator out_firstItem, std::size_t numItems);
63
69 std::size_t estimatedSize() const;
70
71private:
72
73#if PH_ENSURE_LOCKFREE_ALGORITHMS_ARE_LOCKLESS
74 static_assert(moodycamel::ConcurrentQueue<T>::is_lock_free());
75#endif
76
77 /*
78 `moodycamel::ConcurrentQueue` is lock free and the queue itself is thread-safe, meaning that using
79 the queue under multiple producers and multiple consumers will not break the internal state of the
80 queue, and all operations generally will not block in any way. However, there are several implications
81 due to its design, the most important ones are:
82
83 * Items enqueued by each thread will come out in the same order as they were enqueued, however, there
84 is no defined item order between threads even with external synchronization. It is FIFO when viewed
85 from any single thread, but not FIFO when viewed across threads. Normally this is not an issue, as if
86 a total item order is desired, you need some sort of synchronization and might as well just queue into
87 a synchronized queue from a single thread.
88 See [1] ConcurrentQueue not a FIFO
89 https://github.com/cameron314/concurrentqueue/issues/52
90 [2] I can't understand how this queue ensure ordering between multiple producer...
91 https://github.com/cameron314/concurrentqueue/issues/262
92 [3] FIFO guarantees for SPMC usage?
93 https://github.com/cameron314/concurrentqueue/issues/309
94
95 * Items enqueued on one thread may not be successfully dequeued in another thread, unless there are
96 external means to ensure the enqueue operation is visible on the dequeue thread.
97 See [1] [SPMC] externally synchronized try_dequeue fails with items in queue
98 https://github.com/cameron314/concurrentqueue/issues/265
99 [2] try_enqueue() returns true but approx_size() is always 0 and try_dequeue() fails
100 https://github.com/cameron314/concurrentqueue/issues/285
101
102 * All the memory effects done by a thread before it enqueues an item will be visible on another
103 thread after it dequeues that item. Basic aquire-release semantics are guaranteed. This ensures
104 that all the effects of work done by a thread before it enqueues an element will be visible on
105 another thread after it dequeues that element.
106 See [1] sequential consistent
107 https://github.com/cameron314/concurrentqueue/issues/73
108
109 Most performant case: single-producer multi-consumer.
110 */
111 moodycamel::ConcurrentQueue<T> m_queue;
112};
113
114}// end namespace ph
115
A multi-producer, multi-consumer, lock-free concurrent queue-like structure. For single-thread uses,...
Definition TAtomicQuasiQueue.h:24
std::size_t estimatedSize() const
Approximated size of the queue.
Definition TAtomicQuasiQueue.ipp:65
bool tryDequeue(T *out_item)
Try to dequeue an item. While there is no contention, dequeue fails if there is no item or the memory...
Definition TAtomicQuasiQueue.ipp:51
std::size_t tryDequeueBulk(Iterator out_firstItem, std::size_t numItems)
Definition TAtomicQuasiQueue.ipp:59
TAtomicQuasiQueue()
Definition TAtomicQuasiQueue.ipp:12
bool tryEnqueue(U &&item)
Try to enqueue an item. Never allocate memory.
Definition TAtomicQuasiQueue.ipp:45
void enqueue(U &&item)
Enqueue an item. Allocate memory if required. Basic aquire-release semantics are guaranteed....
Definition TAtomicQuasiQueue.ipp:23
void enqueueBulk(Iterator firstItem, std::size_t numItems)
Enqueue multiple items at once. Similar to enqueue(1). Use std::make_move_iterator if the items shoul...
Definition TAtomicQuasiQueue.ipp:34
The root for all renderer implementations.
Definition EEngineProject.h:6