Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 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 | |
| 11 #ifndef WEBRTC_COMMON_AUDIO_SWAP_QUEUE_H_ | |
| 12 #define WEBRTC_COMMON_AUDIO_SWAP_QUEUE_H_ | |
| 13 | |
| 14 #include <algorithm> | |
| 15 #include <utility> | |
| 16 #include <vector> | |
| 17 | |
| 18 #include "webrtc/base/checks.h" | |
| 19 #include "webrtc/base/criticalsection.h" | |
| 20 | |
| 21 namespace webrtc { | |
| 22 | |
| 23 namespace internal { | |
| 24 | |
| 25 // (Internal; please don't use outside this file.) | |
| 26 template <typename T> | |
| 27 bool NoopSwapQueueItemVerifierFunction(const T&) { | |
| 28 return true; | |
| 29 } | |
| 30 | |
| 31 } // namespace internal | |
| 32 | |
| 33 // Functor to use when supplying a verifier function for the queue. | |
| 34 template <typename T, | |
| 35 bool (*QueueItemVerifierFunction)(const T&) = | |
| 36 internal::NoopSwapQueueItemVerifierFunction> | |
| 37 class SwapQueueItemVerifier { | |
| 38 public: | |
| 39 bool operator()(const T& t) const { return QueueItemVerifierFunction(t); } | |
| 40 }; | |
| 41 | |
| 42 // This class is a fixed-size queue. A producer calls Insert() to insert | |
| 43 // an element of type T at the back of the queue, and a consumer calls | |
| 44 // Remove() to remove an element from the front of the queue. It's safe | |
| 45 // for the producer(s) and the consumer(s) to access the queue | |
| 46 // concurrently, from different threads. | |
| 47 // | |
| 48 // To avoid the construction, copying, and destruction of Ts that a naive | |
| 49 // queue implementation would require, for each "full" T passed from | |
| 50 // producer to consumer, SwapQueue<T> passes an "empty" T in the other | |
| 51 // direction (an "empty" T is one that contains nothing of value for the | |
| 52 // consumer). This bidirectional movement is implemented with swap(). | |
| 53 // | |
| 54 // // Create queue: | |
| 55 // Bottle proto(568); // Prepare an empty Bottle. Heap allocates space for | |
| 56 // // 568 ml. | |
| 57 // SwapQueue<Bottle> q(N, proto); // Init queue with N copies of proto. | |
| 58 // // Each copy allocates on the heap. | |
| 59 // // Producer pseudo-code: | |
| 60 // Bottle b(568); // Prepare an empty Bottle. Heap allocates space for 568 ml. | |
| 61 // loop { | |
| 62 // b.Fill(amount); // Where amount <= 568 ml. | |
| 63 // q.Insert(&b); // Swap our full Bottle for an empty one from q. | |
| 64 // } | |
| 65 // | |
| 66 // // Consumer pseudo-code: | |
| 67 // Bottle b(568); // Prepare an empty Bottle. Heap allocates space for 568 ml. | |
| 68 // loop { | |
| 69 // q.Remove(&b); // Swap our empty Bottle for the next-in-line full Bottle. | |
| 70 // Drink(&b); | |
| 71 // } | |
| 72 // | |
| 73 // For a well-behaved Bottle class, there are no allocations in the | |
| 74 // producer, since it just fills an empty Bottle that's already large | |
| 75 // enough; no deallocations in the consumer, since it returns each empty | |
| 76 // Bottle to the queue after having drunk it; and no copies along the | |
| 77 // way, since the queue uses swap() everywhere to move full Bottles in | |
| 78 // one direction and empty ones in the other. | |
| 79 template <typename T, typename QueueItemVerifier = SwapQueueItemVerifier<T>> | |
| 80 class SwapQueue { | |
| 81 public: | |
| 82 // Creates a queue of size size and fills it with default constructed Ts. | |
| 83 explicit SwapQueue(size_t size) : queue_(size) { | |
| 84 RTC_DCHECK(VerifyQueueSlots()); | |
| 85 } | |
| 86 | |
| 87 // Same as above and accepts a functor to use for initializing the | |
| 88 // item verification functor. | |
| 89 SwapQueue(size_t size, const QueueItemVerifier& queue_item_verifier) | |
| 90 : queue_item_verifier_(queue_item_verifier), queue_(size) { | |
| 91 RTC_DCHECK(VerifyQueueSlots()); | |
| 92 } | |
| 93 | |
| 94 // Creates a queue of size size and fills it with copies of prototype. | |
| 95 SwapQueue(size_t size, const T& prototype) : queue_(size, prototype) { | |
| 96 RTC_DCHECK(VerifyQueueSlots()); | |
| 97 } | |
| 98 | |
| 99 // Same as above and accepts a functor to use for initializing the item | |
| 100 // verification functor. | |
| 101 SwapQueue(size_t size, | |
| 102 const QueueItemVerifier& queue_item_verifier, | |
| 103 const T& prototype) | |
| 104 : queue_item_verifier_(queue_item_verifier), queue_(size, prototype) { | |
| 105 RTC_DCHECK(VerifyQueueSlots()); | |
| 106 } | |
| 107 | |
| 108 // Copy constructor. | |
| 109 SwapQueue& operator=(const SwapQueue& o) { | |
|
the sun
2015/10/30 09:19:47
1. This isn't a copy constructor, it is an assignm
kwiberg-webrtc
2015/10/30 09:27:29
Yeah, good point about copying the elements. And m
peah-webrtc
2015/10/30 09:42:16
Acknowledged.
peah-webrtc
2015/10/30 09:42:16
True on all bullets. Did not think of the fact tha
| |
| 110 QueueItemVerifier queue_item_verifier; | |
| 111 size_t next_write_index; | |
| 112 size_t next_read_index; | |
| 113 size_t num_elements; | |
| 114 std::vector<T> queue; | |
| 115 { | |
| 116 rtc::CritScope cs(&o.crit_queue_); | |
| 117 queue_item_verifier = o.queue_item_verifier_; | |
| 118 next_write_index = o.next_write_index_; | |
| 119 next_read_index = o.next_read_index_; | |
| 120 num_elements = o.num_elements_; | |
| 121 queue = o.queue_; | |
| 122 } | |
| 123 { | |
| 124 rtc::CritScope cs(&crit_queue_); | |
| 125 queue_item_verifier_ = queue_item_verifier; | |
| 126 next_write_index_ = next_write_index; | |
| 127 next_read_index_ = next_read_index; | |
| 128 num_elements_ = num_elements; | |
| 129 using std::swap; | |
| 130 swap(queue_, queue); | |
| 131 } | |
| 132 | |
| 133 return *this; | |
| 134 } | |
| 135 | |
| 136 // Resets the queue to have zero content wile maintaining the queue size. | |
| 137 void Clear() { | |
| 138 rtc::CritScope cs(&crit_queue_); | |
| 139 next_write_index_ = 0; | |
| 140 next_read_index_ = 0; | |
| 141 num_elements_ = 0; | |
| 142 } | |
| 143 | |
| 144 // Inserts a "full" T at the back of the queue by swapping *input with an | |
| 145 // "empty" T from the queue. | |
| 146 // Returns true if the item was inserted or false if not (the queue was full). | |
| 147 // When specified, the T given in *input must pass the ItemVerifier() test. | |
| 148 // The contents of *input after the call are then also guaranteed to pass the | |
| 149 // ItemVerifier() test. | |
| 150 bool Insert(T* input) WARN_UNUSED_RESULT { | |
| 151 RTC_DCHECK(input); | |
| 152 | |
| 153 rtc::CritScope cs(&crit_queue_); | |
| 154 | |
| 155 RTC_DCHECK(queue_item_verifier_(*input)); | |
| 156 | |
| 157 if (num_elements_ == queue_.size()) { | |
| 158 return false; | |
| 159 } | |
| 160 | |
| 161 using std::swap; | |
| 162 swap(*input, queue_[next_write_index_]); | |
| 163 | |
| 164 ++next_write_index_; | |
| 165 if (next_write_index_ == queue_.size()) { | |
| 166 next_write_index_ = 0; | |
| 167 } | |
| 168 | |
| 169 ++num_elements_; | |
| 170 | |
| 171 RTC_DCHECK_LT(next_write_index_, queue_.size()); | |
| 172 RTC_DCHECK_LE(num_elements_, queue_.size()); | |
| 173 | |
| 174 return true; | |
| 175 } | |
| 176 | |
| 177 // Removes the frontmost "full" T from the queue by swapping it with | |
| 178 // the "empty" T in *output. | |
| 179 // Returns true if an item could be removed or false if not (the queue was | |
| 180 // empty). When specified, The T given in *output must pass the ItemVerifier() | |
| 181 // test and the contents of *output after the call are then also guaranteed to | |
| 182 // pass the ItemVerifier() test. | |
| 183 bool Remove(T* output) WARN_UNUSED_RESULT { | |
| 184 RTC_DCHECK(output); | |
| 185 | |
| 186 rtc::CritScope cs(&crit_queue_); | |
| 187 | |
| 188 RTC_DCHECK(queue_item_verifier_(*output)); | |
| 189 | |
| 190 if (num_elements_ == 0) { | |
| 191 return false; | |
| 192 } | |
| 193 | |
| 194 using std::swap; | |
| 195 swap(*output, queue_[next_read_index_]); | |
| 196 | |
| 197 ++next_read_index_; | |
| 198 if (next_read_index_ == queue_.size()) { | |
| 199 next_read_index_ = 0; | |
| 200 } | |
| 201 | |
| 202 --num_elements_; | |
| 203 | |
| 204 RTC_DCHECK_LT(next_read_index_, queue_.size()); | |
| 205 RTC_DCHECK_LE(num_elements_, queue_.size()); | |
| 206 | |
| 207 return true; | |
| 208 } | |
| 209 | |
| 210 private: | |
| 211 // Verify that the queue slots complies with the ItemVerifier test. | |
| 212 bool VerifyQueueSlots() { | |
| 213 rtc::CritScope cs(&crit_queue_); | |
| 214 for (const auto& v : queue_) { | |
| 215 RTC_DCHECK(queue_item_verifier_(v)); | |
| 216 } | |
| 217 return true; | |
| 218 } | |
| 219 | |
| 220 mutable rtc::CriticalSection crit_queue_; | |
|
the sun
2015/10/30 09:19:47
Remove mutable again?
peah-webrtc
2015/10/30 09:42:16
Done.
| |
| 221 | |
| 222 QueueItemVerifier queue_item_verifier_ GUARDED_BY(crit_queue_); | |
| 223 | |
| 224 // (next_read_index_ + num_elements_) % queue_.size() = | |
| 225 // next_write_index_ | |
| 226 size_t next_write_index_ GUARDED_BY(crit_queue_) = 0; | |
| 227 size_t next_read_index_ GUARDED_BY(crit_queue_) = 0; | |
| 228 size_t num_elements_ GUARDED_BY(crit_queue_) = 0; | |
| 229 | |
| 230 // queue_.size() is constant. | |
| 231 std::vector<T> queue_ GUARDED_BY(crit_queue_); | |
|
the sun
2015/10/30 09:19:47
Add
DISALLOW_COPY_AND_ASSIGN(SwapQueue);
peah-webrtc
2015/10/30 09:42:16
Done.
| |
| 232 }; | |
| 233 | |
| 234 } // namespace webrtc | |
| 235 | |
| 236 #endif // WEBRTC_COMMON_AUDIO_SWAP_QUEUE_H_ | |
| OLD | NEW |