GRPC Core  18.0.0
closure.h
Go to the documentation of this file.
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 
23 
24 #include <assert.h>
25 #include <stdbool.h>
26 
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
29 
35 
36 struct grpc_closure;
37 typedef struct grpc_closure grpc_closure;
38 
40 
41 typedef struct grpc_closure_list {
45 
53 typedef void (*grpc_iomgr_cb_func)(void* arg, grpc_error_handle error);
54 
56 struct grpc_closure {
59  union {
64  uintptr_t scratch;
66 
69 
71  void* cb_arg;
72 
74  union {
76  uintptr_t scratch;
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;
86  const char* file_initiated;
88 #endif
89 };
90 
91 #ifndef NDEBUG
92 inline grpc_closure* grpc_closure_init(const char* file, int line,
94  grpc_iomgr_cb_func cb, void* cb_arg) {
95 #else
97  grpc_iomgr_cb_func cb, void* cb_arg) {
98 #endif
99  closure->cb = cb;
100  closure->cb_arg = cb_arg;
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 
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 
126  void* cb_arg;
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
146  static_cast<closure_impl::wrapped_closure*>(gpr_malloc(sizeof(*wc)));
147  wc->cb = cb;
148  wc->cb_arg = cb_arg;
149 #ifndef NDEBUG
151  wc);
152 #else
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 
177 inline bool grpc_closure_list_append(grpc_closure_list* closure_list,
180  if (closure == nullptr) {
182  return false;
183  }
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 
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 
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 
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,
232  (void)location;
233  if (closure == nullptr) {
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]",
241  location.file(), location.line());
242  }
243  GPR_ASSERT(closure->cb != nullptr);
244 #endif
246 #ifndef NDEBUG
247  if (grpc_trace_closure.enabled()) {
248  gpr_log(GPR_DEBUG, "closure %p finished", closure);
249  }
250 #endif
252  }
253 };
254 } // namespace grpc_core
255 
256 #endif /* GRPC_CORE_LIB_IOMGR_CLOSURE_H */
Definition: closure.h:228
static void Run(const DebugLocation &location, grpc_closure *closure, grpc_error_handle error)
Definition: closure.h:230
Definition: debug_location.h:31
int line() const
Definition: debug_location.h:35
const char * file() const
Definition: debug_location.h:34
Definition: manual_constructor.h:169
Definition: trace.h:61
bool enabled()
Definition: trace.h:80
bool grpc_closure_list_empty(grpc_closure_list closure_list)
return whether list is empty.
Definition: closure.h:223
struct grpc_closure_list grpc_closure_list
grpc_core::DebugOnlyTraceFlag grpc_trace_closure
void grpc_closure_list_move(grpc_closure_list *src, grpc_closure_list *dst)
append all closures from src to dst and empty src.
Definition: closure.h:208
grpc_closure * grpc_closure_init(const char *file, int line, grpc_closure *closure, grpc_iomgr_cb_func cb, void *cb_arg)
Definition: closure.h:92
grpc_closure * grpc_closure_create(const char *file, int line, grpc_iomgr_cb_func cb, void *cb_arg)
Definition: closure.h:140
void grpc_closure_list_fail_all(grpc_closure_list *list, grpc_error_handle forced_failure)
force all success bits in list to false
Definition: closure.h:197
bool grpc_closure_list_append(grpc_closure_list *closure_list, grpc_closure *closure, grpc_error_handle error)
add closure to the end of list and set closure's result to error Returns true if list becomes non-emp...
Definition: closure.h:177
void grpc_closure_list_init(grpc_closure_list *closure_list)
Definition: closure.h:170
void(* grpc_iomgr_cb_func)(void *arg, grpc_error_handle error)
gRPC Callback definition.
Definition: closure.h:53
#define GRPC_ERROR_NONE
The following "special" errors can be propagated without allocating memory.
Definition: error.h:228
#define GRPC_ERROR_UNREF(err)
Definition: error.h:254
#define GRPC_ERROR_REF(err)
Definition: error.h:253
#define GPR_DEBUG
Macros to build log contexts at various severity levels.
Definition: log.h:53
#define GPR_ASSERT(x)
abort() the process if x is zero, having written a line to the log.
Definition: log.h:92
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
Log a message.
GPRAPI void gpr_free(void *ptr)
free
Definition: alloc.cc:50
GPRAPI void * gpr_malloc(size_t size)
malloc.
Definition: alloc.cc:28
grpc_error_handle error
Definition: lame_client.cc:54
Definition: closure.h:122
void closure_wrapper(void *arg, grpc_error_handle error)
Definition: closure.h:129
Round Robin Policy.
Definition: backend_metric.cc:26
grpc_closure closure
Definition: server.cc:460
Definition: closure.h:124
grpc_iomgr_cb_func cb
Definition: closure.h:125
grpc_closure wrapper
Definition: closure.h:127
void * cb_arg
Definition: closure.h:126
Definition: closure.h:41
grpc_closure * tail
Definition: closure.h:43
grpc_closure * head
Definition: closure.h:42
A closure over a grpc_iomgr_cb_func.
Definition: closure.h:56
bool scheduled
Definition: closure.h:82
int line_initiated
Definition: closure.h:87
bool run
Definition: closure.h:83
void * cb_arg
Arguments to be passed to "cb".
Definition: closure.h:71
grpc_closure * next
Definition: closure.h:60
uintptr_t scratch
Definition: closure.h:64
const char * file_created
Definition: closure.h:84
int line_created
Definition: closure.h:85
union grpc_closure::@17 next_data
Once queued, next indicates the next queued closure; before then, scratch space.
grpc_error_handle error
Definition: closure.h:75
grpc_core::ManualConstructor< grpc_core::MultiProducerSingleConsumerQueue::Node > mpscq_node
Definition: closure.h:63
union grpc_closure::@18 error_data
Once queued, the result of the closure.
const char * file_initiated
Definition: closure.h:86
grpc_iomgr_cb_func cb
Bound callback.
Definition: closure.h:68
Definition: error_internal.h:41