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_MODULES_SWAP_QUEUE_H_ | |
| 12 #define WEBRTC_MODULES_SWAP_QUEUE_H_ | |
|
kwiberg-webrtc
2015/10/14 14:10:16
Don't worry, you'll get it right one of these days
peah-webrtc
2015/10/15 07:35:00
Done.
peah-webrtc
2015/10/15 07:35:00
:-)
| |
| 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 // This class is a one-way queue where data is written from one end and read | |
| 24 // from the other end. | |
| 25 // It is safe to access the class concurrently from multiple threads. | |
| 26 // The queueable objects must all be swappable as the Insert and Remove methods | |
|
kwiberg-webrtc
2015/10/14 14:10:16
"queuable" -> "enqueued", maybe?
peah-webrtc
2015/10/15 07:34:59
Used a slightly different wording.
| |
| 27 // work by swapping the "empty" and "full" items with the existing content in | |
| 28 // the queue. When doing an Insert, the "empty" content in the next write | |
| 29 // position in the queue is swapped for the "full" item in the input to Insert, | |
| 30 // and that queue position then becomes "full", while the input item to Insert | |
| 31 // becomes "empty". In contrast, when doing a Remove, the "empty" content in the | |
| 32 // input to Remove is swapped for the "full" element in the next read position | |
| 33 // in the queue. After the call the input to Remove is "full" and the element | |
| 34 // in the queue is "empty". | |
|
kwiberg-webrtc
2015/10/14 14:10:16
Perhaps still a bit too imprecise. How about somet
peah-webrtc
2015/10/15 07:34:59
This looks super. I used it as it is, and added so
| |
| 35 // This is illustrated in the example below: | |
| 36 // | |
| 37 // Example of queue usage: | |
| 38 // SwapQueue<int> q(2)); // q contains [0(empty) 0(empty)]; | |
| 39 // int i = 1; | |
| 40 // q.Insert(&i); // q contains [1(full) 0(empty)], i=0 | |
| 41 // i = 2; | |
| 42 // q.Insert(&i); // q contains [1(full) 2(full)], i=0 | |
| 43 // i = -3(; | |
| 44 // q.Remove(&i); // q contains [-3(empty) 2(full)], i=1 | |
| 45 // i = 4; | |
| 46 // q.Insert(&i); // q contains [4(full) 2(full)], i=-3 | |
| 47 // i = -5; | |
| 48 // q.Remove(&i); // q contains [4(full) -5(empty)], i=2 | |
| 49 // i = -6; | |
| 50 // q.Remove(&i); // q contains [-6(empty) -5(empty)], i=4 | |
| 51 // | |
| 52 // It should be noted that if you want the "empty" Ts that are returned from the | |
| 53 // Insert to be of a certain characteristic (e.g., certain size), care must be | |
| 54 // taken that the queue is initialized with such elements, and all Ts that are | |
| 55 // used as input to Remove have that characteristic. In order to verify this | |
| 56 // invariance a callback function can be supplied that verifies this in debug | |
| 57 // mode (via RTC_DCHECK) for the queue elements. Examples of this can be found | |
| 58 // in the unit tests. | |
| 59 | |
| 60 // Default item invariance verifier callback function. | |
|
the sun
2015/10/14 13:05:21
Please use the term "invariant" as that is more co
kwiberg-webrtc
2015/10/14 14:10:16
You could conceivably also put it in its own names
peah-webrtc
2015/10/15 07:35:00
I'm still not fully happy with invariant. I change
peah-webrtc
2015/10/15 07:35:00
Done.
kwiberg-webrtc
2015/10/15 08:55:40
That works too. But the usual meaning of "invarian
peah-webrtc
2015/10/26 08:56:57
Not sure about the conclusion, should we go for in
| |
| 61 template <typename T> | |
| 62 bool DefaultItemInvarianceVerifier(const T&) { | |
| 63 return true; | |
| 64 } | |
| 65 | |
| 66 // Queue template implementing a queue of elements of type T and with an | |
| 67 // optional invariance verifier callback function that verifies | |
| 68 // that the queue elements are compliant to a certain requirement. | |
| 69 template <typename T, | |
| 70 bool (*ItemInvarianceVerifier)(const T&) = | |
|
the sun
2015/10/14 13:05:21
I think CheckItemInvariant was a better name...
peah-webrtc
2015/10/15 07:34:59
Hmm, I'm not either superhappy about the current n
| |
| 71 DefaultItemInvarianceVerifier> | |
|
kwiberg-webrtc
2015/10/14 14:10:16
Not too happy with this name. Maybe simply "ItemCh
peah-webrtc
2015/10/15 07:35:00
Me neither. I changed it now to ItemVerifier. In m
kwiberg-webrtc
2015/10/15 08:55:40
That we allow the user to require an invariant (wh
peah-webrtc
2015/10/26 08:56:57
Acknowledged.
| |
| 72 class SwapQueue { | |
| 73 public: | |
| 74 // Creates a queue of size size and fills it with the specified number of | |
| 75 // default constructed Ts. | |
| 76 explicit SwapQueue(size_t size) : queue_(size) { CheckQueueInvariance(); } | |
| 77 | |
| 78 // Creates a queue of size and fills it with copies of prototype. | |
|
kwiberg-webrtc
2015/10/14 14:10:16
"size" -> "size size".
peah-webrtc
2015/10/15 07:34:59
Done.
| |
| 79 SwapQueue(size_t size, const T& prototype) : queue_(size, prototype) { | |
| 80 CheckQueueInvariance(); | |
| 81 } | |
| 82 | |
| 83 // Resets the queue to have zero content. | |
| 84 void Clear() { | |
| 85 rtc::CritScope cs(&crit_queue_); | |
| 86 next_write_index_ = 0; | |
| 87 next_read_index_ = 0; | |
| 88 num_elements_ = 0; | |
| 89 } | |
| 90 | |
| 91 // Inserts a Y into the queue. The "full" input is swapped with the "empty" | |
|
the sun
2015/10/14 13:05:21
Y -> T
"Insert a T into the queue by swapping *in
kwiberg-webrtc
2015/10/14 14:10:16
"Y" -> "T"
peah-webrtc
2015/10/15 07:35:00
Done.
peah-webrtc
2015/10/15 07:35:00
Looks super. I did some minor editing, in particul
| |
| 92 // content of type T present in the queue. | |
| 93 // Returns false if the queue was already full (in which case no insert | |
| 94 // is performed), and true if not. | |
| 95 bool Insert(T* input) { | |
| 96 RTC_DCHECK(input); | |
| 97 RTC_DCHECK(ItemInvarianceVerifier(*input)); | |
| 98 | |
| 99 rtc::CritScope cs(&crit_queue_); | |
| 100 | |
| 101 if (num_elements_ == queue_.size()) { | |
|
the sun
2015/10/14 13:05:21
Thank you! :)
kwiberg-webrtc
2015/10/14 14:10:16
Aww! Don't give in just because he's more intimida
peah-webrtc
2015/10/15 07:35:00
:D
peah-webrtc
2015/10/15 07:35:00
This is tricky, and I'm a bit split in this.
I've
| |
| 102 return false; | |
| 103 } | |
| 104 | |
| 105 using std::swap; | |
| 106 swap(*input, queue_[next_write_index_]); | |
| 107 | |
| 108 next_write_index_++; | |
| 109 if (next_write_index_ == queue_.size()) { | |
| 110 next_write_index_ = 0; | |
| 111 } | |
| 112 | |
| 113 num_elements_++; | |
| 114 return true; | |
| 115 } | |
| 116 | |
| 117 // Removes a chunk of data from the queue. The "full" queue element is swapped | |
|
the sun
2015/10/14 13:05:21
"Remove a T from the queue by swapping *output wit
kwiberg-webrtc
2015/10/14 14:10:16
"chunk of data" -> "T"
peah-webrtc
2015/10/15 07:34:59
Done.
peah-webrtc
2015/10/15 07:35:00
Nice! I changed to that, but revised it a bit. Ple
| |
| 118 // with the existing "empty" content of output. | |
| 119 // Returns false if the queue was already empty (om which case no element is | |
| 120 // removed), and true if not. | |
| 121 bool Remove(T* output) { | |
| 122 RTC_DCHECK(output); | |
| 123 RTC_DCHECK(ItemInvarianceVerifier(*output)); | |
| 124 | |
| 125 rtc::CritScope cs(&crit_queue_); | |
| 126 | |
| 127 if (num_elements_ == 0) { | |
| 128 return false; | |
| 129 } | |
| 130 | |
| 131 using std::swap; | |
| 132 swap(*output, queue_[next_read_index_]); | |
| 133 | |
| 134 next_read_index_++; | |
| 135 if (next_read_index_ == queue_.size()) { | |
| 136 next_read_index_ = 0; | |
| 137 } | |
| 138 | |
| 139 num_elements_--; | |
| 140 return true; | |
| 141 } | |
| 142 | |
| 143 private: | |
| 144 // Checks queue invariance. | |
| 145 void CheckQueueInvariance() { | |
|
kwiberg-webrtc
2015/10/14 14:10:16
invariance -> invariants. Otherwise, it sounds as
peah-webrtc
2015/10/15 07:35:00
I think that what we do is to verify the complianc
| |
| 146 rtc::CritScope cs(&crit_queue_); | |
| 147 for (const auto& v : queue_) { | |
| 148 RTC_DCHECK(ItemInvarianceVerifier(v)); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 // queue_.size() is constant. | |
| 153 std::vector<T> queue_ GUARDED_BY(crit_queue_); | |
| 154 | |
| 155 // (next_read_index_ + num_elements_) % queue_.size() = | |
| 156 // next_write_index_ | |
| 157 // 0 <= next_write_index_ <= queue.size() | |
| 158 size_t next_write_index_ GUARDED_BY(crit_queue_) = 0; | |
| 159 // 0 <= next_read_index_ <= queue.size() | |
| 160 size_t next_read_index_ GUARDED_BY(crit_queue_) = 0; | |
| 161 | |
| 162 // 0 <= num_elements_ <= queue.size() | |
| 163 size_t num_elements_ GUARDED_BY(crit_queue_) = 0; | |
| 164 | |
| 165 rtc::CriticalSection crit_queue_; | |
| 166 | |
| 167 RTC_DISALLOW_COPY_AND_ASSIGN(SwapQueue); | |
| 168 }; | |
| 169 | |
| 170 } // namespace webrtc | |
| 171 | |
| 172 #endif // WEBRTC_MODULES_AUDIO_PROCESSING_SWAPPED_NONBLOCKING_QUEUE_H_ | |
| OLD | NEW |