blob: 8e4efd3370d977819fb82359bf69ea2f5c6f3531 [file] [log] [blame]
Craig Tiller298751c2015-09-22 09:41:05 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tiller298751c2015-09-22 09:41:05 -07004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Craig Tiller9533d042016-03-25 17:11:06 -070034#include "src/core/lib/iomgr/closure.h"
Craig Tiller298751c2015-09-22 09:41:05 -070035
Craig Tiller3ff551b2015-10-06 09:11:37 -070036#include <grpc/support/alloc.h>
Craig Tiller7c70b6c2017-01-23 07:48:42 -080037#include <grpc/support/log.h>
Craig Tiller3ff551b2015-10-06 09:11:37 -070038
Craig Tillerde2c41c2016-09-01 15:08:08 -070039#include "src/core/lib/profiling/timers.h"
40
Craig Tiller91031da2016-12-28 15:44:25 -080041grpc_closure *grpc_closure_init(grpc_closure *closure, grpc_iomgr_cb_func cb,
42 void *cb_arg,
43 grpc_closure_scheduler *scheduler) {
Craig Tiller298751c2015-09-22 09:41:05 -070044 closure->cb = cb;
45 closure->cb_arg = cb_arg;
Craig Tiller91031da2016-12-28 15:44:25 -080046 closure->scheduler = scheduler;
47 return closure;
Craig Tiller298751c2015-09-22 09:41:05 -070048}
49
Craig Tiller8d8d0d32016-07-08 17:08:57 -070050void grpc_closure_list_init(grpc_closure_list *closure_list) {
51 closure_list->head = closure_list->tail = NULL;
52}
53
Craig Tiller27f59af2016-04-28 14:19:48 -070054void grpc_closure_list_append(grpc_closure_list *closure_list,
55 grpc_closure *closure, grpc_error *error) {
Craig Tiller9ccf5f12016-05-07 21:41:01 -070056 if (closure == NULL) {
57 GRPC_ERROR_UNREF(error);
58 return;
59 }
Craig Tillera7cd41c2016-08-31 12:59:24 -070060 closure->error_data.error = error;
Craig Tiller781bab532016-05-05 08:15:28 -070061 closure->next_data.next = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -070062 if (closure_list->head == NULL) {
63 closure_list->head = closure;
64 } else {
Craig Tiller781bab532016-05-05 08:15:28 -070065 closure_list->tail->next_data.next = closure;
Craig Tillera82950e2015-09-22 12:33:20 -070066 }
Craig Tiller298751c2015-09-22 09:41:05 -070067 closure_list->tail = closure;
68}
69
Craig Tiller27f59af2016-04-28 14:19:48 -070070void grpc_closure_list_fail_all(grpc_closure_list *list,
71 grpc_error *forced_failure) {
Craig Tiller781bab532016-05-05 08:15:28 -070072 for (grpc_closure *c = list->head; c != NULL; c = c->next_data.next) {
Craig Tillera7cd41c2016-08-31 12:59:24 -070073 if (c->error_data.error == GRPC_ERROR_NONE) {
74 c->error_data.error = GRPC_ERROR_REF(forced_failure);
Craig Tiller27f59af2016-04-28 14:19:48 -070075 }
Craig Tiller0ede5452016-04-23 12:21:45 -070076 }
Craig Tillerf707d622016-05-06 14:26:12 -070077 GRPC_ERROR_UNREF(forced_failure);
Craig Tiller0ede5452016-04-23 12:21:45 -070078}
79
Craig Tiller6c396862016-01-28 13:53:40 -080080bool grpc_closure_list_empty(grpc_closure_list closure_list) {
Craig Tiller298751c2015-09-22 09:41:05 -070081 return closure_list.head == NULL;
82}
83
Craig Tillera82950e2015-09-22 12:33:20 -070084void grpc_closure_list_move(grpc_closure_list *src, grpc_closure_list *dst) {
85 if (src->head == NULL) {
86 return;
87 }
88 if (dst->head == NULL) {
89 *dst = *src;
90 } else {
Craig Tiller781bab532016-05-05 08:15:28 -070091 dst->tail->next_data.next = src->head;
Craig Tillera82950e2015-09-22 12:33:20 -070092 dst->tail = src->tail;
93 }
Craig Tiller298751c2015-09-22 09:41:05 -070094 src->head = src->tail = NULL;
95}
David Garcia Quintas4bc34632015-10-07 16:12:35 -070096
Craig Tiller3ff551b2015-10-06 09:11:37 -070097typedef struct {
98 grpc_iomgr_cb_func cb;
99 void *cb_arg;
100 grpc_closure wrapper;
101} wrapped_closure;
102
Craig Tillerc027e772016-05-03 16:27:00 -0700103static void closure_wrapper(grpc_exec_ctx *exec_ctx, void *arg,
104 grpc_error *error) {
Craig Tiller3ff551b2015-10-06 09:11:37 -0700105 wrapped_closure *wc = arg;
106 grpc_iomgr_cb_func cb = wc->cb;
107 void *cb_arg = wc->cb_arg;
108 gpr_free(wc);
Craig Tillerc027e772016-05-03 16:27:00 -0700109 cb(exec_ctx, cb_arg, error);
Craig Tiller3ff551b2015-10-06 09:11:37 -0700110}
111
Craig Tiller91031da2016-12-28 15:44:25 -0800112grpc_closure *grpc_closure_create(grpc_iomgr_cb_func cb, void *cb_arg,
113 grpc_closure_scheduler *scheduler) {
Craig Tiller3ff551b2015-10-06 09:11:37 -0700114 wrapped_closure *wc = gpr_malloc(sizeof(*wc));
115 wc->cb = cb;
116 wc->cb_arg = cb_arg;
Craig Tiller91031da2016-12-28 15:44:25 -0800117 grpc_closure_init(&wc->wrapper, closure_wrapper, wc, scheduler);
Craig Tiller3ff551b2015-10-06 09:11:37 -0700118 return &wc->wrapper;
119}
Craig Tillerde2c41c2016-09-01 15:08:08 -0700120
Craig Tiller44b12f92016-09-08 10:06:14 -0700121void grpc_closure_run(grpc_exec_ctx *exec_ctx, grpc_closure *c,
122 grpc_error *error) {
Craig Tillerde2c41c2016-09-01 15:08:08 -0700123 GPR_TIMER_BEGIN("grpc_closure_run", 0);
Craig Tillerc2dd2a22016-10-14 07:54:23 -0700124 if (c != NULL) {
Craig Tiller91031da2016-12-28 15:44:25 -0800125 c->scheduler->vtable->run(exec_ctx, c, error);
126 } else {
127 GRPC_ERROR_UNREF(error);
Craig Tillerc2dd2a22016-10-14 07:54:23 -0700128 }
Craig Tillerde2c41c2016-09-01 15:08:08 -0700129 GPR_TIMER_END("grpc_closure_run", 0);
130}
Craig Tiller91031da2016-12-28 15:44:25 -0800131
132void grpc_closure_sched(grpc_exec_ctx *exec_ctx, grpc_closure *c,
133 grpc_error *error) {
134 GPR_TIMER_BEGIN("grpc_closure_sched", 0);
135 if (c != NULL) {
136 c->scheduler->vtable->sched(exec_ctx, c, error);
137 } else {
138 GRPC_ERROR_UNREF(error);
139 }
140 GPR_TIMER_END("grpc_closure_sched", 0);
141}
142
143void grpc_closure_list_sched(grpc_exec_ctx *exec_ctx, grpc_closure_list *list) {
144 grpc_closure *c = list->head;
145 while (c != NULL) {
146 grpc_closure *next = c->next_data.next;
147 c->scheduler->vtable->sched(exec_ctx, c, c->error_data.error);
148 c = next;
149 }
150 list->head = list->tail = NULL;
151}