Get all elements from the queue and append them to a vector. If 'deadline' passes and no elements have been returned from the queue, returns Status::TimedOut(). If 'deadline' is uninitialized, no deadline is used. If the queue has been shut down, but there are still elements waiting, then it returns those elements as if the queue were not yet shut down. Returns: - OK if successful - TimedOut if
| 118 | // - TimedOut if the deadline passed |
| 119 | // - Aborted if the queue shut down |
| 120 | Status BlockingDrainTo(std::vector<T>* out, MonoTime deadline = {}) { |
| 121 | MutexLock l(lock_); |
| 122 | while (true) { |
| 123 | if (!queue_.empty()) { |
| 124 | out->reserve(queue_.size()); |
| 125 | for (const T& elt : queue_) { |
| 126 | decrement_size_unlocked(elt); |
| 127 | } |
| 128 | std::move(queue_.begin(), queue_.end(), std::back_inserter(*out)); |
| 129 | queue_.clear(); |
| 130 | l.Unlock(); |
| 131 | not_full_.Signal(); |
| 132 | return Status::OK(); |
| 133 | } |
| 134 | if (PREDICT_FALSE(shutdown_)) { |
| 135 | return Status::Aborted(""); |
| 136 | } |
| 137 | if (!deadline.Initialized()) { |
| 138 | not_empty_.Wait(); |
| 139 | } else if (PREDICT_FALSE(!not_empty_.WaitUntil(deadline))) { |
| 140 | return Status::TimedOut(""); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // Attempts to put the given value in the queue. |
| 146 | // Returns: |