From be85608ad1f77a33e474791d1bfe2c6cd86e1a9f Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Thu, 10 Nov 2022 13:08:05 -0800 Subject: [PATCH] [c++14] Make latch impl simpler with c++14 (#31434) * [c++14] Make latch impl simpler with c++14 * fix --- src/core/lib/promise/latch.h | 32 +++++++++++++------------------- src/core/lib/surface/call.cc | 4 ++-- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/core/lib/promise/latch.h b/src/core/lib/promise/latch.h index a43b92d8890..b0a3671bf73 100644 --- a/src/core/lib/promise/latch.h +++ b/src/core/lib/promise/latch.h @@ -17,6 +17,8 @@ #include +#include + #include #include "src/core/lib/promise/intra_activity_waiter.h" @@ -31,23 +33,6 @@ namespace grpc_core { template class Latch { public: - // This is the type of the promise returned by Wait. - class WaitPromise { - public: - Poll operator()() const { - if (latch_->has_value_) { - return &latch_->value_; - } else { - return latch_->waiter_.pending(); - } - } - - private: - friend class Latch; - explicit WaitPromise(Latch* latch) : latch_(latch) {} - Latch* latch_; - }; - Latch() = default; Latch(const Latch&) = delete; Latch& operator=(const Latch&) = delete; @@ -67,11 +52,17 @@ class Latch { } // Produce a promise to wait for a value from this latch. - WaitPromise Wait() { + auto Wait() { #ifndef NDEBUG has_had_waiters_ = true; #endif - return WaitPromise(this); + return [this]() -> Poll { + if (has_value_) { + return &value_; + } else { + return waiter_.pending(); + } + }; } // Set the value of the latch. Can only be called once. @@ -98,6 +89,9 @@ class Latch { IntraActivityWaiter waiter_; }; +template +using LatchWaitPromise = decltype(std::declval>().Wait()); + } // namespace grpc_core #endif // GRPC_CORE_LIB_PROMISE_LATCH_H diff --git a/src/core/lib/surface/call.cc b/src/core/lib/surface/call.cc index 318537041e6..fe3ed7a3c51 100644 --- a/src/core/lib/surface/call.cc +++ b/src/core/lib/surface/call.cc @@ -2454,7 +2454,7 @@ class ClientPromiseBasedCall final : public PromiseBasedCall { ABSL_GUARDED_BY(mu()); absl::optional::NextType> outstanding_recv_ ABSL_GUARDED_BY(mu()); - absl::optional::WaitPromise> + absl::optional> server_initial_metadata_ready_; absl::optional incoming_compression_algorithm_; Completion recv_initial_metadata_completion_ ABSL_GUARDED_BY(mu()); @@ -2537,7 +2537,7 @@ void ClientPromiseBasedCall::CommitBatch(const grpc_op* ops, size_t nops, case GRPC_OP_RECV_INITIAL_METADATA: { recv_initial_metadata_ = op.data.recv_initial_metadata.recv_initial_metadata; - server_initial_metadata_ready_ = server_initial_metadata_.Wait(); + server_initial_metadata_ready_.emplace(server_initial_metadata_.Wait()); recv_initial_metadata_completion_ = AddOpToCompletion(completion, PendingOp::kReceiveInitialMetadata); } break;