blob: c845caea0a95f97e4d1f79a38ecaac9e9fac7d62 [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"
40#include <grpc/support/alloc.h>
41#include <grpc/support/log.h>
42#include <grpc/support/thd.h>
43#include <grpc/support/sync.h>
44
45typedef struct delayed_callback {
46 grpc_iomgr_cb_func cb;
47 void *cb_arg;
48 int success;
49 struct delayed_callback *next;
50} delayed_callback;
51
52static gpr_mu g_mu;
Nicolas "Pixel" Nobleae7b45a2015-02-04 03:28:34 +010053static gpr_cv g_rcv;
ctiller58393c22015-01-07 14:03:30 -080054static delayed_callback *g_cbs_head = NULL;
55static delayed_callback *g_cbs_tail = NULL;
56static int g_shutdown;
57static int g_refs;
58static gpr_event g_background_callback_executor_done;
59
60/* Execute followup callbacks continuously.
61 Other threads may check in and help during pollset_work() */
62static void background_callback_executor(void *ignored) {
63 gpr_mu_lock(&g_mu);
64 while (!g_shutdown) {
65 gpr_timespec deadline = gpr_inf_future;
David Garcia Quintas00ff7df2015-05-12 00:19:47 -070066 gpr_timespec short_deadline =
67 gpr_time_add(gpr_now(), gpr_time_from_millis(100));
ctiller58393c22015-01-07 14:03:30 -080068 if (g_cbs_head) {
69 delayed_callback *cb = g_cbs_head;
70 g_cbs_head = cb->next;
71 if (!g_cbs_head) g_cbs_tail = NULL;
72 gpr_mu_unlock(&g_mu);
73 cb->cb(cb->cb_arg, cb->success);
74 gpr_free(cb);
75 gpr_mu_lock(&g_mu);
76 } else if (grpc_alarm_check(&g_mu, gpr_now(), &deadline)) {
77 } else {
David Garcia Quintas00ff7df2015-05-12 00:19:47 -070078 gpr_mu_unlock(&g_mu);
79 gpr_sleep_until(gpr_time_min(short_deadline, deadline));
80 gpr_mu_lock(&g_mu);
ctiller58393c22015-01-07 14:03:30 -080081 }
82 }
83 gpr_mu_unlock(&g_mu);
84 gpr_event_set(&g_background_callback_executor_done, (void *)1);
85}
86
David Garcia Quintas00ff7df2015-05-12 00:19:47 -070087void grpc_kick_poller(void) { }
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);
ctiller58393c22015-01-07 14:03:30 -080093 grpc_alarm_list_init(gpr_now());
94 g_refs = 0;
95 grpc_iomgr_platform_init();
96 gpr_event_init(&g_background_callback_executor_done);
97 gpr_thd_new(&id, background_callback_executor, NULL, NULL);
98}
99
Craig Tiller32946d32015-01-15 11:37:30 -0800100void grpc_iomgr_shutdown(void) {
ctiller58393c22015-01-07 14:03:30 -0800101 delayed_callback *cb;
102 gpr_timespec shutdown_deadline =
103 gpr_time_add(gpr_now(), gpr_time_from_seconds(10));
104
ctiller58393c22015-01-07 14:03:30 -0800105
106 gpr_mu_lock(&g_mu);
107 g_shutdown = 1;
108 while (g_cbs_head || g_refs) {
109 gpr_log(GPR_DEBUG, "Waiting for %d iomgr objects to be destroyed%s", g_refs,
110 g_cbs_head ? " and executing final callbacks" : "");
111 while (g_cbs_head) {
112 cb = g_cbs_head;
113 g_cbs_head = cb->next;
114 if (!g_cbs_head) g_cbs_tail = NULL;
115 gpr_mu_unlock(&g_mu);
116
117 cb->cb(cb->cb_arg, 0);
118 gpr_free(cb);
119 gpr_mu_lock(&g_mu);
120 }
121 if (g_refs) {
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700122 int timeout = 0;
123 gpr_timespec short_deadline = gpr_time_add(gpr_now(),
124 gpr_time_from_millis(100));
125 while (gpr_cv_wait(&g_rcv, &g_mu, short_deadline) && g_cbs_head == NULL) {
126 if (gpr_time_cmp(gpr_now(), shutdown_deadline) > 0) {
127 timeout = 1;
128 break;
129 }
130 }
131 if (timeout) {
ctiller58393c22015-01-07 14:03:30 -0800132 gpr_log(GPR_DEBUG,
133 "Failed to free %d iomgr objects before shutdown deadline: "
134 "memory leaks are likely",
135 g_refs);
136 break;
137 }
138 }
139 }
140 gpr_mu_unlock(&g_mu);
141
Nicolas "Pixel" Nobleae7b45a2015-02-04 03:28:34 +0100142 grpc_kick_poller();
ctiller58393c22015-01-07 14:03:30 -0800143 gpr_event_wait(&g_background_callback_executor_done, gpr_inf_future);
144
David Klempnerd1785242015-01-28 17:00:21 -0800145 grpc_iomgr_platform_shutdown();
ctiller58393c22015-01-07 14:03:30 -0800146 grpc_alarm_list_shutdown();
147 gpr_mu_destroy(&g_mu);
Nicolas "Pixel" Nobleae7b45a2015-02-04 03:28:34 +0100148 gpr_cv_destroy(&g_rcv);
ctiller58393c22015-01-07 14:03:30 -0800149}
150
Craig Tiller32946d32015-01-15 11:37:30 -0800151void grpc_iomgr_ref(void) {
ctiller58393c22015-01-07 14:03:30 -0800152 gpr_mu_lock(&g_mu);
153 ++g_refs;
154 gpr_mu_unlock(&g_mu);
155}
156
Craig Tiller32946d32015-01-15 11:37:30 -0800157void grpc_iomgr_unref(void) {
ctiller58393c22015-01-07 14:03:30 -0800158 gpr_mu_lock(&g_mu);
159 if (0 == --g_refs) {
Nicolas "Pixel" Nobleae7b45a2015-02-04 03:28:34 +0100160 gpr_cv_signal(&g_rcv);
ctiller58393c22015-01-07 14:03:30 -0800161 }
162 gpr_mu_unlock(&g_mu);
163}
164
165void grpc_iomgr_add_delayed_callback(grpc_iomgr_cb_func cb, void *cb_arg,
166 int success) {
167 delayed_callback *dcb = gpr_malloc(sizeof(delayed_callback));
168 dcb->cb = cb;
169 dcb->cb_arg = cb_arg;
170 dcb->success = success;
171 gpr_mu_lock(&g_mu);
172 dcb->next = NULL;
173 if (!g_cbs_tail) {
174 g_cbs_head = g_cbs_tail = dcb;
175 } else {
176 g_cbs_tail->next = dcb;
177 g_cbs_tail = dcb;
178 }
ctiller58393c22015-01-07 14:03:30 -0800179 gpr_mu_unlock(&g_mu);
180}
181
182void grpc_iomgr_add_callback(grpc_iomgr_cb_func cb, void *cb_arg) {
183 grpc_iomgr_add_delayed_callback(cb, cb_arg, 1);
184}
185
186int grpc_maybe_call_delayed_callbacks(gpr_mu *drop_mu, int success) {
187 int n = 0;
188 gpr_mu *retake_mu = NULL;
189 delayed_callback *cb;
190 for (;;) {
191 /* check for new work */
192 if (!gpr_mu_trylock(&g_mu)) {
193 break;
194 }
195 cb = g_cbs_head;
196 if (!cb) {
197 gpr_mu_unlock(&g_mu);
198 break;
199 }
200 g_cbs_head = cb->next;
201 if (!g_cbs_head) g_cbs_tail = NULL;
202 gpr_mu_unlock(&g_mu);
203 /* if we have a mutex to drop, do so before executing work */
204 if (drop_mu) {
205 gpr_mu_unlock(drop_mu);
206 retake_mu = drop_mu;
207 drop_mu = NULL;
208 }
209 cb->cb(cb->cb_arg, success && cb->success);
210 gpr_free(cb);
211 n++;
212 }
213 if (retake_mu) {
214 gpr_mu_lock(retake_mu);
215 }
216 return n;
Craig Tiller190d3602015-02-18 09:23:38 -0800217}