| 546 | |
| 547 | |
| 548 | bool Pipe::Writer::write(string s) |
| 549 | { |
| 550 | bool written = false; |
| 551 | Owned<Promise<string>> read; |
| 552 | |
| 553 | synchronized (data->lock) { |
| 554 | // Ignore writes if either end of the pipe is closed or failed! |
| 555 | if (data->writeEnd == Writer::OPEN && data->readEnd == Reader::OPEN) { |
| 556 | // Don't bother surfacing empty writes to the readers. |
| 557 | if (!s.empty()) { |
| 558 | if (data->reads.empty()) { |
| 559 | data->writes.push(std::move(s)); |
| 560 | } else { |
| 561 | read = data->reads.front(); |
| 562 | data->reads.pop(); |
| 563 | } |
| 564 | } |
| 565 | written = true; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | // NOTE: We set the promise outside the critical section to avoid |
| 570 | // triggering callbacks that try to reacquire the lock. |
| 571 | if (read.get() != nullptr) { |
| 572 | read->set(std::move(s)); // NOLINT(misc-use-after-move) |
| 573 | } |
| 574 | |
| 575 | return written; |
| 576 | } |
| 577 | |
| 578 | |
| 579 | bool Pipe::Writer::close() |