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