ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 4 | * 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 Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 40 | #include "src/core/support/string.h" |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 41 | #include <grpc/support/alloc.h> |
| 42 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame^] | 43 | #include <grpc/support/string_util.h> |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 44 | #include <grpc/support/sync.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame^] | 45 | #include <grpc/support/thd.h> |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 46 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 47 | static gpr_mu g_mu; |
Nicolas "Pixel" Noble | ae7b45a | 2015-02-04 03:28:34 +0100 | [diff] [blame] | 48 | static gpr_cv g_rcv; |
David Garcia Quintas | 5f228f5 | 2015-05-26 19:58:50 -0700 | [diff] [blame] | 49 | static grpc_iomgr_closure *g_cbs_head = NULL; |
| 50 | static grpc_iomgr_closure *g_cbs_tail = NULL; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 51 | static int g_shutdown; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 52 | static gpr_event g_background_callback_executor_done; |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 53 | static grpc_iomgr_object g_root_object; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 54 | |
| 55 | /* Execute followup callbacks continuously. |
| 56 | Other threads may check in and help during pollset_work() */ |
| 57 | static 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 Quintas | 00ff7df | 2015-05-12 00:19:47 -0700 | [diff] [blame] | 61 | gpr_timespec short_deadline = |
| 62 | gpr_time_add(gpr_now(), gpr_time_from_millis(100)); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 63 | if (g_cbs_head) { |
David Garcia Quintas | a30020f | 2015-05-27 19:21:01 -0700 | [diff] [blame] | 64 | grpc_iomgr_closure *closure = g_cbs_head; |
| 65 | g_cbs_head = closure->next; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 66 | if (!g_cbs_head) g_cbs_tail = NULL; |
| 67 | gpr_mu_unlock(&g_mu); |
David Garcia Quintas | a30020f | 2015-05-27 19:21:01 -0700 | [diff] [blame] | 68 | closure->cb(closure->cb_arg, closure->success); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 69 | gpr_mu_lock(&g_mu); |
| 70 | } else if (grpc_alarm_check(&g_mu, gpr_now(), &deadline)) { |
| 71 | } else { |
David Garcia Quintas | 00ff7df | 2015-05-12 00:19:47 -0700 | [diff] [blame] | 72 | gpr_mu_unlock(&g_mu); |
| 73 | gpr_sleep_until(gpr_time_min(short_deadline, deadline)); |
| 74 | gpr_mu_lock(&g_mu); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | gpr_mu_unlock(&g_mu); |
| 78 | gpr_event_set(&g_background_callback_executor_done, (void *)1); |
| 79 | } |
| 80 | |
David Garcia Quintas | 5b984ce | 2015-05-12 16:04:28 -0700 | [diff] [blame] | 81 | void grpc_kick_poller(void) { |
| 82 | /* Empty. The background callback executor polls periodically. The activity |
| 83 | * the kicker is trying to draw the executor's attention to will be picked up |
| 84 | * either by one of the periodic wakeups or by one of the polling application |
| 85 | * threads. */ |
| 86 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 87 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 88 | void grpc_iomgr_init(void) { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 89 | gpr_thd_id id; |
| 90 | gpr_mu_init(&g_mu); |
Nicolas "Pixel" Noble | ae7b45a | 2015-02-04 03:28:34 +0100 | [diff] [blame] | 91 | gpr_cv_init(&g_rcv); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 92 | grpc_alarm_list_init(gpr_now()); |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 93 | g_root_object.next = g_root_object.prev = &g_root_object; |
| 94 | g_root_object.name = "root"; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 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 Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 100 | static size_t count_objects(void) { |
| 101 | grpc_iomgr_object *obj; |
| 102 | size_t n = 0; |
| 103 | for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) { |
| 104 | n++; |
| 105 | } |
| 106 | return n; |
| 107 | } |
| 108 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 109 | void grpc_iomgr_shutdown(void) { |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 110 | grpc_iomgr_object *obj; |
David Garcia Quintas | a30020f | 2015-05-27 19:21:01 -0700 | [diff] [blame] | 111 | grpc_iomgr_closure *closure; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 112 | gpr_timespec shutdown_deadline = |
| 113 | gpr_time_add(gpr_now(), gpr_time_from_seconds(10)); |
| 114 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 115 | |
| 116 | gpr_mu_lock(&g_mu); |
| 117 | g_shutdown = 1; |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 118 | while (g_cbs_head || g_root_object.next != &g_root_object) { |
| 119 | size_t nobjs = count_objects(); |
| 120 | gpr_log(GPR_DEBUG, "Waiting for %d iomgr objects to be destroyed%s", nobjs, |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 121 | g_cbs_head ? " and executing final callbacks" : ""); |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 122 | if (g_cbs_head) { |
| 123 | do { |
Craig Tiller | 3d67c7c | 2015-06-01 20:10:13 -0700 | [diff] [blame] | 124 | closure = g_cbs_head; |
| 125 | g_cbs_head = closure->next; |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 126 | if (!g_cbs_head) g_cbs_tail = NULL; |
| 127 | gpr_mu_unlock(&g_mu); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 128 | |
Craig Tiller | 3d67c7c | 2015-06-01 20:10:13 -0700 | [diff] [blame] | 129 | closure->cb(closure->cb_arg, 0); |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 130 | gpr_mu_lock(&g_mu); |
| 131 | } while (g_cbs_head); |
| 132 | continue; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 133 | } |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 134 | if (nobjs > 0) { |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 135 | int timeout = 0; |
| 136 | gpr_timespec short_deadline = gpr_time_add(gpr_now(), |
| 137 | gpr_time_from_millis(100)); |
| 138 | while (gpr_cv_wait(&g_rcv, &g_mu, short_deadline) && g_cbs_head == NULL) { |
| 139 | if (gpr_time_cmp(gpr_now(), shutdown_deadline) > 0) { |
| 140 | timeout = 1; |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | if (timeout) { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 145 | gpr_log(GPR_DEBUG, |
| 146 | "Failed to free %d iomgr objects before shutdown deadline: " |
| 147 | "memory leaks are likely", |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 148 | count_objects()); |
| 149 | for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) { |
| 150 | gpr_log(GPR_DEBUG, "LEAKED OBJECT: %s", obj->name); |
| 151 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 152 | break; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | gpr_mu_unlock(&g_mu); |
| 157 | |
Nicolas "Pixel" Noble | ae7b45a | 2015-02-04 03:28:34 +0100 | [diff] [blame] | 158 | grpc_kick_poller(); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 159 | gpr_event_wait(&g_background_callback_executor_done, gpr_inf_future); |
| 160 | |
David Klempner | d178524 | 2015-01-28 17:00:21 -0800 | [diff] [blame] | 161 | grpc_iomgr_platform_shutdown(); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 162 | grpc_alarm_list_shutdown(); |
| 163 | gpr_mu_destroy(&g_mu); |
Nicolas "Pixel" Noble | ae7b45a | 2015-02-04 03:28:34 +0100 | [diff] [blame] | 164 | gpr_cv_destroy(&g_rcv); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 167 | void grpc_iomgr_register_object(grpc_iomgr_object *obj, const char *name) { |
| 168 | obj->name = gpr_strdup(name); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 169 | gpr_mu_lock(&g_mu); |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 170 | obj->next = &g_root_object; |
| 171 | obj->prev = obj->next->prev; |
| 172 | obj->next->prev = obj->prev->next = obj; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 173 | gpr_mu_unlock(&g_mu); |
| 174 | } |
| 175 | |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 176 | void grpc_iomgr_unregister_object(grpc_iomgr_object *obj) { |
| 177 | gpr_free(obj->name); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 178 | gpr_mu_lock(&g_mu); |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 179 | obj->next->prev = obj->prev; |
| 180 | obj->prev->next = obj->next; |
| 181 | gpr_cv_signal(&g_rcv); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 182 | gpr_mu_unlock(&g_mu); |
| 183 | } |
| 184 | |
David Garcia Quintas | a30020f | 2015-05-27 19:21:01 -0700 | [diff] [blame] | 185 | |
| 186 | void grpc_iomgr_closure_init(grpc_iomgr_closure *closure, grpc_iomgr_cb_func cb, |
| 187 | void *cb_arg) { |
| 188 | closure->cb = cb; |
| 189 | closure->cb_arg = cb_arg; |
David Garcia Quintas | a30020f | 2015-05-27 19:21:01 -0700 | [diff] [blame] | 190 | closure->next = NULL; |
David Garcia Quintas | 5f228f5 | 2015-05-26 19:58:50 -0700 | [diff] [blame] | 191 | } |
| 192 | |
David Garcia Quintas | a30020f | 2015-05-27 19:21:01 -0700 | [diff] [blame] | 193 | void grpc_iomgr_add_delayed_callback(grpc_iomgr_closure *closure, int success) { |
| 194 | closure->success = success; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 195 | gpr_mu_lock(&g_mu); |
David Garcia Quintas | a30020f | 2015-05-27 19:21:01 -0700 | [diff] [blame] | 196 | closure->next = NULL; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 197 | if (!g_cbs_tail) { |
David Garcia Quintas | a30020f | 2015-05-27 19:21:01 -0700 | [diff] [blame] | 198 | g_cbs_head = g_cbs_tail = closure; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 199 | } else { |
David Garcia Quintas | a30020f | 2015-05-27 19:21:01 -0700 | [diff] [blame] | 200 | g_cbs_tail->next = closure; |
| 201 | g_cbs_tail = closure; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 202 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 203 | gpr_mu_unlock(&g_mu); |
| 204 | } |
| 205 | |
David Garcia Quintas | 5f228f5 | 2015-05-26 19:58:50 -0700 | [diff] [blame] | 206 | |
David Garcia Quintas | a30020f | 2015-05-27 19:21:01 -0700 | [diff] [blame] | 207 | void grpc_iomgr_add_callback(grpc_iomgr_closure *closure) { |
David Garcia Quintas | 1c762bd | 2015-05-31 17:04:43 -0700 | [diff] [blame] | 208 | grpc_iomgr_add_delayed_callback(closure, 1 /* GPR_TRUE */); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 209 | } |
| 210 | |
David Garcia Quintas | 5f228f5 | 2015-05-26 19:58:50 -0700 | [diff] [blame] | 211 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 212 | int grpc_maybe_call_delayed_callbacks(gpr_mu *drop_mu, int success) { |
| 213 | int n = 0; |
| 214 | gpr_mu *retake_mu = NULL; |
David Garcia Quintas | a30020f | 2015-05-27 19:21:01 -0700 | [diff] [blame] | 215 | grpc_iomgr_closure *closure; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 216 | for (;;) { |
| 217 | /* check for new work */ |
| 218 | if (!gpr_mu_trylock(&g_mu)) { |
| 219 | break; |
| 220 | } |
David Garcia Quintas | a30020f | 2015-05-27 19:21:01 -0700 | [diff] [blame] | 221 | closure = g_cbs_head; |
| 222 | if (!closure) { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 223 | gpr_mu_unlock(&g_mu); |
| 224 | break; |
| 225 | } |
David Garcia Quintas | a30020f | 2015-05-27 19:21:01 -0700 | [diff] [blame] | 226 | g_cbs_head = closure->next; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 227 | if (!g_cbs_head) g_cbs_tail = NULL; |
| 228 | gpr_mu_unlock(&g_mu); |
| 229 | /* if we have a mutex to drop, do so before executing work */ |
| 230 | if (drop_mu) { |
| 231 | gpr_mu_unlock(drop_mu); |
| 232 | retake_mu = drop_mu; |
| 233 | drop_mu = NULL; |
| 234 | } |
David Garcia Quintas | a30020f | 2015-05-27 19:21:01 -0700 | [diff] [blame] | 235 | closure->cb(closure->cb_arg, success && closure->success); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 236 | n++; |
| 237 | } |
| 238 | if (retake_mu) { |
| 239 | gpr_mu_lock(retake_mu); |
| 240 | } |
| 241 | return n; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 242 | } |