GRPC C++  1.26.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* error);
54 
56 struct grpc_closure {
59  union {
64  uintptr_t scratch;
65  } next_data;
66 
69 
71  void* cb_arg;
72 
74  union {
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;
86  const char* file_initiated;
88 #endif
89 };
90 
91 #ifndef NDEBUG
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
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 
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 typedef struct {
126  void* cb_arg;
129 
130 inline void closure_wrapper(void* arg, grpc_error* error) {
131  wrapped_closure* wc = static_cast<wrapped_closure*>(arg);
132  grpc_iomgr_cb_func cb = wc->cb;
133  void* cb_arg = wc->cb_arg;
134  gpr_free(wc);
135  cb(cb_arg, error);
136 }
137 
138 } // namespace closure_impl
139 
140 #ifndef NDEBUG
141 inline grpc_closure* grpc_closure_create(const char* file, int line,
142  grpc_iomgr_cb_func cb, void* cb_arg) {
143 #else
144 inline grpc_closure* grpc_closure_create(grpc_iomgr_cb_func cb, void* cb_arg) {
145 #endif
147  static_cast<closure_impl::wrapped_closure*>(gpr_malloc(sizeof(*wc)));
148  wc->cb = cb;
149  wc->cb_arg = cb_arg;
150 #ifndef NDEBUG
152  wc);
153 #else
155 #endif
156  return &wc->wrapper;
157 }
158 
159 /* Create a heap allocated closure: try to avoid except for very rare events */
160 #ifndef NDEBUG
161 #define GRPC_CLOSURE_CREATE(cb, cb_arg, scheduler) \
162  grpc_closure_create(__FILE__, __LINE__, cb, cb_arg)
163 #else
164 #define GRPC_CLOSURE_CREATE(cb, cb_arg, scheduler) \
165  grpc_closure_create(cb, cb_arg)
166 #endif
167 
168 #define GRPC_CLOSURE_LIST_INIT \
169  { nullptr, nullptr }
170 
171 inline void grpc_closure_list_init(grpc_closure_list* closure_list) {
172  closure_list->head = closure_list->tail = nullptr;
173 }
174 
178 inline bool grpc_closure_list_append(grpc_closure_list* closure_list,
179  grpc_closure* closure, grpc_error* 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 
198  grpc_error* 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,
231  grpc_error* 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 */
void * cb_arg
Arguments to be passed to "cb".
Definition: closure.h:71
grpc_closure wrapper
Definition: closure.h:127
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_iomgr_cb_func cb
Definition: closure.h:125
const char * file_created
Definition: closure.h:84
bool enabled()
Definition: trace.h:80
GPRAPI void gpr_free(void *ptr)
free
bool grpc_closure_list_append(grpc_closure_list *closure_list, grpc_closure *closure, grpc_error *error)
add closure to the end of list and set closure&#39;s result to error Returns true if list becomes non-emp...
Definition: closure.h:178
bool run
Definition: closure.h:83
union grpc_closure::@11 next_data
Once queued, next indicates the next queued closure; before then, scratch space.
const char * file() const
Definition: debug_location.h:34
Definition: closure.h:228
bool scheduled
Definition: closure.h:82
grpc_closure * head
Definition: closure.h:42
Definition: debug_location.h:31
grpc_core::DebugOnlyTraceFlag grpc_trace_closure
uintptr_t scratch
Definition: closure.h:64
Definition: error_internal.h:39
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
Log a message.
grpc_error * error
Definition: closure.h:75
Definition: closure.h:41
Internal thread interface.
Definition: backoff.h:26
grpc_closure * grpc_closure_create(const char *file, int line, grpc_iomgr_cb_func cb, void *cb_arg)
Definition: closure.h:141
int line_created
Definition: closure.h:85
Definition: trace.h:61
void * cb_arg
Definition: closure.h:126
#define GRPC_ERROR_REF(err)
Definition: error.h:185
grpc_core::ManualConstructor< grpc_core::MultiProducerSingleConsumerQueue::Node > mpscq_node
Definition: closure.h:63
int line() const
Definition: debug_location.h:35
#define GPR_ASSERT(x)
abort() the process if x is zero, having written a line to the log.
Definition: log.h:94
#define GPR_DEBUG
Macros to build log contexts at various severity levels.
Definition: log.h:55
grpc_closure * next
Definition: closure.h:60
grpc_closure * tail
Definition: closure.h:43
void closure_wrapper(void *arg, grpc_error *error)
Definition: closure.h:130
grpc_iomgr_cb_func cb
Bound callback.
Definition: closure.h:68
Definition: manual_constructor.h:169
void grpc_closure_list_fail_all(grpc_closure_list *list, grpc_error *forced_failure)
force all success bits in list to false
Definition: closure.h:197
A closure over a grpc_iomgr_cb_func.
Definition: closure.h:56
void grpc_closure_list_init(grpc_closure_list *closure_list)
Definition: closure.h:171
#define GRPC_ERROR_NONE
The following "special" errors can be propagated without allocating memory.
Definition: error.h:125
struct grpc_closure_list grpc_closure_list
GPRAPI void * gpr_malloc(size_t size)
malloc.
Definition: closure.h:122
union grpc_closure::@12 error_data
Once queued, the result of the closure.
Definition: closure.h:124
void(* grpc_iomgr_cb_func)(void *arg, grpc_error *error)
gRPC Callback definition.
Definition: closure.h:53
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
#define GRPC_ERROR_UNREF(err)
Definition: error.h:186
const char * file_initiated
Definition: closure.h:86
int line_initiated
Definition: closure.h:87
bool grpc_closure_list_empty(grpc_closure_list closure_list)
return whether list is empty.
Definition: closure.h:223
static void Run(const DebugLocation &location, grpc_closure *closure, grpc_error *error)
Definition: closure.h:230