diff options
Diffstat (limited to 'src/common/threadsafe_queue.h')
| -rw-r--r-- | src/common/threadsafe_queue.h | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/src/common/threadsafe_queue.h b/src/common/threadsafe_queue.h index a0c731e8c..edf13bc49 100644 --- a/src/common/threadsafe_queue.h +++ b/src/common/threadsafe_queue.h | |||
| @@ -33,9 +33,11 @@ public: | |||
| 33 | bool Empty() const { | 33 | bool Empty() const { |
| 34 | return !read_ptr->next.load(); | 34 | return !read_ptr->next.load(); |
| 35 | } | 35 | } |
| 36 | |||
| 36 | T& Front() const { | 37 | T& Front() const { |
| 37 | return read_ptr->current; | 38 | return read_ptr->current; |
| 38 | } | 39 | } |
| 40 | |||
| 39 | template <typename Arg> | 41 | template <typename Arg> |
| 40 | void Push(Arg&& t) { | 42 | void Push(Arg&& t) { |
| 41 | // create the element, add it to the queue | 43 | // create the element, add it to the queue |
| @@ -108,15 +110,41 @@ private: | |||
| 108 | // single reader, multiple writer queue | 110 | // single reader, multiple writer queue |
| 109 | 111 | ||
| 110 | template <typename T, bool NeedSize = true> | 112 | template <typename T, bool NeedSize = true> |
| 111 | class MPSCQueue : public SPSCQueue<T, NeedSize> { | 113 | class MPSCQueue { |
| 112 | public: | 114 | public: |
| 115 | u32 Size() const { | ||
| 116 | return spsc_queue.Size(); | ||
| 117 | } | ||
| 118 | |||
| 119 | bool Empty() const { | ||
| 120 | return spsc_queue.Empty(); | ||
| 121 | } | ||
| 122 | |||
| 123 | T& Front() const { | ||
| 124 | return spsc_queue.Front(); | ||
| 125 | } | ||
| 126 | |||
| 113 | template <typename Arg> | 127 | template <typename Arg> |
| 114 | void Push(Arg&& t) { | 128 | void Push(Arg&& t) { |
| 115 | std::lock_guard<std::mutex> lock(write_lock); | 129 | std::lock_guard<std::mutex> lock(write_lock); |
| 116 | SPSCQueue<T, NeedSize>::Push(t); | 130 | spsc_queue.Push(t); |
| 131 | } | ||
| 132 | |||
| 133 | void Pop() { | ||
| 134 | return spsc_queue.Pop(); | ||
| 135 | } | ||
| 136 | |||
| 137 | bool Pop(T& t) { | ||
| 138 | return spsc_queue.Pop(t); | ||
| 139 | } | ||
| 140 | |||
| 141 | // not thread-safe | ||
| 142 | void Clear() { | ||
| 143 | spsc_queue.Clear(); | ||
| 117 | } | 144 | } |
| 118 | 145 | ||
| 119 | private: | 146 | private: |
| 147 | SPSCQueue<T, NeedSize> spsc_queue; | ||
| 120 | std::mutex write_lock; | 148 | std::mutex write_lock; |
| 121 | }; | 149 | }; |
| 122 | } // namespace Common | 150 | } // namespace Common |