latch.h
Go to the documentation of this file.
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_LATCH_H
16 #define GRPC_CORE_LIB_PROMISE_LATCH_H
17 
19 
20 #include <grpc/support/log.h>
21 
24 
25 namespace grpc_core {
26 
27 // Latch provides a single set waitable object.
28 // Initially the Latch is unset.
29 // It can be waited upon by the Wait method, which produces a Promise that
30 // resolves when the Latch is Set to a value of type T.
31 template <typename T>
32 class Latch {
33  public:
34  // This is the type of the promise returned by Wait.
35  class WaitPromise {
36  public:
37  Poll<T*> operator()() const {
38  if (latch_->has_value_) {
39  return &latch_->value_;
40  } else {
41  return latch_->waiter_.pending();
42  }
43  }
44 
45  private:
46  friend class Latch;
47  explicit WaitPromise(Latch* latch) : latch_(latch) {}
49  };
50 
51  Latch() = default;
52  Latch(const Latch&) = delete;
53  Latch& operator=(const Latch&) = delete;
54  Latch(Latch&& other) noexcept
55  : value_(std::move(other.value_)), has_value_(other.has_value_) {
56 #ifndef NDEBUG
57  GPR_DEBUG_ASSERT(!other.has_had_waiters_);
58 #endif
59  }
60  Latch& operator=(Latch&& other) noexcept {
61 #ifndef NDEBUG
62  GPR_DEBUG_ASSERT(!other.has_had_waiters_);
63 #endif
64  value_ = std::move(other.value_);
65  has_value_ = other.has_value_;
66  return *this;
67  }
68 
69  // Produce a promise to wait for a value from this latch.
70  WaitPromise Wait() {
71 #ifndef NDEBUG
72  has_had_waiters_ = true;
73 #endif
74  return WaitPromise(this);
75  }
76 
77  // Set the value of the latch. Can only be called once.
78  void Set(T value) {
81  has_value_ = true;
82  waiter_.Wake();
83  }
84 
85  private:
86  // The value stored (if has_value_ is true), otherwise some random value, we
87  // don't care.
88  // Why not absl::optional<>? Writing things this way lets us compress
89  // has_value_ with waiter_ and leads to some significant memory savings for
90  // some scenarios.
92  // True if we have a value set, false otherwise.
93  bool has_value_ = false;
94 #ifndef NDEBUG
95  // Has this latch ever had waiters.
96  bool has_had_waiters_ = false;
97 #endif
99 };
100 
101 } // namespace grpc_core
102 
103 #endif // GRPC_CORE_LIB_PROMISE_LATCH_H
grpc_core::Latch::Latch
Latch()=default
log.h
grpc_core::Latch::WaitPromise::latch_
Latch * latch_
Definition: latch.h:48
GPR_DEBUG_ASSERT
#define GPR_DEBUG_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:103
grpc_core::Latch::operator=
Latch & operator=(Latch &&other) noexcept
Definition: latch.h:60
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::IntraActivityWaiter
Definition: intra_activity_waiter.h:27
grpc_core::Latch::Wait
WaitPromise Wait()
Definition: latch.h:70
grpc_core::Latch::has_value_
bool has_value_
Definition: latch.h:93
T
#define T(upbtypeconst, upbtype, ctype, default_value)
grpc_core::Latch::WaitPromise::WaitPromise
WaitPromise(Latch *latch)
Definition: latch.h:47
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc_core::Latch::WaitPromise
Definition: latch.h:35
intra_activity_waiter.h
GPR_NO_UNIQUE_ADDRESS
#define GPR_NO_UNIQUE_ADDRESS
Definition: impl/codegen/port_platform.h:692
grpc_core::Latch::value_
GPR_NO_UNIQUE_ADDRESS T value_
Definition: latch.h:91
value
const char * value
Definition: hpack_parser_table.cc:165
poll.h
grpc_core::IntraActivityWaiter::pending
Pending pending()
Definition: intra_activity_waiter.h:31
grpc_core::IntraActivityWaiter::Wake
void Wake()
Definition: intra_activity_waiter.h:36
grpc_core::Latch::has_had_waiters_
bool has_had_waiters_
Definition: latch.h:96
grpc_core::Latch::WaitPromise::operator()
Poll< T * > operator()() const
Definition: latch.h:37
grpc_core::Latch::operator=
Latch & operator=(const Latch &)=delete
grpc_core::Latch::Set
void Set(T value)
Definition: latch.h:78
absl::variant
Definition: abseil-cpp/absl/types/internal/variant.h:46
grpc_core::Latch::Latch
Latch(Latch &&other) noexcept
Definition: latch.h:54
grpc_core::Latch
Definition: latch.h:32
grpc_core::Latch::waiter_
IntraActivityWaiter waiter_
Definition: latch.h:98
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:27