Debian Codegraph @

src/core/lib/promise/latch.h

raw
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
18 #include <grpc/support/port_platform.h>
19
20 #include <grpc/support/log.h>
21
22 #include "src/core/lib/promise/intra_activity_waiter.h"
23 #include "src/core/lib/promise/poll.h"
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) {}
48 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) {
79 GPR_DEBUG_ASSERT(!has_value_);
80 value_ = std::move(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.
91 GPR_NO_UNIQUE_ADDRESS T value_;
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
98 IntraActivityWaiter waiter_;
99 };
100
101 } // namespace grpc_core
102
103 #endif // GRPC_CORE_LIB_PROMISE_LATCH_H