| 2573 | } |
| 2574 | |
| 2575 | void Stream::ReturnSubStream(Stream *sub_stream) { |
| 2576 | absl::MutexLock lock(&mu_); |
| 2577 | |
| 2578 | // Look for the sub-stream. |
| 2579 | for (int64 index = 0; index < sub_streams_.size(); ++index) { |
| 2580 | std::pair<std::unique_ptr<Stream>, bool> &pair = sub_streams_[index]; |
| 2581 | if (pair.first.get() != sub_stream) { |
| 2582 | continue; |
| 2583 | } |
| 2584 | |
| 2585 | // Found the sub_stream. |
| 2586 | if (sub_stream->ok()) { |
| 2587 | VLOG(1) << DebugStreamPointers() << " returned ok sub_stream " |
| 2588 | << sub_stream->DebugStreamPointers(); |
| 2589 | pair.second = true; |
| 2590 | } else { |
| 2591 | // The returned stream is not ok. Streams have a monotonic state |
| 2592 | // machine; the stream will remain in !ok forever. Swap it with the last |
| 2593 | // stream and pop it off. |
| 2594 | VLOG(1) << DebugStreamPointers() << " returned !ok sub_stream " |
| 2595 | << sub_stream->DebugStreamPointers(); |
| 2596 | const int64 last = sub_streams_.size() - 1; |
| 2597 | if (index != last) { |
| 2598 | std::swap(pair, sub_streams_[last]); |
| 2599 | } |
| 2600 | sub_streams_.pop_back(); |
| 2601 | } |
| 2602 | return; |
| 2603 | } |
| 2604 | |
| 2605 | LOG(FATAL) << DebugStreamPointers() |
| 2606 | << " did not create the returned sub-stream " |
| 2607 | << sub_stream->DebugStreamPointers(); |
| 2608 | } |
| 2609 | |
| 2610 | Stream &Stream::ThenStartTimer(Timer *t) { |
| 2611 | VLOG_CALL(PARAM(t)); |