| 171 | /// inserted into the queue. |
| 172 | template <typename V> |
| 173 | bool BlockingPutWithTimeout(V&& val, int64_t timeout_micros) { |
| 174 | MonotonicStopWatch timer; |
| 175 | int64_t val_bytes = ElemBytesFn()(val); |
| 176 | DCHECK_GE(val_bytes, 0); |
| 177 | std::unique_lock<std::mutex> write_lock(put_lock_); |
| 178 | timespec abs_time; |
| 179 | TimeFromNowMicros(timeout_micros, &abs_time); |
| 180 | bool notified = true; |
| 181 | while (!HasCapacityInternal(write_lock, val_bytes) && !shutdown_ && notified) { |
| 182 | if (put_wait_timer_ != nullptr) timer.Start(); |
| 183 | // Wait until we're notified or until the timeout expires. |
| 184 | notified = put_cv_.WaitUntil(write_lock, abs_time); |
| 185 | if (put_wait_timer_ != nullptr) timer.Stop(); |
| 186 | } |
| 187 | if (put_wait_timer_ != nullptr) put_wait_timer_->Add(timer.ElapsedTime()); |
| 188 | // If the list is still full or if the the queue has been shut down, return false. |
| 189 | // NOTE: We don't check 'notified' here as it appears that pthread condition variables |
| 190 | // have a weird behavior in which they can return ETIMEDOUT from timed_wait even if |
| 191 | // another thread did in fact signal |
| 192 | if (!HasCapacityInternal(write_lock, val_bytes)) return false; |
| 193 | DCHECK_LT(put_list_.size(), max_elements_); |
| 194 | put_bytes_enqueued_ += val_bytes; |
| 195 | Put(std::forward<V>(val)); |
| 196 | write_lock.unlock(); |
| 197 | get_cv_.NotifyOne(); |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | /// Shut down the queue. Wakes up all threads waiting on BlockingGet or BlockingPut. |
| 202 | void Shutdown() { |