blob: c3246dba94f3994530672ca1c9385a15db958f1f [file] [log] [blame]
peah48407f72015-11-09 05:24:50 -08001/*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_SWAP_QUEUE_H_
12#define RTC_BASE_SWAP_QUEUE_H_
peah48407f72015-11-09 05:24:50 -080013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020015#include <utility>
16#include <vector>
peah48407f72015-11-09 05:24:50 -080017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/constructor_magic.h"
20#include "rtc_base/critical_section.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020021#include "rtc_base/system/unused.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "rtc_base/thread_annotations.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020023
24namespace webrtc {
25
26namespace internal {
27
28// (Internal; please don't use outside this file.)
29template <typename T>
30bool NoopSwapQueueItemVerifierFunction(const T&) {
31 return true;
32}
33
34} // namespace internal
35
36// Functor to use when supplying a verifier function for the queue.
37template <typename T,
38 bool (*QueueItemVerifierFunction)(const T&) =
39 internal::NoopSwapQueueItemVerifierFunction>
40class SwapQueueItemVerifier {
41 public:
42 bool operator()(const T& t) const { return QueueItemVerifierFunction(t); }
43};
44
45// This class is a fixed-size queue. A producer calls Insert() to insert
46// an element of type T at the back of the queue, and a consumer calls
47// Remove() to remove an element from the front of the queue. It's safe
48// for the producer(s) and the consumer(s) to access the queue
49// concurrently, from different threads.
50//
51// To avoid the construction, copying, and destruction of Ts that a naive
52// queue implementation would require, for each "full" T passed from
53// producer to consumer, SwapQueue<T> passes an "empty" T in the other
54// direction (an "empty" T is one that contains nothing of value for the
55// consumer). This bidirectional movement is implemented with swap().
56//
57// // Create queue:
58// Bottle proto(568); // Prepare an empty Bottle. Heap allocates space for
59// // 568 ml.
60// SwapQueue<Bottle> q(N, proto); // Init queue with N copies of proto.
61// // Each copy allocates on the heap.
62// // Producer pseudo-code:
63// Bottle b(568); // Prepare an empty Bottle. Heap allocates space for 568 ml.
64// loop {
65// b.Fill(amount); // Where amount <= 568 ml.
66// q.Insert(&b); // Swap our full Bottle for an empty one from q.
67// }
68//
69// // Consumer pseudo-code:
70// Bottle b(568); // Prepare an empty Bottle. Heap allocates space for 568 ml.
71// loop {
72// q.Remove(&b); // Swap our empty Bottle for the next-in-line full Bottle.
73// Drink(&b);
74// }
75//
76// For a well-behaved Bottle class, there are no allocations in the
77// producer, since it just fills an empty Bottle that's already large
78// enough; no deallocations in the consumer, since it returns each empty
79// Bottle to the queue after having drunk it; and no copies along the
80// way, since the queue uses swap() everywhere to move full Bottles in
81// one direction and empty ones in the other.
82template <typename T, typename QueueItemVerifier = SwapQueueItemVerifier<T>>
83class SwapQueue {
84 public:
85 // Creates a queue of size size and fills it with default constructed Ts.
86 explicit SwapQueue(size_t size) : queue_(size) {
87 RTC_DCHECK(VerifyQueueSlots());
88 }
89
90 // Same as above and accepts an item verification functor.
91 SwapQueue(size_t size, const QueueItemVerifier& queue_item_verifier)
92 : queue_item_verifier_(queue_item_verifier), queue_(size) {
93 RTC_DCHECK(VerifyQueueSlots());
94 }
95
96 // Creates a queue of size size and fills it with copies of prototype.
97 SwapQueue(size_t size, const T& prototype) : queue_(size, prototype) {
98 RTC_DCHECK(VerifyQueueSlots());
99 }
100
101 // Same as above and accepts an item verification functor.
102 SwapQueue(size_t size,
103 const T& prototype,
104 const QueueItemVerifier& queue_item_verifier)
105 : queue_item_verifier_(queue_item_verifier), queue_(size, prototype) {
106 RTC_DCHECK(VerifyQueueSlots());
107 }
108
109 // Resets the queue to have zero content wile maintaining the queue size.
110 void Clear() {
111 rtc::CritScope cs(&crit_queue_);
112 next_write_index_ = 0;
113 next_read_index_ = 0;
114 num_elements_ = 0;
115 }
116
117 // Inserts a "full" T at the back of the queue by swapping *input with an
118 // "empty" T from the queue.
119 // Returns true if the item was inserted or false if not (the queue was full).
120 // When specified, the T given in *input must pass the ItemVerifier() test.
121 // The contents of *input after the call are then also guaranteed to pass the
122 // ItemVerifier() test.
123 bool Insert(T* input) RTC_WARN_UNUSED_RESULT {
124 RTC_DCHECK(input);
125
126 rtc::CritScope cs(&crit_queue_);
127
128 RTC_DCHECK(queue_item_verifier_(*input));
129
130 if (num_elements_ == queue_.size()) {
131 return false;
132 }
133
134 using std::swap;
135 swap(*input, queue_[next_write_index_]);
136
137 ++next_write_index_;
138 if (next_write_index_ == queue_.size()) {
139 next_write_index_ = 0;
140 }
141
142 ++num_elements_;
143
144 RTC_DCHECK_LT(next_write_index_, queue_.size());
145 RTC_DCHECK_LE(num_elements_, queue_.size());
146
147 return true;
148 }
149
150 // Removes the frontmost "full" T from the queue by swapping it with
151 // the "empty" T in *output.
152 // Returns true if an item could be removed or false if not (the queue was
153 // empty). When specified, The T given in *output must pass the ItemVerifier()
154 // test and the contents of *output after the call are then also guaranteed to
155 // pass the ItemVerifier() test.
156 bool Remove(T* output) RTC_WARN_UNUSED_RESULT {
157 RTC_DCHECK(output);
158
159 rtc::CritScope cs(&crit_queue_);
160
161 RTC_DCHECK(queue_item_verifier_(*output));
162
163 if (num_elements_ == 0) {
164 return false;
165 }
166
167 using std::swap;
168 swap(*output, queue_[next_read_index_]);
169
170 ++next_read_index_;
171 if (next_read_index_ == queue_.size()) {
172 next_read_index_ = 0;
173 }
174
175 --num_elements_;
176
177 RTC_DCHECK_LT(next_read_index_, queue_.size());
178 RTC_DCHECK_LE(num_elements_, queue_.size());
179
180 return true;
181 }
182
183 private:
184 // Verify that the queue slots complies with the ItemVerifier test.
185 bool VerifyQueueSlots() {
186 rtc::CritScope cs(&crit_queue_);
187 for (const auto& v : queue_) {
188 RTC_DCHECK(queue_item_verifier_(v));
189 }
190 return true;
191 }
192
193 rtc::CriticalSection crit_queue_;
194
195 // TODO(peah): Change this to use std::function() once we can use C++11 std
196 // lib.
danilchap3c6abd22017-09-06 05:46:29 -0700197 QueueItemVerifier queue_item_verifier_ RTC_GUARDED_BY(crit_queue_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200198
199 // (next_read_index_ + num_elements_) % queue_.size() =
200 // next_write_index_
danilchap3c6abd22017-09-06 05:46:29 -0700201 size_t next_write_index_ RTC_GUARDED_BY(crit_queue_) = 0;
202 size_t next_read_index_ RTC_GUARDED_BY(crit_queue_) = 0;
203 size_t num_elements_ RTC_GUARDED_BY(crit_queue_) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200204
205 // queue_.size() is constant.
danilchap3c6abd22017-09-06 05:46:29 -0700206 std::vector<T> queue_ RTC_GUARDED_BY(crit_queue_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200207
208 RTC_DISALLOW_COPY_AND_ASSIGN(SwapQueue);
209};
210
211} // namespace webrtc
peah48407f72015-11-09 05:24:50 -0800212
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200213#endif // RTC_BASE_SWAP_QUEUE_H_