Debian Codegraph @

src/core/lib/promise/arena_promise.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_ARENA_PROMISE_H
16 #define GRPC_CORE_LIB_PROMISE_ARENA_PROMISE_H
17
18 #include <grpc/support/port_platform.h>
19
20 #include <stdlib.h>
21
22 #include <memory>
23 #include <utility>
24
25 #include "absl/meta/type_traits.h"
26
27 #include "src/core/lib/gprpp/construct_destruct.h"
28 #include "src/core/lib/promise/context.h"
29 #include "src/core/lib/promise/poll.h"
30 #include "src/core/lib/resource_quota/arena.h"
31
32 namespace grpc_core {
33
34 namespace arena_promise_detail {
35
36 using ArgType = std::aligned_storage_t<sizeof(void*)>;
37 template <typename T>
38 T*& ArgAsPtr(ArgType* arg) {
39 static_assert(sizeof(ArgType) >= sizeof(T**),
40 "Must have ArgType of at least one pointer size");
41 return *reinterpret_cast<T**>(arg);
42 }
43
44 template <typename T>
45 struct Vtable {
46 // Poll the promise, once.
47 Poll<T> (*poll_once)(ArgType* arg);
48 // Destroy the underlying callable object if there is one.
49 // Since we don't delete (the arena owns the memory) but we may need to call a
50 // destructor, we expose this for when the ArenaPromise object is destroyed.
51 void (*destroy)(ArgType* arg);
52 };
53
54 template <typename T>
55 struct VtableAndArg {
56 const Vtable<T>* vtable;
57 ArgType arg;
58 };
59
60 // Implementation of Vtable for an empty object.
61 // Used when an empty ArenaPromise is created, or when the ArenaPromise is moved
62 // from. Since in either case these objects should not be polled, we simply
63 // crash if it is.
64 template <typename T>
65 struct Null {
66 static const Vtable<T> vtable;
67
68 static Poll<T> PollOnce(ArgType*) {
69 abort();
70 GPR_UNREACHABLE_CODE(return Pending{});
71 }
72
73 static void Destroy(ArgType*) {}
74 };
75
76 template <typename T>
77 const Vtable<T> Null<T>::vtable = {PollOnce, Destroy};
78
79 // Implementation of ImplInterface for a callable object.
80 template <typename T, typename Callable>
81 struct AllocatedCallable {
82 static const Vtable<T> vtable;
83
84 static Poll<T> PollOnce(ArgType* arg) {
85 return poll_cast<T>((*ArgAsPtr<Callable>(arg))());
86 }
87
88 static void Destroy(ArgType* arg) { Destruct(ArgAsPtr<Callable>(arg)); }
89 };
90
91 template <typename T, typename Callable>
92 const Vtable<T> AllocatedCallable<T, Callable>::vtable = {PollOnce, Destroy};
93
94 // Implementation of ImplInterface for a small callable object (one that fits
95 // within the ArgType arg)
96 template <typename T, typename Callable>
97 struct Inlined {
98 static const Vtable<T> vtable;
99
100 static Poll<T> PollOnce(ArgType* arg) {
101 return poll_cast<T>((*reinterpret_cast<Callable*>(arg))());
102 }
103
104 static void Destroy(ArgType* arg) {
105 Destruct(reinterpret_cast<Callable*>(arg));
106 }
107 };
108
109 template <typename T, typename Callable>
110 const Vtable<T> Inlined<T, Callable>::vtable = {PollOnce, Destroy};
111
112 // If a callable object is empty we can substitute any instance of that callable
113 // for the one we call (for how could we tell the difference)?
114 // Since this corresponds to a lambda with no fields, and we expect these to be
115 // reasonably common, we can elide the arena allocation entirely and simply poll
116 // a global shared instance.
117 // (this comes up often when the promise only accesses context data from the
118 // containing activity).
119 template <typename T, typename Callable>
120 struct SharedCallable {
121 static const Vtable<T> vtable;
122
123 static Poll<T> PollOnce(ArgType* arg) {
124 return (*reinterpret_cast<Callable*>(arg))();
125 }
126 };
127
128 template <typename T, typename Callable>
129 const Vtable<T> SharedCallable<T, Callable>::vtable = {PollOnce,
130 Null<T>::Destroy};
131
132 // Redirector type: given a callable type, expose a Make() function that creates
133 // the appropriate underlying implementation.
134 template <typename T, typename Callable, typename Ignored = void>
135 struct ChooseImplForCallable;
136
137 template <typename T, typename Callable>
138 struct ChooseImplForCallable<
139 T, Callable,
140 absl::enable_if_t<!std::is_empty<Callable>::value &&
141 (sizeof(Callable) > sizeof(ArgType))>> {
142 static void Make(Callable&& callable, VtableAndArg<T>* out) {
143 out->vtable = &AllocatedCallable<T, Callable>::vtable;
144 ArgAsPtr<Callable>(&out->arg) = GetContext<Arena>()->template New<Callable>(
145 std::forward<Callable>(callable));
146 }
147 };
148
149 template <typename T, typename Callable>
150 struct ChooseImplForCallable<
151 T, Callable,
152 absl::enable_if_t<!std::is_empty<Callable>::value &&
153 (sizeof(Callable) <= sizeof(ArgType))>> {
154 static void Make(Callable&& callable, VtableAndArg<T>* out) {
155 out->vtable = &Inlined<T, Callable>::vtable;
156 Construct(reinterpret_cast<Callable*>(&out->arg),
157 std::forward<Callable>(callable));
158 }
159 };
160
161 template <typename T, typename Callable>
162 struct ChooseImplForCallable<
163 T, Callable, absl::enable_if_t<std::is_empty<Callable>::value>> {
164 static void Make(Callable&&, VtableAndArg<T>* out) {
165 out->vtable = &SharedCallable<T, Callable>::vtable;
166 }
167 };
168
169 // Wrap ChooseImplForCallable with a friend approachable syntax.
170 template <typename T, typename Callable>
171 void MakeImplForCallable(Callable&& callable, VtableAndArg<T>* out) {
172 ChooseImplForCallable<T, Callable>::Make(std::forward<Callable>(callable),
173 out);
174 }
175
176 } // namespace arena_promise_detail
177
178 // A promise for which the state memory is allocated from an arena.
179 template <typename T>
180 class ArenaPromise {
181 public:
182 // Construct an empty, uncallable, invalid ArenaPromise.
183 ArenaPromise() = default;
184
185 // Construct an ArenaPromise that will call the given callable when polled.
186 template <typename Callable,
187 typename Ignored =
188 absl::enable_if_t<!std::is_same<Callable, ArenaPromise>::value>>
189 // NOLINTNEXTLINE(google-explicit-constructor)
190 ArenaPromise(Callable&& callable) {
191 arena_promise_detail::MakeImplForCallable(std::forward<Callable>(callable),
192 &vtable_and_arg_);
193 }
194
195 // ArenaPromise is not copyable.
196 ArenaPromise(const ArenaPromise&) = delete;
197 ArenaPromise& operator=(const ArenaPromise&) = delete;
198 // ArenaPromise is movable.
199 ArenaPromise(ArenaPromise&& other) noexcept
200 : vtable_and_arg_(other.vtable_and_arg_) {
201 other.vtable_and_arg_.vtable = &arena_promise_detail::Null<T>::vtable;
202 }
203 ArenaPromise& operator=(ArenaPromise&& other) noexcept {
204 vtable_and_arg_.vtable->destroy(&vtable_and_arg_.arg);
205 vtable_and_arg_ = other.vtable_and_arg_;
206 other.vtable_and_arg_.vtable = &arena_promise_detail::Null<T>::vtable;
207 return *this;
208 }
209
210 // Destruction => call Destroy on the underlying impl object.
211 ~ArenaPromise() { vtable_and_arg_.vtable->destroy(&vtable_and_arg_.arg); }
212
213 // Expose the promise interface: a call operator that returns Poll<T>.
214 Poll<T> operator()() {
215 return vtable_and_arg_.vtable->poll_once(&vtable_and_arg_.arg);
216 }
217
218 bool has_value() const {
219 return vtable_and_arg_.vtable != &arena_promise_detail::Null<T>::vtable;
220 }
221
222 private:
223 // Underlying impl object.
224 arena_promise_detail::VtableAndArg<T> vtable_and_arg_ = {
225 &arena_promise_detail::Null<T>::vtable, {}};
226 };
227
228 } // namespace grpc_core
229
230 #endif /* GRPC_CORE_LIB_PROMISE_ARENA_PROMISE_H */