\brief Tell the consumer we have finished producing It is allowed to call this and later call Push() again ("early close"). In this case, calls to Push() after the queue is closed are silently ignored. This can help implementing non-trivial cancellation cases. True is returned on success, false if the generator is already closed or destroyed.
| 902 | /// True is returned on success, false if the generator is already closed |
| 903 | /// or destroyed. |
| 904 | bool Close() { |
| 905 | auto state = weak_state_.lock(); |
| 906 | if (!state) { |
| 907 | // Generator was destroyed |
| 908 | return false; |
| 909 | } |
| 910 | auto lock = state->mutex.Lock(); |
| 911 | if (state->finished) { |
| 912 | // Already closed |
| 913 | return false; |
| 914 | } |
| 915 | state->finished = true; |
| 916 | if (state->consumer_fut.has_value()) { |
| 917 | auto fut = std::move(state->consumer_fut.value()); |
| 918 | state->consumer_fut.reset(); |
| 919 | lock.Unlock(); // unlock before potentially invoking a callback |
| 920 | fut.MarkFinished(IterationTraits<T>::End()); |
| 921 | } |
| 922 | return true; |
| 923 | } |
| 924 | |
| 925 | /// Return whether the generator was closed or destroyed. |
| 926 | bool is_closed() const { |