| 40 | } |
| 41 | |
| 42 | bool LifoServiceQueue::BlockingGet(std::unique_ptr<InboundCall>* out) { |
| 43 | auto consumer = tl_consumer_; |
| 44 | if (PREDICT_FALSE(!consumer)) { |
| 45 | consumer = tl_consumer_ = new ConsumerState(this); |
| 46 | std::lock_guard<simple_spinlock> l(lock_); |
| 47 | consumers_.emplace_back(consumer); |
| 48 | } |
| 49 | |
| 50 | while (true) { |
| 51 | { |
| 52 | std::lock_guard<simple_spinlock> l(lock_); |
| 53 | if (!queue_.empty()) { |
| 54 | auto it = queue_.begin(); |
| 55 | out->reset(*it); |
| 56 | queue_.erase(it); |
| 57 | return true; |
| 58 | } |
| 59 | if (PREDICT_FALSE(shutdown_)) { |
| 60 | return false; |
| 61 | } |
| 62 | consumer->DCheckBoundInstance(this); |
| 63 | waiting_consumers_.push_back(consumer); |
| 64 | } |
| 65 | InboundCall* call = consumer->Wait(); |
| 66 | if (call != nullptr) { |
| 67 | out->reset(call); |
| 68 | return true; |
| 69 | } |
| 70 | // if call == nullptr, this means we are shutting down the queue. |
| 71 | // Loop back around and re-check 'shutdown_'. |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | QueueStatus LifoServiceQueue::Put(InboundCall* call, |
| 76 | std::optional<InboundCall*>* evicted) { |