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 <stdbool.h>
26
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
29
30 #include "src/core/lib/gprpp/debug_location.h"
31 #include "src/core/lib/gprpp/manual_constructor.h"
32 #include "src/core/lib/gprpp/mpscq.h"
33 #include "src/core/lib/iomgr/error.h"
34 #include "src/core/lib/profiling/timers.h"
35
36 struct grpc_closure;
37 typedef struct grpc_closure grpc_closure;
38
39 extern grpc_core::DebugOnlyTraceFlag grpc_trace_closure;
40
41 typedef struct grpc_closure_list {
42 grpc_closure* head;
43 grpc_closure* tail;
44 } grpc_closure_list;
45
46 /** gRPC Callback definition.
47 *
48 * \param arg Arbitrary input.
49 * \param error GRPC_ERROR_NONE if no error occurred, otherwise some grpc_error
50 * describing what went wrong.
51 * Error contract: it is not the cb's job to unref this error;
52 * the closure scheduler will do that after the cb returns */
53 typedef void (*grpc_iomgr_cb_func)(void* arg, grpc_error* error);
54
55 /** A closure over a grpc_iomgr_cb_func. */
56 struct grpc_closure {
57 /** Once queued, next indicates the next queued closure; before then, scratch
58 * space */
59 union {
60 grpc_closure* next;
61 grpc_core::ManualConstructor<
62 grpc_core::MultiProducerSingleConsumerQueue::Node>
63 mpscq_node;
64 uintptr_t scratch;
65 } next_data;
66
67 /** Bound callback. */
68 grpc_iomgr_cb_func cb;
69
70 /** Arguments to be passed to "cb". */
71 void* cb_arg;
72
73 /** Once queued, the result of the closure. Before then: scratch space */
74 union {
75 grpc_error* error;
76 uintptr_t scratch;
77 } error_data;
78
79 // extra tracing and debugging for grpc_closure. This incurs a decent amount of
80 // overhead per closure, so it must be enabled at compile time.
81 #ifndef NDEBUG
82 bool scheduled;
83 bool run; // true = run, false = scheduled
84 const char* file_created;
85 int line_created;
86 const char* file_initiated;
87 int line_initiated;
88 #endif
89 };
90
91 #ifndef NDEBUG
grpc_closure_init(const char * file,int line,grpc_closure * closure,grpc_iomgr_cb_func cb,void * cb_arg)92 inline grpc_closure* grpc_closure_init(const char* file, int line,
93 grpc_closure* closure,
94 grpc_iomgr_cb_func cb, void* cb_arg) {
95 #else
96 inline grpc_closure* grpc_closure_init(grpc_closure* closure,
97 grpc_iomgr_cb_func cb, void* cb_arg) {
98 #endif
99 closure->cb = cb;
100 closure->cb_arg = cb_arg;
101 closure->error_data.error = GRPC_ERROR_NONE;
102 #ifndef NDEBUG
103 closure->scheduled = false;
104 closure->file_initiated = nullptr;
105 closure->line_initiated = 0;
106 closure->run = false;
107 closure->file_created = file;
108 closure->line_created = line;
109 #endif
110 return closure;
111 }
112
113 /** Initializes \a closure with \a cb and \a cb_arg. Returns \a closure. */
114 #ifndef NDEBUG
115 #define GRPC_CLOSURE_INIT(closure, cb, cb_arg, scheduler) \
116 grpc_closure_init(__FILE__, __LINE__, closure, cb, cb_arg)
117 #else
118 #define GRPC_CLOSURE_INIT(closure, cb, cb_arg, scheduler) \
119 grpc_closure_init(closure, cb, cb_arg)
120 #endif
121
122 namespace closure_impl {
123
124 struct wrapped_closure {
125 grpc_iomgr_cb_func cb;
126 void* cb_arg;
127 grpc_closure wrapper;
128 };
129 inline void closure_wrapper(void* arg, grpc_error* error) {
130 wrapped_closure* wc = static_cast<wrapped_closure*>(arg);
131 grpc_iomgr_cb_func cb = wc->cb;
132 void* cb_arg = wc->cb_arg;
133 gpr_free(wc);
134 cb(cb_arg, error);
135 }
136
137 } // namespace closure_impl
138
139 #ifndef NDEBUG
140 inline grpc_closure* grpc_closure_create(const char* file, int line,
141 grpc_iomgr_cb_func cb, void* cb_arg) {
142 #else
143 inline grpc_closure* grpc_closure_create(grpc_iomgr_cb_func cb, void* cb_arg) {
144 #endif
145 closure_impl::wrapped_closure* wc =
146 static_cast<closure_impl::wrapped_closure*>(gpr_malloc(sizeof(*wc)));
147 wc->cb = cb;
148 wc->cb_arg = cb_arg;
149 #ifndef NDEBUG
150 grpc_closure_init(file, line, &wc->wrapper, closure_impl::closure_wrapper,
151 wc);
152 #else
153 grpc_closure_init(&wc->wrapper, closure_impl::closure_wrapper, wc);
154 #endif
155 return &wc->wrapper;
156 }
157
158 /* Create a heap allocated closure: try to avoid except for very rare events */
159 #ifndef NDEBUG
160 #define GRPC_CLOSURE_CREATE(cb, cb_arg, scheduler) \
161 grpc_closure_create(__FILE__, __LINE__, cb, cb_arg)
162 #else
163 #define GRPC_CLOSURE_CREATE(cb, cb_arg, scheduler) \
164 grpc_closure_create(cb, cb_arg)
165 #endif
166
167 #define GRPC_CLOSURE_LIST_INIT \
168 { nullptr, nullptr }
169
170 inline void grpc_closure_list_init(grpc_closure_list* closure_list) {
171 closure_list->head = closure_list->tail = nullptr;
172 }
173
174 /** add \a closure to the end of \a list
175 and set \a closure's result to \a error
176 Returns true if \a list becomes non-empty */
177 inline bool grpc_closure_list_append(grpc_closure_list* closure_list,
178 grpc_closure* closure, grpc_error* error) {
179 if (closure == nullptr) {
180 GRPC_ERROR_UNREF(error);
181 return false;
182 }
183 closure->error_data.error = error;
184 closure->next_data.next = nullptr;
185 bool was_empty = (closure_list->head == nullptr);
186 if (was_empty) {
187 closure_list->head = closure;
188 } else {
189 closure_list->tail->next_data.next = closure;
190 }
191 closure_list->tail = closure;
192 return was_empty;
193 }
194
195 /** force all success bits in \a list to false */
196 inline void grpc_closure_list_fail_all(grpc_closure_list* list,
197 grpc_error* forced_failure) {
198 for (grpc_closure* c = list->head; c != nullptr; c = c->next_data.next) {
199 if (c->error_data.error == GRPC_ERROR_NONE) {
200 c->error_data.error = GRPC_ERROR_REF(forced_failure);
201 }
202 }
203 GRPC_ERROR_UNREF(forced_failure);
204 }
205
206 /** append all closures from \a src to \a dst and empty \a src. */
207 inline void grpc_closure_list_move(grpc_closure_list* src,
208 grpc_closure_list* dst) {
209 if (src->head == nullptr) {
210 return;
211 }
212 if (dst->head == nullptr) {
213 *dst = *src;
214 } else {
215 dst->tail->next_data.next = src->head;
216 dst->tail = src->tail;
217 }
218 src->head = src->tail = nullptr;
219 }
220
221 /** return whether \a list is empty. */
222 inline bool grpc_closure_list_empty(grpc_closure_list closure_list) {
223 return closure_list.head == nullptr;
224 }
225
226 namespace grpc_core {
227 class Closure {
228 public:
229 static void Run(const DebugLocation& location, grpc_closure* closure,
230 grpc_error* error) {
231 (void)location;
232 if (closure == nullptr) {
233 GRPC_ERROR_UNREF(error);
234 return;
235 }
236 #ifndef NDEBUG
237 if (grpc_trace_closure.enabled()) {
238 gpr_log(GPR_DEBUG, "running closure %p: created [%s:%d]: run [%s:%d]",
239 closure, closure->file_created, closure->line_created,
240 location.file(), location.line());
241 }
242 GPR_ASSERT(closure->cb != nullptr);
243 #endif
244 closure->cb(closure->cb_arg, error);
245 #ifndef NDEBUG
246 if (grpc_trace_closure.enabled()) {
247 gpr_log(GPR_DEBUG, "closure %p finished", closure);
248 }
249 #endif
250 GRPC_ERROR_UNREF(error);
251 }
252 };
253 } // namespace grpc_core
254
255 #endif /* GRPC_CORE_LIB_IOMGR_CLOSURE_H */
256