blob: 6954ddbf76509e8706845960f778a9dee43bc0bf [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>
Masood Malekghassemi701af602015-06-03 15:01:17 -070043#include <grpc/support/string_util.h>
ctiller58393c22015-01-07 14:03:30 -080044#include <grpc/support/sync.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070045#include <grpc/support/thd.h>
ctiller58393c22015-01-07 14:03:30 -080046
ctiller58393c22015-01-07 14:03:30 -080047static gpr_mu g_mu;
Nicolas "Pixel" Nobleae7b45a2015-02-04 03:28:34 +010048static gpr_cv g_rcv;
David Garcia Quintas5f228f52015-05-26 19:58:50 -070049static grpc_iomgr_closure *g_cbs_head = NULL;
50static grpc_iomgr_closure *g_cbs_tail = NULL;
ctiller58393c22015-01-07 14:03:30 -080051static int g_shutdown;
ctiller58393c22015-01-07 14:03:30 -080052static gpr_event g_background_callback_executor_done;
Craig Tillerfa275a92015-06-01 13:55:54 -070053static grpc_iomgr_object g_root_object;
ctiller58393c22015-01-07 14:03:30 -080054
55/* Execute followup callbacks continuously.
56 Other threads may check in and help during pollset_work() */
57static void background_callback_executor(void *ignored) {
58 gpr_mu_lock(&g_mu);
59 while (!g_shutdown) {
60 gpr_timespec deadline = gpr_inf_future;
David Garcia Quintas00ff7df2015-05-12 00:19:47 -070061 gpr_timespec short_deadline =
Craig Tillerf3756c12015-07-01 17:21:01 -070062 gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(100));
ctiller58393c22015-01-07 14:03:30 -080063 if (g_cbs_head) {
David Garcia Quintasa30020f2015-05-27 19:21:01 -070064 grpc_iomgr_closure *closure = g_cbs_head;
65 g_cbs_head = closure->next;
ctiller58393c22015-01-07 14:03:30 -080066 if (!g_cbs_head) g_cbs_tail = NULL;
67 gpr_mu_unlock(&g_mu);
David Garcia Quintasa30020f2015-05-27 19:21:01 -070068 closure->cb(closure->cb_arg, closure->success);
ctiller58393c22015-01-07 14:03:30 -080069 gpr_mu_lock(&g_mu);
Craig Tillerf1bff012015-07-06 11:20:50 -070070 } else if (grpc_alarm_check(&g_mu, gpr_now(GPR_CLOCK_REALTIME),
71 &deadline)) {
ctiller58393c22015-01-07 14:03:30 -080072 } else {
David Garcia Quintas00ff7df2015-05-12 00:19:47 -070073 gpr_mu_unlock(&g_mu);
74 gpr_sleep_until(gpr_time_min(short_deadline, deadline));
75 gpr_mu_lock(&g_mu);
ctiller58393c22015-01-07 14:03:30 -080076 }
77 }
78 gpr_mu_unlock(&g_mu);
79 gpr_event_set(&g_background_callback_executor_done, (void *)1);
80}
81
David Garcia Quintas5b984ce2015-05-12 16:04:28 -070082void grpc_kick_poller(void) {
83 /* Empty. The background callback executor polls periodically. The activity
84 * the kicker is trying to draw the executor's attention to will be picked up
85 * either by one of the periodic wakeups or by one of the polling application
86 * threads. */
87}
ctiller58393c22015-01-07 14:03:30 -080088
Craig Tiller32946d32015-01-15 11:37:30 -080089void grpc_iomgr_init(void) {
ctiller58393c22015-01-07 14:03:30 -080090 gpr_thd_id id;
91 gpr_mu_init(&g_mu);
Nicolas "Pixel" Nobleae7b45a2015-02-04 03:28:34 +010092 gpr_cv_init(&g_rcv);
Craig Tillerf3756c12015-07-01 17:21:01 -070093 grpc_alarm_list_init(gpr_now(GPR_CLOCK_REALTIME));
Craig Tillerfa275a92015-06-01 13:55:54 -070094 g_root_object.next = g_root_object.prev = &g_root_object;
95 g_root_object.name = "root";
ctiller58393c22015-01-07 14:03:30 -080096 grpc_iomgr_platform_init();
97 gpr_event_init(&g_background_callback_executor_done);
98 gpr_thd_new(&id, background_callback_executor, NULL, NULL);
99}
100
Craig Tillerfa275a92015-06-01 13:55:54 -0700101static size_t count_objects(void) {
102 grpc_iomgr_object *obj;
103 size_t n = 0;
104 for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
105 n++;
106 }
107 return n;
108}
109
Craig Tiller32946d32015-01-15 11:37:30 -0800110void grpc_iomgr_shutdown(void) {
Craig Tillerfa275a92015-06-01 13:55:54 -0700111 grpc_iomgr_object *obj;
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700112 grpc_iomgr_closure *closure;
ctiller58393c22015-01-07 14:03:30 -0800113 gpr_timespec shutdown_deadline =
Craig Tillerf3756c12015-07-01 17:21:01 -0700114 gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(10));
ctiller58393c22015-01-07 14:03:30 -0800115
ctiller58393c22015-01-07 14:03:30 -0800116 gpr_mu_lock(&g_mu);
117 g_shutdown = 1;
Craig Tiller8e0b08a2015-06-01 17:04:17 -0700118 while (g_cbs_head != NULL || g_root_object.next != &g_root_object) {
119 if (g_cbs_head != NULL && g_root_object.next != &g_root_object) {
120 gpr_log(GPR_DEBUG,
121 "Waiting for %d iomgr objects to be destroyed and executing "
122 "final callbacks",
123 count_objects());
Craig Tiller29f741f2015-05-29 12:29:43 -0700124 } else if (g_cbs_head != NULL) {
125 gpr_log(GPR_DEBUG, "Executing final iomgr callbacks");
126 } else {
Craig Tiller8e0b08a2015-06-01 17:04:17 -0700127 gpr_log(GPR_DEBUG, "Waiting for %d iomgr objects to be destroyed",
128 count_objects());
Craig Tiller29f741f2015-05-29 12:29:43 -0700129 }
Craig Tillerfa275a92015-06-01 13:55:54 -0700130 if (g_cbs_head) {
131 do {
Craig Tiller3d67c7c2015-06-01 20:10:13 -0700132 closure = g_cbs_head;
133 g_cbs_head = closure->next;
Craig Tillerfa275a92015-06-01 13:55:54 -0700134 if (!g_cbs_head) g_cbs_tail = NULL;
135 gpr_mu_unlock(&g_mu);
ctiller58393c22015-01-07 14:03:30 -0800136
Craig Tiller3d67c7c2015-06-01 20:10:13 -0700137 closure->cb(closure->cb_arg, 0);
Craig Tillerfa275a92015-06-01 13:55:54 -0700138 gpr_mu_lock(&g_mu);
139 } while (g_cbs_head);
140 continue;
ctiller58393c22015-01-07 14:03:30 -0800141 }
Craig Tiller8e0b08a2015-06-01 17:04:17 -0700142 if (grpc_alarm_check(&g_mu, gpr_inf_future, NULL)) {
143 gpr_log(GPR_DEBUG, "got late alarm");
144 continue;
145 }
146 if (g_root_object.next != &g_root_object) {
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700147 int timeout = 0;
Craig Tiller8674cb12015-06-05 07:09:25 -0700148 gpr_timespec short_deadline =
Craig Tillerf3756c12015-07-01 17:21:01 -0700149 gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(100));
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700150 while (gpr_cv_wait(&g_rcv, &g_mu, short_deadline) && g_cbs_head == NULL) {
Craig Tillerf3756c12015-07-01 17:21:01 -0700151 if (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), shutdown_deadline) > 0) {
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700152 timeout = 1;
153 break;
154 }
155 }
156 if (timeout) {
ctiller58393c22015-01-07 14:03:30 -0800157 gpr_log(GPR_DEBUG,
158 "Failed to free %d iomgr objects before shutdown deadline: "
159 "memory leaks are likely",
Craig Tillerfa275a92015-06-01 13:55:54 -0700160 count_objects());
161 for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
162 gpr_log(GPR_DEBUG, "LEAKED OBJECT: %s", obj->name);
163 }
ctiller58393c22015-01-07 14:03:30 -0800164 break;
165 }
166 }
167 }
168 gpr_mu_unlock(&g_mu);
169
Nicolas "Pixel" Nobleae7b45a2015-02-04 03:28:34 +0100170 grpc_kick_poller();
ctiller58393c22015-01-07 14:03:30 -0800171 gpr_event_wait(&g_background_callback_executor_done, gpr_inf_future);
172
173 grpc_alarm_list_shutdown();
Craig Tiller8e0b08a2015-06-01 17:04:17 -0700174
ctiller58393c22015-01-07 14:03:30 -0800175 grpc_iomgr_platform_shutdown();
ctiller58393c22015-01-07 14:03:30 -0800176 gpr_mu_destroy(&g_mu);
Nicolas "Pixel" Nobleae7b45a2015-02-04 03:28:34 +0100177 gpr_cv_destroy(&g_rcv);
ctiller58393c22015-01-07 14:03:30 -0800178}
179
Craig Tillerfa275a92015-06-01 13:55:54 -0700180void grpc_iomgr_register_object(grpc_iomgr_object *obj, const char *name) {
ctiller58393c22015-01-07 14:03:30 -0800181 gpr_mu_lock(&g_mu);
Craig Tiller8e0b08a2015-06-01 17:04:17 -0700182 obj->name = gpr_strdup(name);
Craig Tillerfa275a92015-06-01 13:55:54 -0700183 obj->next = &g_root_object;
184 obj->prev = obj->next->prev;
185 obj->next->prev = obj->prev->next = obj;
ctiller58393c22015-01-07 14:03:30 -0800186 gpr_mu_unlock(&g_mu);
187}
188
Craig Tillerfa275a92015-06-01 13:55:54 -0700189void grpc_iomgr_unregister_object(grpc_iomgr_object *obj) {
ctiller58393c22015-01-07 14:03:30 -0800190 gpr_mu_lock(&g_mu);
Craig Tillerfa275a92015-06-01 13:55:54 -0700191 obj->next->prev = obj->prev;
192 obj->prev->next = obj->next;
Craig Tiller8e0b08a2015-06-01 17:04:17 -0700193 gpr_free(obj->name);
Craig Tillerfa275a92015-06-01 13:55:54 -0700194 gpr_cv_signal(&g_rcv);
ctiller58393c22015-01-07 14:03:30 -0800195 gpr_mu_unlock(&g_mu);
196}
197
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700198void grpc_iomgr_closure_init(grpc_iomgr_closure *closure, grpc_iomgr_cb_func cb,
199 void *cb_arg) {
200 closure->cb = cb;
201 closure->cb_arg = cb_arg;
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700202 closure->next = NULL;
David Garcia Quintas5f228f52015-05-26 19:58:50 -0700203}
204
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700205void grpc_iomgr_add_delayed_callback(grpc_iomgr_closure *closure, int success) {
206 closure->success = success;
ctiller58393c22015-01-07 14:03:30 -0800207 gpr_mu_lock(&g_mu);
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700208 closure->next = NULL;
ctiller58393c22015-01-07 14:03:30 -0800209 if (!g_cbs_tail) {
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700210 g_cbs_head = g_cbs_tail = closure;
ctiller58393c22015-01-07 14:03:30 -0800211 } else {
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700212 g_cbs_tail->next = closure;
213 g_cbs_tail = closure;
ctiller58393c22015-01-07 14:03:30 -0800214 }
Craig Tiller3c1331f2015-06-01 13:17:15 -0700215 if (g_shutdown) {
216 gpr_cv_signal(&g_rcv);
217 }
ctiller58393c22015-01-07 14:03:30 -0800218 gpr_mu_unlock(&g_mu);
219}
220
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700221void grpc_iomgr_add_callback(grpc_iomgr_closure *closure) {
David Garcia Quintas1c762bd2015-05-31 17:04:43 -0700222 grpc_iomgr_add_delayed_callback(closure, 1 /* GPR_TRUE */);
ctiller58393c22015-01-07 14:03:30 -0800223}
224
225int grpc_maybe_call_delayed_callbacks(gpr_mu *drop_mu, int success) {
226 int n = 0;
227 gpr_mu *retake_mu = NULL;
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700228 grpc_iomgr_closure *closure;
ctiller58393c22015-01-07 14:03:30 -0800229 for (;;) {
230 /* check for new work */
231 if (!gpr_mu_trylock(&g_mu)) {
232 break;
233 }
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700234 closure = g_cbs_head;
235 if (!closure) {
ctiller58393c22015-01-07 14:03:30 -0800236 gpr_mu_unlock(&g_mu);
237 break;
238 }
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700239 g_cbs_head = closure->next;
ctiller58393c22015-01-07 14:03:30 -0800240 if (!g_cbs_head) g_cbs_tail = NULL;
241 gpr_mu_unlock(&g_mu);
242 /* if we have a mutex to drop, do so before executing work */
243 if (drop_mu) {
244 gpr_mu_unlock(drop_mu);
245 retake_mu = drop_mu;
246 drop_mu = NULL;
247 }
David Garcia Quintasa30020f2015-05-27 19:21:01 -0700248 closure->cb(closure->cb_arg, success && closure->success);
ctiller58393c22015-01-07 14:03:30 -0800249 n++;
250 }
251 if (retake_mu) {
252 gpr_mu_lock(retake_mu);
253 }
254 return n;
Craig Tiller190d3602015-02-18 09:23:38 -0800255}