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 
54 
56 struct grpc_closure {
59  union {
65  } next_data;
66 
69 
71  void* cb_arg;
72 
74  union {
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,
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 = 0;
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 };
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 
176 inline bool grpc_closure_list_append(grpc_closure_list* closure_list,
178  if (closure == nullptr) {
179  return false;
180  }
181  closure->next_data.next = nullptr;
182  bool was_empty = (closure_list->head == nullptr);
183  if (was_empty) {
184  closure_list->head = closure;
185  } else {
186  closure_list->tail->next_data.next = closure;
187  }
188  closure_list->tail = closure;
189  return was_empty;
190 }
191 
195 inline bool grpc_closure_list_append(grpc_closure_list* closure_list,
198  if (closure == nullptr) {
200  return false;
201  }
202 #ifdef GRPC_ERROR_IS_ABSEIL_STATUS
204 #else
205  closure->error_data.error = reinterpret_cast<intptr_t>(error);
206 #endif
207  return grpc_closure_list_append(closure_list, closure);
208 }
209 
212  grpc_error_handle forced_failure) {
213  for (grpc_closure* c = list->head; c != nullptr; c = c->next_data.next) {
214  if (c->error_data.error == 0) {
215 #ifdef GRPC_ERROR_IS_ABSEIL_STATUS
216  c->error_data.error =
218 #else
219  c->error_data.error =
220  reinterpret_cast<intptr_t>(GRPC_ERROR_REF(forced_failure));
221 #endif
222  }
223  }
224  GRPC_ERROR_UNREF(forced_failure);
225 }
226 
230  if (src->head == nullptr) {
231  return;
232  }
233  if (dst->head == nullptr) {
234  *dst = *src;
235  } else {
236  dst->tail->next_data.next = src->head;
237  dst->tail = src->tail;
238  }
239  src->head = src->tail = nullptr;
240 }
241 
243 inline bool grpc_closure_list_empty(grpc_closure_list closure_list) {
244  return closure_list.head == nullptr;
245 }
246 
247 namespace grpc_core {
248 class Closure {
249  public:
250  static void Run(const DebugLocation& location, grpc_closure* closure,
252  (void)location;
253  if (closure == nullptr) {
255  return;
256  }
257 #ifndef NDEBUG
258  if (grpc_trace_closure.enabled()) {
259  gpr_log(GPR_DEBUG, "running closure %p: created [%s:%d]: run [%s:%d]",
260  closure, closure->file_created, closure->line_created,
261  location.file(), location.line());
262  }
263  GPR_ASSERT(closure->cb != nullptr);
264 #endif
265  closure->cb(closure->cb_arg, error);
266 #ifndef NDEBUG
267  if (grpc_trace_closure.enabled()) {
268  gpr_log(GPR_DEBUG, "closure %p finished", closure);
269  }
270 #endif
272  }
273 };
274 } // namespace grpc_core
275 
276 #endif /* GRPC_CORE_LIB_IOMGR_CLOSURE_H */
grpc_closure_list::tail
grpc_closure * tail
Definition: closure.h:43
dst
static const char dst[]
Definition: test-fs-copyfile.c:37
log.h
mpscq.h
grpc_trace_closure
grpc_core::DebugOnlyTraceFlag grpc_trace_closure
grpc_closure::run
bool run
Definition: closure.h:83
grpc_core::DebugLocation
Definition: debug_location.h:31
grpc_core::Closure
Definition: closure.h:248
grpc_closure::cb
grpc_iomgr_cb_func cb
Definition: closure.h:68
file
const grpc_generator::File * file
Definition: python_private_generator.h:38
timers.h
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::internal::StatusAllocHeapPtr
uintptr_t StatusAllocHeapPtr(absl::Status s)
Definition: status_helper.cc:421
closure_impl
Definition: closure.h:122
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
error
grpc_error_handle error
Definition: retry_filter.cc:499
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
grpc_closure_create
grpc_closure * grpc_closure_create(const char *file, int line, grpc_iomgr_cb_func cb, void *cb_arg)
Definition: closure.h:140
grpc_closure::mpscq_node
grpc_core::ManualConstructor< grpc_core::MultiProducerSingleConsumerQueue::Node > mpscq_node
Definition: closure.h:63
closure_impl::wrapped_closure::cb
grpc_iomgr_cb_func cb
Definition: closure.h:125
grpc_core::DebugLocation::file
const char * file() const
Definition: debug_location.h:34
grpc_closure_list
struct grpc_closure_list grpc_closure_list
grpc_closure_list_append
bool grpc_closure_list_append(grpc_closure_list *closure_list, grpc_closure *closure)
Definition: closure.h:176
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_closure::next_data
union grpc_closure::@14 next_data
closure_impl::wrapped_closure::cb_arg
void * cb_arg
Definition: closure.h:126
grpc_core::DebugLocation::line
int line() const
Definition: debug_location.h:35
grpc_closure_list::head
grpc_closure * head
Definition: closure.h:42
grpc_closure::line_initiated
int line_initiated
Definition: closure.h:87
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc_closure::error
uintptr_t error
Definition: closure.h:75
closure_impl::wrapped_closure
Definition: closure.h:124
closure
grpc_closure closure
Definition: src/core/lib/surface/server.cc:466
grpc_closure::cb_arg
void * cb_arg
Definition: closure.h:71
grpc_closure::scratch
uintptr_t scratch
Definition: closure.h:64
arg
Definition: cmdline.cc:40
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
error.h
uintptr_t
_W64 unsigned int uintptr_t
Definition: stdint-msvc2008.h:119
grpc_closure_list_init
void grpc_closure_list_init(grpc_closure_list *closure_list)
Definition: closure.h:170
manual_constructor.h
grpc_core::TraceFlag
Definition: debug/trace.h:63
grpc_closure_list
Definition: closure.h:41
grpc_core::TraceFlag::enabled
bool enabled()
Definition: debug/trace.h:82
grpc_closure_init
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_ERROR_REF
#define GRPC_ERROR_REF(err)
Definition: error.h:261
debug_location.h
grpc_closure::error_data
union grpc_closure::@15 error_data
alloc.h
grpc_core::MultiProducerSingleConsumerQueue::Node
Definition: mpscq.h:38
grpc_closure::next
grpc_closure * next
Definition: closure.h:60
grpc_iomgr_cb_func
void(* grpc_iomgr_cb_func)(void *arg, grpc_error_handle error)
Definition: closure.h:53
grpc_closure::file_initiated
const char * file_initiated
Definition: closure.h:86
regen-readme.line
line
Definition: regen-readme.py:30
closure_impl::closure_wrapper
void closure_wrapper(void *arg, grpc_error_handle error)
Definition: closure.h:129
arg
struct arg arg
grpc_closure::file_created
const char * file_created
Definition: closure.h:84
closure
Definition: proxy.cc:59
GRPC_ERROR_UNREF
#define GRPC_ERROR_UNREF(err)
Definition: error.h:262
grpc_closure_list_fail_all
void grpc_closure_list_fail_all(grpc_closure_list *list, grpc_error_handle forced_failure)
Definition: closure.h:211
grpc_closure::line_created
int line_created
Definition: closure.h:85
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
grpc_closure::scheduled
bool scheduled
Definition: closure.h:82
grpc_closure_list_move
void grpc_closure_list_move(grpc_closure_list *src, grpc_closure_list *dst)
Definition: closure.h:228
grpc_error
Definition: error_internal.h:42
grpc_closure_list_empty
bool grpc_closure_list_empty(grpc_closure_list closure_list)
Definition: closure.h:243
grpc_closure
Definition: closure.h:56
grpc_core::Closure::Run
static void Run(const DebugLocation &location, grpc_closure *closure, grpc_error_handle error)
Definition: closure.h:250
cb
OPENSSL_EXPORT pem_password_cb * cb
Definition: pem.h:351
closure_impl::wrapped_closure::wrapper
grpc_closure wrapper
Definition: closure.h:127
grpc_core::ManualConstructor
Definition: manual_constructor.h:103
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:47