\brief Push a value on the queue True is returned if the value was pushed, false if the generator is already closed or destroyed. If the latter, it is recommended to stop producing any further values.
| 872 | /// already closed or destroyed. If the latter, it is recommended to stop |
| 873 | /// producing any further values. |
| 874 | bool Push(Result<T> result) { |
| 875 | auto state = weak_state_.lock(); |
| 876 | if (!state) { |
| 877 | // Generator was destroyed |
| 878 | return false; |
| 879 | } |
| 880 | auto lock = state->mutex.Lock(); |
| 881 | if (state->finished) { |
| 882 | // Closed early |
| 883 | return false; |
| 884 | } |
| 885 | if (state->consumer_fut.has_value()) { |
| 886 | auto fut = std::move(state->consumer_fut.value()); |
| 887 | state->consumer_fut.reset(); |
| 888 | lock.Unlock(); // unlock before potentially invoking a callback |
| 889 | fut.MarkFinished(std::move(result)); |
| 890 | } else { |
| 891 | state->result_q.push_back(std::move(result)); |
| 892 | } |
| 893 | return true; |
| 894 | } |
| 895 | |
| 896 | /// \brief Tell the consumer we have finished producing |
| 897 | /// |