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_handle 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_handle 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_handle 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,
179                                      grpc_error_handle error) {
180   if (closure == nullptr) {
181     GRPC_ERROR_UNREF(error);
182     return false;
183   }
184   closure->error_data.error = error;
185   closure->next_data.next = nullptr;
186   bool was_empty = (closure_list->head == nullptr);
187   if (was_empty) {
188     closure_list->head = closure;
189   } else {
190     closure_list->tail->next_data.next = closure;
191   }
192   closure_list->tail = closure;
193   return was_empty;
194 }
195 
196 /** force all success bits in \a list to false */
197 inline void grpc_closure_list_fail_all(grpc_closure_list* list,
198                                        grpc_error_handle forced_failure) {
199   for (grpc_closure* c = list->head; c != nullptr; c = c->next_data.next) {
200     if (c->error_data.error == GRPC_ERROR_NONE) {
201       c->error_data.error = GRPC_ERROR_REF(forced_failure);
202     }
203   }
204   GRPC_ERROR_UNREF(forced_failure);
205 }
206 
207 /** append all closures from \a src to \a dst and empty \a src. */
208 inline void grpc_closure_list_move(grpc_closure_list* src,
209                                    grpc_closure_list* dst) {
210   if (src->head == nullptr) {
211     return;
212   }
213   if (dst->head == nullptr) {
214     *dst = *src;
215   } else {
216     dst->tail->next_data.next = src->head;
217     dst->tail = src->tail;
218   }
219   src->head = src->tail = nullptr;
220 }
221 
222 /** return whether \a list is empty. */
223 inline bool grpc_closure_list_empty(grpc_closure_list closure_list) {
224   return closure_list.head == nullptr;
225 }
226 
227 namespace grpc_core {
228 class Closure {
229  public:
230   static void Run(const DebugLocation& location, grpc_closure* closure,
231                   grpc_error_handle error) {
232     (void)location;
233     if (closure == nullptr) {
234       GRPC_ERROR_UNREF(error);
235       return;
236     }
237 #ifndef NDEBUG
238     if (grpc_trace_closure.enabled()) {
239       gpr_log(GPR_DEBUG, "running closure %p: created [%s:%d]: run [%s:%d]",
240               closure, closure->file_created, closure->line_created,
241               location.file(), location.line());
242     }
243     GPR_ASSERT(closure->cb != nullptr);
244 #endif
245     closure->cb(closure->cb_arg, error);
246 #ifndef NDEBUG
247     if (grpc_trace_closure.enabled()) {
248       gpr_log(GPR_DEBUG, "closure %p finished", closure);
249     }
250 #endif
251     GRPC_ERROR_UNREF(error);
252   }
253 };
254 }  // namespace grpc_core
255 
256 #endif /* GRPC_CORE_LIB_IOMGR_CLOSURE_H */
257