|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <library/cpp/threading/future/future.h> |
| 4 | + |
| 5 | +#include <coroutine> |
| 6 | + |
| 7 | +template <typename... Args> |
| 8 | +struct std::coroutine_traits<NThreading::TFuture<void>, Args...> { |
| 9 | + struct promise_type { |
| 10 | + |
| 11 | + NThreading::TFuture<void> get_return_object() { |
| 12 | + return Promise_.GetFuture(); |
| 13 | + } |
| 14 | + |
| 15 | + std::suspend_never initial_suspend() { return {}; } |
| 16 | + std::suspend_never final_suspend() noexcept { return {}; } |
| 17 | + |
| 18 | + void unhandled_exception() { |
| 19 | + Promise_.SetException(std::current_exception()); |
| 20 | + } |
| 21 | + |
| 22 | + void return_void() { |
| 23 | + Promise_.SetValue(); |
| 24 | + } |
| 25 | + |
| 26 | + private: |
| 27 | + NThreading::TPromise<void> Promise_ = NThreading::NewPromise(); |
| 28 | + }; |
| 29 | +}; |
| 30 | + |
| 31 | +template <typename T, typename... Args> |
| 32 | +struct std::coroutine_traits<NThreading::TFuture<T>, Args...> { |
| 33 | + struct promise_type { |
| 34 | + NThreading::TFuture<T> get_return_object() { |
| 35 | + return Promise_.GetFuture(); |
| 36 | + } |
| 37 | + |
| 38 | + std::suspend_never initial_suspend() { return {}; } |
| 39 | + std::suspend_never final_suspend() noexcept { return {}; } |
| 40 | + |
| 41 | + void unhandled_exception() { |
| 42 | + Promise_.SetException(std::current_exception()); |
| 43 | + } |
| 44 | + |
| 45 | + void return_value(auto&& val) { |
| 46 | + Promise_.SetValue(std::forward<decltype(val)>(val)); |
| 47 | + } |
| 48 | + |
| 49 | + private: |
| 50 | + NThreading::TPromise<T> Promise_ = NThreading::NewPromise<T>(); |
| 51 | + }; |
| 52 | +}; |
| 53 | + |
| 54 | +namespace NThreading { |
| 55 | + |
| 56 | + template <typename T, bool Extracting = false> |
| 57 | + struct TFutureAwaitable { |
| 58 | + NThreading::TFuture<T> Future; |
| 59 | + |
| 60 | + TFutureAwaitable(const NThreading::TFuture<T>& future) noexcept requires (!Extracting) |
| 61 | + : Future{future} |
| 62 | + { |
| 63 | + } |
| 64 | + |
| 65 | + TFutureAwaitable(NThreading::TFuture<T>&& future) noexcept |
| 66 | + : Future{std::move(future)} |
| 67 | + { |
| 68 | + } |
| 69 | + |
| 70 | + bool await_ready() const noexcept { |
| 71 | + return Future.IsReady(); |
| 72 | + } |
| 73 | + |
| 74 | + void await_suspend(auto h) noexcept { |
| 75 | + /* |
| 76 | + * This library assumes that resume never throws an exception. |
| 77 | + * This assumption is made due to the fact that the users of these library in most cases do not need to write their own coroutine handlers, |
| 78 | + * and all coroutine handlers provided by the library do not throw exception from resume. |
| 79 | + * |
| 80 | + * WARNING: do not change subscribe to apply or something other here, creating an extra future state degrades performance. |
| 81 | + */ |
| 82 | + Future.NoexceptSubscribe( |
| 83 | + [h](auto) mutable noexcept { |
| 84 | + h(); |
| 85 | + } |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + decltype(auto) await_resume() { |
| 90 | + if constexpr (Extracting && !std::is_same_v<T, void>) { // Future<void> has only GetValue() |
| 91 | + return Future.ExtractValue(); |
| 92 | + } else { |
| 93 | + return Future.GetValue(); |
| 94 | + } |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + template <typename T> |
| 99 | + using TExtractingFutureAwaitable = TFutureAwaitable<T, true>; |
| 100 | + |
| 101 | +} // namespace NThreading |
| 102 | + |
| 103 | +template <typename T> |
| 104 | +auto operator co_await(const NThreading::TFuture<T>& future) noexcept { |
| 105 | + return NThreading::TFutureAwaitable{future}; |
| 106 | +} |
| 107 | + |
| 108 | +template <typename T> |
| 109 | +auto operator co_await(NThreading::TFuture<T>&& future) noexcept { |
| 110 | + // Not TExtractongFutureAwaitable, because TFuture works like std::shared_future. |
| 111 | + // auto value = co_await GetCachedFuture(); |
| 112 | + // If GetCachedFuture stores a future in some cache and returns its copies, |
| 113 | + // then subsequent uses of co_await will return a moved-from value. |
| 114 | + return NThreading::TFutureAwaitable{std::move(future)}; |
| 115 | +} |
| 116 | + |
| 117 | +namespace NThreading { |
| 118 | + |
| 119 | + template <typename T> |
| 120 | + auto AsAwaitable(const NThreading::TFuture<T>& fut) noexcept { |
| 121 | + return TFutureAwaitable(fut); |
| 122 | + } |
| 123 | + |
| 124 | + template <typename T> |
| 125 | + auto AsExtractingAwaitable(NThreading::TFuture<T>&& fut) noexcept { |
| 126 | + return TExtractingFutureAwaitable<T>(std::move(fut)); |
| 127 | + } |
| 128 | + |
| 129 | +} // namespace NThreading |
0 commit comments