| 1 |
/* |
| 2 |
* Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <folly/CancellationToken.h> |
| 18 |
#include <folly/experimental/coro/Baton.h> |
| 19 |
#include <folly/experimental/coro/WithCancellation.h> |
| 20 |
|
| 21 |
#if FOLLY_HAS_COROUTINES |
| 22 |
|
| 23 |
namespace folly::coro { |
| 24 |
|
| 25 |
namespace detail { |
| 26 |
|
| 27 |
template <bool> |
| 28 |
struct DiscardImpl { |
| 29 |
folly::coro::Baton baton; |
| 30 |
exception_wrapper timeoutResult; |
| 31 |
bool parentCancelled = false; |
| 32 |
bool checkedTimeout = false; |
| 33 |
}; |
| 34 |
|
| 35 |
template <> |
| 36 |
struct DiscardImpl<false> {}; |
| 37 |
|
| 38 |
template <typename SemiAwaitable, typename Duration, bool discard> |
| 39 |
Task<typename semi_await_try_result_t<SemiAwaitable>::element_type> timeoutImpl( |
| 40 |
SemiAwaitable semiAwaitable, Duration timeoutDuration, Timekeeper* tk) { |
| 41 |
CancellationSource cancelSource; |
| 42 |
DiscardImpl<discard> impl; |
| 43 |
auto sleepFuture = |
| 44 |
folly::futures::sleep(timeoutDuration, tk).toUnsafeFuture(); |
| 45 |
sleepFuture.setCallback_( |
| 46 |
[&, cancelSource](Executor::KeepAlive<>&&, Try<Unit>&& result) noexcept { |
| 47 |
if constexpr (discard) { |
| 48 |
if (result.hasException()) { |
| 49 |
impl.timeoutResult = std::move(result.exception()); |
| 50 |
} else { |
| 51 |
impl.timeoutResult = folly::make_exception_wrapper<FutureTimeout>(); |
| 52 |
} |
| 53 |
impl.baton.post(); |
| 54 |
} |
| 55 |
cancelSource.requestCancellation(); |
| 56 |
}); |
| 57 |
|
| 58 |
bool isSleepCancelled = false; |
| 59 |
auto tryCancelSleep = [&]() noexcept { |
| 60 |
if (!isSleepCancelled) { |
| 61 |
isSleepCancelled = true; |
| 62 |
sleepFuture.cancel(); |
| 63 |
} |
| 64 |
}; |
| 65 |
|
| 66 |
std::optional<CancellationCallback> cancelCallback{ |
| 67 |
std::in_place, co_await co_current_cancellation_token, [&]() { |
| 68 |
cancelSource.requestCancellation(); |
| 69 |
tryCancelSleep(); |
| 70 |
if constexpr (discard) { |
| 71 |
impl.parentCancelled = true; |
| 72 |
} |
| 73 |
}}; |
| 74 |
|
| 75 |
exception_wrapper error; |
| 76 |
try { |
| 77 |
auto resultTry = |
| 78 |
co_await folly::coro::co_awaitTry(folly::coro::co_withCancellation( |
| 79 |
cancelSource.getToken(), std::move(semiAwaitable))); |
| 80 |
|
| 81 |
cancelCallback.reset(); |
| 82 |
|
| 83 |
if constexpr (discard) { |
| 84 |
if (!impl.parentCancelled && impl.baton.ready()) { |
| 85 |
// Timer already fired |
| 86 |
co_yield folly::coro::co_error(std::move(impl.timeoutResult)); |
| 87 |
} |
| 88 |
impl.checkedTimeout = true; |
| 89 |
} |
| 90 |
|
| 91 |
tryCancelSleep(); |
| 92 |
if constexpr (discard) { |
| 93 |
co_await impl.baton; |
| 94 |
} |
| 95 |
|
| 96 |
if (resultTry.hasException()) { |
| 97 |
co_yield folly::coro::co_error(std::move(resultTry).exception()); |
| 98 |
} |
| 99 |
|
| 100 |
co_return std::move(resultTry).value(); |
| 101 |
} catch (...) { |
| 102 |
error = exception_wrapper{std::current_exception()}; |
| 103 |
} |
| 104 |
|
| 105 |
assert(error); |
| 106 |
|
| 107 |
cancelCallback.reset(); |
| 108 |
|
| 109 |
if constexpr (discard) { |
| 110 |
if (!impl.checkedTimeout && !impl.parentCancelled && impl.baton.ready()) { |
| 111 |
// Timer already fired |
| 112 |
co_yield folly::coro::co_error(std::move(impl.timeoutResult)); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
tryCancelSleep(); |
| 117 |
if constexpr (discard) { |
| 118 |
co_await impl.baton; |
| 119 |
} |
| 120 |
|
| 121 |
co_yield folly::coro::co_error(std::move(error)); |
| 122 |
} |
| 123 |
|
| 124 |
} // namespace detail |
| 125 |
|
| 126 |
template <typename SemiAwaitable, typename Duration> |
| 127 |
Task<typename semi_await_try_result_t<SemiAwaitable>::element_type> timeout( |
| 128 |
SemiAwaitable semiAwaitable, Duration timeoutDuration, Timekeeper* tk) { |
| 129 |
return detail::timeoutImpl<SemiAwaitable, Duration, /*discard=*/true>( |
| 130 |
std::move(semiAwaitable), timeoutDuration, tk); |
| 131 |
} |
| 132 |
|
| 133 |
template <typename SemiAwaitable, typename Duration> |
| 134 |
Task<typename semi_await_try_result_t<SemiAwaitable>::element_type> |
| 135 |
timeoutNoDiscard( |
| 136 |
SemiAwaitable semiAwaitable, Duration timeoutDuration, Timekeeper* tk) { |
| 137 |
return detail::timeoutImpl<SemiAwaitable, Duration, /*discard=*/false>( |
| 138 |
std::move(semiAwaitable), timeoutDuration, tk); |
| 139 |
} |
| 140 |
|
| 141 |
} // namespace folly::coro |
| 142 |
|
| 143 |
#endif // FOLLY_HAS_COROUTINES |