| 1 |
// Copyright 2021 gRPC authors. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#ifndef GRPC_CORE_LIB_PROMISE_LOOP_H |
| 16 |
#define GRPC_CORE_LIB_PROMISE_LOOP_H |
| 17 |
|
| 18 |
#include <grpc/support/port_platform.h> |
| 19 |
|
| 20 |
#include <new> |
| 21 |
#include <type_traits> |
| 22 |
#include <utility> |
| 23 |
|
| 24 |
#include "absl/status/status.h" |
| 25 |
#include "absl/status/statusor.h" |
| 26 |
#include "absl/types/variant.h" |
| 27 |
|
| 28 |
#include "src/core/lib/promise/detail/promise_factory.h" |
| 29 |
#include "src/core/lib/promise/poll.h" |
| 30 |
|
| 31 |
namespace grpc_core { |
| 32 |
|
| 33 |
// Special type - signals to loop to take another iteration, instead of |
| 34 |
// finishing |
| 35 |
struct Continue {}; |
| 36 |
|
| 37 |
// Result of polling a loop promise - either Continue looping, or return a value |
| 38 |
// T |
| 39 |
template <typename T> |
| 40 |
using LoopCtl = absl::variant<Continue, T>; |
| 41 |
|
| 42 |
namespace promise_detail { |
| 43 |
|
| 44 |
template <typename T> |
| 45 |
struct LoopTraits; |
| 46 |
|
| 47 |
template <typename T> |
| 48 |
struct LoopTraits<LoopCtl<T>> { |
| 49 |
using Result = T; |
| 50 |
static LoopCtl<T> ToLoopCtl(LoopCtl<T> value) { return value; } |
| 51 |
}; |
| 52 |
|
| 53 |
template <typename T> |
| 54 |
struct LoopTraits<absl::StatusOr<LoopCtl<T>>> { |
| 55 |
using Result = absl::StatusOr<T>; |
| 56 |
static LoopCtl<Result> ToLoopCtl(absl::StatusOr<LoopCtl<T>> value) { |
| 57 |
if (!value.ok()) return value.status(); |
| 58 |
const auto& inner = *value; |
| 59 |
if (absl::holds_alternative<Continue>(inner)) return Continue{}; |
| 60 |
return absl::get<T>(inner); |
| 61 |
} |
| 62 |
}; |
| 63 |
|
| 64 |
template <> |
| 65 |
struct LoopTraits<absl::StatusOr<LoopCtl<absl::Status>>> { |
| 66 |
using Result = absl::Status; |
| 67 |
static LoopCtl<Result> ToLoopCtl( |
| 68 |
absl::StatusOr<LoopCtl<absl::Status>> value) { |
| 69 |
if (!value.ok()) return value.status(); |
| 70 |
const auto& inner = *value; |
| 71 |
if (absl::holds_alternative<Continue>(inner)) return Continue{}; |
| 72 |
return absl::get<absl::Status>(inner); |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
template <typename F> |
| 77 |
class Loop { |
| 78 |
private: |
| 79 |
using Factory = promise_detail::RepeatedPromiseFactory<void, F>; |
| 80 |
using PromiseType = decltype(std::declval<Factory>().Make()); |
| 81 |
using PromiseResult = typename PromiseType::Result; |
| 82 |
|
| 83 |
public: |
| 84 |
using Result = typename LoopTraits<PromiseResult>::Result; |
| 85 |
|
| 86 |
explicit Loop(F f) : factory_(std::move(f)), promise_(factory_.Make()) {} |
| 87 |
~Loop() { promise_.~PromiseType(); } |
| 88 |
|
| 89 |
Loop(Loop&& loop) noexcept |
| 90 |
: factory_(std::move(loop.factory_)), |
| 91 |
promise_(std::move(loop.promise_)) {} |
| 92 |
|
| 93 |
Loop(const Loop& loop) = delete; |
| 94 |
Loop& operator=(const Loop& loop) = delete; |
| 95 |
|
| 96 |
Poll<Result> operator()() { |
| 97 |
while (true) { |
| 98 |
// Poll the inner promise. |
| 99 |
auto promise_result = promise_(); |
| 100 |
// If it returns a value: |
| 101 |
if (auto* p = absl::get_if<kPollReadyIdx>(&promise_result)) { |
| 102 |
// - then if it's Continue, destroy the promise and recreate a new one |
| 103 |
// from our factory. |
| 104 |
auto lc = LoopTraits<PromiseResult>::ToLoopCtl(*p); |
| 105 |
if (absl::holds_alternative<Continue>(lc)) { |
| 106 |
promise_.~PromiseType(); |
| 107 |
new (&promise_) PromiseType(factory_.Make()); |
| 108 |
continue; |
| 109 |
} |
| 110 |
// - otherwise there's our result... return it out. |
| 111 |
return absl::get<Result>(lc); |
| 112 |
} else { |
| 113 |
// Otherwise the inner promise was pending, so we are pending. |
| 114 |
return Pending(); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
private: |
| 120 |
GPR_NO_UNIQUE_ADDRESS Factory factory_; |
| 121 |
GPR_NO_UNIQUE_ADDRESS union { |
| 122 |
GPR_NO_UNIQUE_ADDRESS PromiseType promise_; |
| 123 |
}; |
| 124 |
}; |
| 125 |
|
| 126 |
} // namespace promise_detail |
| 127 |
|
| 128 |
// Looping combinator. |
| 129 |
// Expects F returns LoopCtl<T> - if it's Continue, then run the loop again - |
| 130 |
// otherwise yield the returned value as the result of the loop. |
| 131 |
template <typename F> |
| 132 |
promise_detail::Loop<F> Loop(F f) { |
| 133 |
return promise_detail::Loop<F>(std::move(f)); |
| 134 |
} |
| 135 |
|
| 136 |
} // namespace grpc_core |
| 137 |
|
| 138 |
#endif // GRPC_CORE_LIB_PROMISE_LOOP_H |