[c++14] Make latch impl simpler with c++14 (#31434)

* [c++14] Make latch impl simpler with c++14

* fix
pull/31617/head
Craig Tiller 2 years ago committed by GitHub
parent e0debec1b1
commit be85608ad1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 32
      src/core/lib/promise/latch.h
  2. 4
      src/core/lib/surface/call.cc

@ -17,6 +17,8 @@
#include <grpc/support/port_platform.h>
#include <type_traits>
#include <grpc/support/log.h>
#include "src/core/lib/promise/intra_activity_waiter.h"
@ -31,23 +33,6 @@ namespace grpc_core {
template <typename T>
class Latch {
public:
// This is the type of the promise returned by Wait.
class WaitPromise {
public:
Poll<T*> 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<T*> {
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 <typename T>
using LatchWaitPromise = decltype(std::declval<Latch<T>>().Wait());
} // namespace grpc_core
#endif // GRPC_CORE_LIB_PROMISE_LATCH_H

@ -2454,7 +2454,7 @@ class ClientPromiseBasedCall final : public PromiseBasedCall {
ABSL_GUARDED_BY(mu());
absl::optional<PipeReceiver<MessageHandle>::NextType> outstanding_recv_
ABSL_GUARDED_BY(mu());
absl::optional<Latch<ServerMetadata*>::WaitPromise>
absl::optional<LatchWaitPromise<ServerMetadata*>>
server_initial_metadata_ready_;
absl::optional<grpc_compression_algorithm> 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;

Loading…
Cancel
Save