// Copyright 2021 gRPC authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef GRPC_SRC_CORE_LIB_PROMISE_ALL_OK_H #define GRPC_SRC_CORE_LIB_PROMISE_ALL_OK_H #include #include #include #include "absl/meta/type_traits.h" #include "absl/status/status.h" #include "absl/status/statusor.h" #include "src/core/lib/promise/detail/join_state.h" #include "src/core/lib/promise/detail/promise_factory.h" #include "src/core/lib/promise/map.h" #include "src/core/lib/promise/poll.h" #include "src/core/lib/promise/status_flag.h" namespace grpc_core { namespace promise_detail { // Traits object to pass to JoinState template struct AllOkTraits { template using ResultType = Result; template GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION static bool IsOk(const T& x) { return IsStatusOk(x); } GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION static Empty Unwrapped(StatusFlag) { return Empty{}; } GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION static Empty Unwrapped(absl::Status) { return Empty{}; } template GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION static R EarlyReturn(T&& x) { return StatusCast(std::forward(x)); } template GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION static Result FinalReturn(A&&...) { return Result{}; } }; // Implementation of AllOk combinator. template class AllOk { public: GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION explicit AllOk(Promises... promises) : state_(std::move(promises)...) {} GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION auto operator()() { return state_.PollOnce(); } private: JoinState, Promises...> state_; }; } // namespace promise_detail // Run all promises. // If any fail, cancel the rest and return the failure. // If all succeed, return Ok. template GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION inline auto AllOk(Promises... promises) { return promise_detail::AllOk(std::move(promises)...); } // Construct a promise for each element of the set, then run them all. // If any fail, cancel the rest and return the failure. // If all succeed, return Ok. template inline auto AllOkIter(Iter begin, Iter end, FactoryFn factory_fn) { using Factory = promise_detail::RepeatedPromiseFactory; Factory factory(std::move(factory_fn)); using Promise = typename Factory::Promise; std::vector promises; std::vector done; for (auto it = begin; it != end; ++it) { promises.emplace_back(factory.Make(*it)); done.push_back(false); } return [promises = std::move(promises), done = std::move(done)]() mutable -> Poll { using Traits = promise_detail::AllOkTraits; bool still_working = false; for (size_t i = 0; i < promises.size(); ++i) { if (done[i]) continue; auto p = promises[i](); if (auto* r = p.value_if_ready()) { if (!Traits::IsOk(*r)) { return Traits::template EarlyReturn(std::move(*r)); } done[i] = true; } else { still_working = true; } } if (still_working) return Pending{}; return Traits::FinalReturn(); }; } } // namespace grpc_core #endif // GRPC_SRC_CORE_LIB_PROMISE_ALL_OK_H