blob: 249228a214013d6c6be05cf9da8a077ff5ea383f [file] [log] [blame]
ctiller58393c22015-01-07 14:03:30 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
ctiller58393c22015-01-07 14:03:30 -08004 * 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
34#include "src/core/iomgr/iomgr.h"
35
36#include <stdlib.h>
37
38#include "src/core/iomgr/iomgr_internal.h"
39#include "src/core/iomgr/alarm_internal.h"
Craig Tillerfa275a92015-06-01 13:55:54 -070040#include "src/core/support/string.h"
ctiller58393c22015-01-07 14:03:30 -080041#include <grpc/support/alloc.h>
42#include <grpc/support/log.h>
43#include <grpc/support/thd.h>
44#include <grpc/support/sync.h>
45
ctiller58393c22015-01-07 14:03:30 -080046static gpr_mu g_mu;
Nicolas "Pixel" Nobleae7b45a2015-02-04 03:28:34 +010047static gpr_cv g_rcv;
David Garcia Quintas5f228f52015-05-26 19:58:50 -070048static grpc_iomgr_closure *g_cbs_head = NULL;
49static grpc_iomgr_closure *g_cbs_tail = NULL;
ctiller58393c22015-01-07 14:03:30 -080050static int g_shutdown;
ctiller58393c22015-01-07 14:03:30 -080051static gpr_event g_background_callback_executor_done;
Craig Tillerfa275a92015-06-01 13:55:54 -070052static grpc_iomgr_object g_root_object;
ctiller58393c22015-01-07 14:03:30 -080053
54/* Execute followup callbacks continuously.
55 Other threads may check in and help during pollset_work() */
56static void background_callback_executor(void *ignored) {
57 gpr_mu_lock(&g_mu);
58 while (!g_shutdown) {
59 gpr_timespec deadline = gpr_inf_future;
David Garcia Quintas00ff7df2015-05-12 00:19:47 -070060 gpr_timespec short_deadline =
61 gpr_time_add(gpr_now(), gpr_time_from_millis(100));
ctiller58393c22015-01-07 14:03:30 -080062 if (g_cbs_head) {
David Garcia Quintasa30020f2015-05-27 19:21:01 -070063 grpc_iomgr_closure *closure = g_cbs_head;
64 g_cbs_head = closure->next;
ctiller58393c22015-01-07 14:03:30 -080065 if (!g_cbs_head) g_cbs_tail = NULL;
66 gpr_mu_unlock(&g_mu);
David Garcia Quintasa30020f2015-05-27 19:21:01 -070067 closure->cb(closure->cb_arg, closure->success);
ctiller58393c22015-01-07 14:03:30 -080068 gpr_mu_lock(&g_mu);
69 } else if (grpc_alarm_check(&g_mu, gpr_now(), &deadline)) {
70 } else {
David Garcia Quintas00ff7df2015-05-12 00:19:47 -070071 gpr_mu_unlock(&g_mu);
72 gpr_sleep_until(gpr_time_min(short_deadline, deadline));
73 gpr_mu_lock(&g_mu);
ctiller58393c22015-01-07 14:03:30 -080074 }
75 }
76 gpr_mu_unlock(&g_mu);
77 gpr_event_set(&g_background_callback_executor_done, (void *)1);
78}
79
David Garcia Quintas5b984ce2015-05-12 16:04:28 -070080void grpc_kick_poller(void) {
81 /* Empty. The background callback executor polls periodically. The activity
82 * the kicker is trying to draw the executor's attention to will be picked up
83 * either by one of the periodic wakeups or by one of the polling application
84 * threads. */
85}
ctiller58393c22015-01-07 14:03:30 -080086
Craig Tiller32946d32015-01-15 11:37:30 -080087void grpc_iomgr_init(void) {
ctiller58393c22015-01-07 14:03:30 -080088 gpr_thd_id id;
89 gpr_mu_init(&g_mu);
Nicolas "Pixel" Nobleae7b45a2015-02-04 03:28:34 +010090 gpr_cv_init(&g_rcv);
ctiller58393c22015-01-07 14:03:30 -080091 grpc_alarm_list_init(gpr_now());
Craig Tillerfa275a92015-06-01 13:55:54 -070092 g_root_object.next = g_root_object.prev = &g_root_object;
93 g_root_object.name = "root";
ctiller58393c22015-01-07 14:03:30 -080094 grpc_iomgr_platform_init();
95 gpr_event_init(&g_background_callback_executor_done);
96 gpr_thd_new(&id, background_callback_executor, NULL, NULL);
97}
98
Craig Tillerfa275a92015-06-01 13:55:54 -070099static size_t count_objects(void) {
100 grpc_iomgr_object *obj;
101 size_t n = 0;
102 for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
103 n++;
104 }
105 return n;
106}
107
Craig Tiller32946d32015-01-15 11:37:30 -0800108void grpc_iomgr_shutdown(void) {
Craig Tillerfa275a92015-06-01 13:55:54 -0700109 grpc_iomgr_object *obj;
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700110 grpc_iomgr_closure *closure;
ctiller58393c22015-01-07 14:03:30 -0800111 gpr_timespec shutdown_deadline =
112 gpr_time_add(gpr_now(), gpr_time_from_seconds(10));
113
ctiller58393c22015-01-07 14:03:30 -0800114
115 gpr_mu_lock(&g_mu);
116 g_shutdown = 1;
Craig Tillerfa275a92015-06-01 13:55:54 -0700117 while (g_cbs_head || g_root_object.next != &g_root_object) {
118 size_t nobjs = count_objects();
119 gpr_log(GPR_DEBUG, "Waiting for %d iomgr objects to be destroyed%s", nobjs,
ctiller58393c22015-01-07 14:03:30 -0800120 g_cbs_head ? " and executing final callbacks" : "");
Craig Tillerfa275a92015-06-01 13:55:54 -0700121 if (g_cbs_head) {
122 do {
Craig Tiller3d67c7c2015-06-01 20:10:13 -0700123 closure = g_cbs_head;
124 g_cbs_head = closure->next;
Craig Tillerfa275a92015-06-01 13:55:54 -0700125 if (!g_cbs_head) g_cbs_tail = NULL;
126 gpr_mu_unlock(&g_mu);
ctiller58393c22015-01-07 14:03:30 -0800127
Craig Tiller3d67c7c2015-06-01 20:10:13 -0700128 closure->cb(closure->cb_arg, 0);
Craig Tillerfa275a92015-06-01 13:55:54 -0700129 gpr_mu_lock(&g_mu);
130 } while (g_cbs_head);
131 continue;
ctiller58393c22015-01-07 14:03:30 -0800132 }
Craig Tillerfa275a92015-06-01 13:55:54 -0700133 if (nobjs > 0) {
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700134 int timeout = 0;
135 gpr_timespec short_deadline = gpr_time_add(gpr_now(),
136 gpr_time_from_millis(100));
137 while (gpr_cv_wait(&g_rcv, &g_mu, short_deadline) && g_cbs_head == NULL) {
138 if (gpr_time_cmp(gpr_now(), shutdown_deadline) > 0) {
139 timeout = 1;
140 break;
141 }
142 }
143 if (timeout) {
ctiller58393c22015-01-07 14:03:30 -0800144 gpr_log(GPR_DEBUG,
145 "Failed to free %d iomgr objects before shutdown deadline: "
146 "memory leaks are likely",
Craig Tillerfa275a92015-06-01 13:55:54 -0700147 count_objects());
148 for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
149 gpr_log(GPR_DEBUG, "LEAKED OBJECT: %s", obj->name);
150 }
ctiller58393c22015-01-07 14:03:30 -0800151 break;
152 }
153 }
154 }
155 gpr_mu_unlock(&g_mu);
156
Nicolas "Pixel" Nobleae7b45a2015-02-04 03:28:34 +0100157 grpc_kick_poller();
ctiller58393c22015-01-07 14:03:30 -0800158 gpr_event_wait(&g_background_callback_executor_done, gpr_inf_future);
159
David Klempnerd1785242015-01-28 17:00:21 -0800160 grpc_iomgr_platform_shutdown();
ctiller58393c22015-01-07 14:03:30 -0800161 grpc_alarm_list_shutdown();
162 gpr_mu_destroy(&g_mu);
Nicolas "Pixel" Nobleae7b45a2015-02-04 03:28:34 +0100163 gpr_cv_destroy(&g_rcv);
ctiller58393c22015-01-07 14:03:30 -0800164}
165
Craig Tillerfa275a92015-06-01 13:55:54 -0700166void grpc_iomgr_register_object(grpc_iomgr_object *obj, const char *name) {
167 obj->name = gpr_strdup(name);
ctiller58393c22015-01-07 14:03:30 -0800168 gpr_mu_lock(&g_mu);
Craig Tillerfa275a92015-06-01 13:55:54 -0700169 obj->next = &g_root_object;
170 obj->prev = obj->next->prev;
171 obj->next->prev = obj->prev->next = obj;
ctiller58393c22015-01-07 14:03:30 -0800172 gpr_mu_unlock(&g_mu);
173}
174
Craig Tillerfa275a92015-06-01 13:55:54 -0700175void grpc_iomgr_unregister_object(grpc_iomgr_object *obj) {
176 gpr_free(obj->name);
ctiller58393c22015-01-07 14:03:30 -0800177 gpr_mu_lock(&g_mu);
Craig Tillerfa275a92015-06-01 13:55:54 -0700178 obj->next->prev = obj->prev;
179 obj->prev->next = obj->next;
180 gpr_cv_signal(&g_rcv);
ctiller58393c22015-01-07 14:03:30 -0800181 gpr_mu_unlock(&g_mu);
182}
183
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700184
185void grpc_iomgr_closure_init(grpc_iomgr_closure *closure, grpc_iomgr_cb_func cb,
186 void *cb_arg) {
187 closure->cb = cb;
188 closure->cb_arg = cb_arg;
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700189 closure->next = NULL;
David Garcia Quintas5f228f52015-05-26 19:58:50 -0700190}
191
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700192void grpc_iomgr_add_delayed_callback(grpc_iomgr_closure *closure, int success) {
193 closure->success = success;
ctiller58393c22015-01-07 14:03:30 -0800194 gpr_mu_lock(&g_mu);
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700195 closure->next = NULL;
ctiller58393c22015-01-07 14:03:30 -0800196 if (!g_cbs_tail) {
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700197 g_cbs_head = g_cbs_tail = closure;
ctiller58393c22015-01-07 14:03:30 -0800198 } else {
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700199 g_cbs_tail->next = closure;
200 g_cbs_tail = closure;
ctiller58393c22015-01-07 14:03:30 -0800201 }
ctiller58393c22015-01-07 14:03:30 -0800202 gpr_mu_unlock(&g_mu);
203}
204
David Garcia Quintas5f228f52015-05-26 19:58:50 -0700205
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700206void grpc_iomgr_add_callback(grpc_iomgr_closure *closure) {
David Garcia Quintas1c762bd2015-05-31 17:04:43 -0700207 grpc_iomgr_add_delayed_callback(closure, 1 /* GPR_TRUE */);
ctiller58393c22015-01-07 14:03:30 -0800208}
209
David Garcia Quintas5f228f52015-05-26 19:58:50 -0700210
ctiller58393c22015-01-07 14:03:30 -0800211int grpc_maybe_call_delayed_callbacks(gpr_mu *drop_mu, int success) {
212 int n = 0;
213 gpr_mu *retake_mu = NULL;
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700214 grpc_iomgr_closure *closure;
ctiller58393c22015-01-07 14:03:30 -0800215 for (;;) {
216 /* check for new work */
217 if (!gpr_mu_trylock(&g_mu)) {
218 break;
219 }
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700220 closure = g_cbs_head;
221 if (!closure) {
ctiller58393c22015-01-07 14:03:30 -0800222 gpr_mu_unlock(&g_mu);
223 break;
224 }
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700225 g_cbs_head = closure->next;
ctiller58393c22015-01-07 14:03:30 -0800226 if (!g_cbs_head) g_cbs_tail = NULL;
227 gpr_mu_unlock(&g_mu);
228 /* if we have a mutex to drop, do so before executing work */
229 if (drop_mu) {
230 gpr_mu_unlock(drop_mu);
231 retake_mu = drop_mu;
232 drop_mu = NULL;
233 }
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700234 closure->cb(closure->cb_arg, success && closure->success);
ctiller58393c22015-01-07 14:03:30 -0800235 n++;
236 }
237 if (retake_mu) {
238 gpr_mu_lock(retake_mu);
239 }
240 return n;
Craig Tiller190d3602015-02-18 09:23:38 -0800241}