1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #ifndef GRPC_CORE_LIB_IOMGR_CLOSURE_H
20 #define GRPC_CORE_LIB_IOMGR_CLOSURE_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include <assert.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include <stdbool.h>
28 #include "src/core/lib/gpr/mpscq.h"
29 #include "src/core/lib/iomgr/error.h"
30 #include "src/core/lib/profiling/timers.h"
31
32 struct grpc_closure;
33 typedef struct grpc_closure grpc_closure;
34
35 extern grpc_core::DebugOnlyTraceFlag grpc_trace_closure;
36
37 typedef struct grpc_closure_list {
38 grpc_closure* head;
39 grpc_closure* tail;
40 } grpc_closure_list;
41
42 /** gRPC Callback definition.
43 *
44 * \param arg Arbitrary input.
45 * \param error GRPC_ERROR_NONE if no error occurred, otherwise some grpc_error
46 * describing what went wrong.
47 * Error contract: it is not the cb's job to unref this error;
48 * the closure scheduler will do that after the cb returns */
49 typedef void (*grpc_iomgr_cb_func)(void* arg, grpc_error* error);
50
51 typedef struct grpc_closure_scheduler grpc_closure_scheduler;
52
53 typedef struct grpc_closure_scheduler_vtable {
54 /* NOTE: for all these functions, closure->scheduler == the scheduler that was
55 used to find this vtable */
56 void (*run)(grpc_closure* closure, grpc_error* error);
57 void (*sched)(grpc_closure* closure, grpc_error* error);
58 const char* name;
59 } grpc_closure_scheduler_vtable;
60
61 /** Abstract type that can schedule closures for execution */
62 struct grpc_closure_scheduler {
63 const grpc_closure_scheduler_vtable* vtable;
64 };
65
66 /** A closure over a grpc_iomgr_cb_func. */
67 struct grpc_closure {
68 /** Once queued, next indicates the next queued closure; before then, scratch
69 * space */
70 union {
71 grpc_closure* next;
72 gpr_mpscq_node atm_next;
73 uintptr_t scratch;
74 } next_data;
75
76 /** Bound callback. */
77 grpc_iomgr_cb_func cb;
78
79 /** Arguments to be passed to "cb". */
80 void* cb_arg;
81
82 /** Scheduler to schedule against: nullptr to schedule against current
83 execution context */
84 grpc_closure_scheduler* scheduler;
85
86 /** Once queued, the result of the closure. Before then: scratch space */
87 union {
88 grpc_error* error;
89 uintptr_t scratch;
90 } error_data;
91
92 // extra tracing and debugging for grpc_closure. This incurs a decent amount of
93 // overhead per closure, so it must be enabled at compile time.
94 #ifndef NDEBUG
95 bool scheduled;
96 bool run; // true = run, false = scheduled
97 const char* file_created;
98 int line_created;
99 const char* file_initiated;
100 int line_initiated;
101 #endif
102 };
103
104 #ifndef NDEBUG
grpc_closure_init(const char * file,int line,grpc_closure * closure,grpc_iomgr_cb_func cb,void * cb_arg,grpc_closure_scheduler * scheduler)105 inline grpc_closure* grpc_closure_init(const char* file, int line,
106 grpc_closure* closure,
107 grpc_iomgr_cb_func cb, void* cb_arg,
108 grpc_closure_scheduler* scheduler) {
109 #else
110 inline grpc_closure* grpc_closure_init(grpc_closure* closure,
111 grpc_iomgr_cb_func cb, void* cb_arg,
112 grpc_closure_scheduler* scheduler) {
113 #endif
114 closure->cb = cb;
115 closure->cb_arg = cb_arg;
116 closure->scheduler = scheduler;
117 #ifndef NDEBUG
118 closure->scheduled = false;
119 closure->file_initiated = nullptr;
120 closure->line_initiated = 0;
121 closure->run = false;
122 closure->file_created = file;
123 closure->line_created = line;
124 #endif
125 return closure;
126 }
127
128 /** Initializes \a closure with \a cb and \a cb_arg. Returns \a closure. */
129 #ifndef NDEBUG
130 #define GRPC_CLOSURE_INIT(closure, cb, cb_arg, scheduler) \
131 grpc_closure_init(__FILE__, __LINE__, closure, cb, cb_arg, scheduler)
132 #else
133 #define GRPC_CLOSURE_INIT(closure, cb, cb_arg, scheduler) \
134 grpc_closure_init(closure, cb, cb_arg, scheduler)
135 #endif
136
137 namespace closure_impl {
138
139 typedef struct {
140 grpc_iomgr_cb_func cb;
141 void* cb_arg;
142 grpc_closure wrapper;
143 } wrapped_closure;
144
145 inline void closure_wrapper(void* arg, grpc_error* error) {
146 wrapped_closure* wc = static_cast<wrapped_closure*>(arg);
147 grpc_iomgr_cb_func cb = wc->cb;
148 void* cb_arg = wc->cb_arg;
149 gpr_free(wc);
150 cb(cb_arg, error);
151 }
152
153 } // namespace closure_impl
154
155 #ifndef NDEBUG
156 inline grpc_closure* grpc_closure_create(const char* file, int line,
157 grpc_iomgr_cb_func cb, void* cb_arg,
158 grpc_closure_scheduler* scheduler) {
159 #else
160 inline grpc_closure* grpc_closure_create(grpc_iomgr_cb_func cb, void* cb_arg,
161 grpc_closure_scheduler* scheduler) {
162 #endif
163 closure_impl::wrapped_closure* wc =
164 static_cast<closure_impl::wrapped_closure*>(gpr_malloc(sizeof(*wc)));
165 wc->cb = cb;
166 wc->cb_arg = cb_arg;
167 #ifndef NDEBUG
168 grpc_closure_init(file, line, &wc->wrapper, closure_impl::closure_wrapper, wc,
169 scheduler);
170 #else
171 grpc_closure_init(&wc->wrapper, closure_impl::closure_wrapper, wc, scheduler);
172 #endif
173 return &wc->wrapper;
174 }
175
176 /* Create a heap allocated closure: try to avoid except for very rare events */
177 #ifndef NDEBUG
178 #define GRPC_CLOSURE_CREATE(cb, cb_arg, scheduler) \
179 grpc_closure_create(__FILE__, __LINE__, cb, cb_arg, scheduler)
180 #else
181 #define GRPC_CLOSURE_CREATE(cb, cb_arg, scheduler) \
182 grpc_closure_create(cb, cb_arg, scheduler)
183 #endif
184
185 #define GRPC_CLOSURE_LIST_INIT \
186 { nullptr, nullptr }
187
188 inline void grpc_closure_list_init(grpc_closure_list* closure_list) {
189 closure_list->head = closure_list->tail = nullptr;
190 }
191
192 /** add \a closure to the end of \a list
193 and set \a closure's result to \a error
194 Returns true if \a list becomes non-empty */
195 inline bool grpc_closure_list_append(grpc_closure_list* closure_list,
196 grpc_closure* closure, grpc_error* error) {
197 if (closure == nullptr) {
198 GRPC_ERROR_UNREF(error);
199 return false;
200 }
201 closure->error_data.error = error;
202 closure->next_data.next = nullptr;
203 bool was_empty = (closure_list->head == nullptr);
204 if (was_empty) {
205 closure_list->head = closure;
206 } else {
207 closure_list->tail->next_data.next = closure;
208 }
209 closure_list->tail = closure;
210 return was_empty;
211 }
212
213 /** force all success bits in \a list to false */
214 inline void grpc_closure_list_fail_all(grpc_closure_list* list,
215 grpc_error* forced_failure) {
216 for (grpc_closure* c = list->head; c != nullptr; c = c->next_data.next) {
217 if (c->error_data.error == GRPC_ERROR_NONE) {
218 c->error_data.error = GRPC_ERROR_REF(forced_failure);
219 }
220 }
221 GRPC_ERROR_UNREF(forced_failure);
222 }
223
224 /** append all closures from \a src to \a dst and empty \a src. */
225 inline void grpc_closure_list_move(grpc_closure_list* src,
226 grpc_closure_list* dst) {
227 if (src->head == nullptr) {
228 return;
229 }
230 if (dst->head == nullptr) {
231 *dst = *src;
232 } else {
233 dst->tail->next_data.next = src->head;
234 dst->tail = src->tail;
235 }
236 src->head = src->tail = nullptr;
237 }
238
239 /** return whether \a list is empty. */
240 inline bool grpc_closure_list_empty(grpc_closure_list closure_list) {
241 return closure_list.head == nullptr;
242 }
243
244 #ifndef NDEBUG
245 inline void grpc_closure_run(const char* file, int line, grpc_closure* c,
246 grpc_error* error) {
247 #else
248 inline void grpc_closure_run(grpc_closure* c, grpc_error* error) {
249 #endif
250 GPR_TIMER_SCOPE("grpc_closure_run", 0);
251 if (c != nullptr) {
252 #ifndef NDEBUG
253 c->file_initiated = file;
254 c->line_initiated = line;
255 c->run = true;
256 GPR_ASSERT(c->cb != nullptr);
257 #endif
258 c->scheduler->vtable->run(c, error);
259 } else {
260 GRPC_ERROR_UNREF(error);
261 }
262 }
263
264 /** Run a closure directly. Caller ensures that no locks are being held above.
265 * Note that calling this at the end of a closure callback function itself is
266 * by definition safe. */
267 #ifndef NDEBUG
268 #define GRPC_CLOSURE_RUN(closure, error) \
269 grpc_closure_run(__FILE__, __LINE__, closure, error)
270 #else
271 #define GRPC_CLOSURE_RUN(closure, error) grpc_closure_run(closure, error)
272 #endif
273
274 #ifndef NDEBUG
275 inline void grpc_closure_sched(const char* file, int line, grpc_closure* c,
276 grpc_error* error) {
277 #else
278 inline void grpc_closure_sched(grpc_closure* c, grpc_error* error) {
279 #endif
280 GPR_TIMER_SCOPE("grpc_closure_sched", 0);
281 if (c != nullptr) {
282 #ifndef NDEBUG
283 if (c->scheduled) {
284 gpr_log(GPR_ERROR,
285 "Closure already scheduled. (closure: %p, created: [%s:%d], "
286 "previously scheduled at: [%s: %d], newly scheduled at [%s: %d], "
287 "run?: %s",
288 c, c->file_created, c->line_created, c->file_initiated,
289 c->line_initiated, file, line, c->run ? "true" : "false");
290 abort();
291 }
292 c->scheduled = true;
293 c->file_initiated = file;
294 c->line_initiated = line;
295 c->run = false;
296 GPR_ASSERT(c->cb != nullptr);
297 #endif
298 c->scheduler->vtable->sched(c, error);
299 } else {
300 GRPC_ERROR_UNREF(error);
301 }
302 }
303
304 /** Schedule a closure to be run. Does not need to be run from a safe point. */
305 #ifndef NDEBUG
306 #define GRPC_CLOSURE_SCHED(closure, error) \
307 grpc_closure_sched(__FILE__, __LINE__, closure, error)
308 #else
309 #define GRPC_CLOSURE_SCHED(closure, error) grpc_closure_sched(closure, error)
310 #endif
311
312 #ifndef NDEBUG
313 inline void grpc_closure_list_sched(const char* file, int line,
314 grpc_closure_list* list) {
315 #else
316 inline void grpc_closure_list_sched(grpc_closure_list* list) {
317 #endif
318 grpc_closure* c = list->head;
319 while (c != nullptr) {
320 grpc_closure* next = c->next_data.next;
321 #ifndef NDEBUG
322 if (c->scheduled) {
323 gpr_log(GPR_ERROR,
324 "Closure already scheduled. (closure: %p, created: [%s:%d], "
325 "previously scheduled at: [%s: %d] run?: %s",
326 c, c->file_created, c->line_created, c->file_initiated,
327 c->line_initiated, c->run ? "true" : "false");
328 abort();
329 }
330 c->scheduled = true;
331 c->file_initiated = file;
332 c->line_initiated = line;
333 c->run = false;
334 GPR_ASSERT(c->cb != nullptr);
335 #endif
336 c->scheduler->vtable->sched(c, c->error_data.error);
337 c = next;
338 }
339 list->head = list->tail = nullptr;
340 }
341
342 /** Schedule all closures in a list to be run. Does not need to be run from a
343 * safe point. */
344 #ifndef NDEBUG
345 #define GRPC_CLOSURE_LIST_SCHED(closure_list) \
346 grpc_closure_list_sched(__FILE__, __LINE__, closure_list)
347 #else
348 #define GRPC_CLOSURE_LIST_SCHED(closure_list) \
349 grpc_closure_list_sched(closure_list)
350 #endif
351
352 #endif /* GRPC_CORE_LIB_IOMGR_CLOSURE_H */
353