Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(194)

Side by Side Diff: third_party/grpc/src/core/iomgr/iomgr.c

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015-2016, Google Inc.
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 #include <string.h>
38
39 #include <grpc/support/alloc.h>
40 #include <grpc/support/log.h>
41 #include <grpc/support/string_util.h>
42 #include <grpc/support/sync.h>
43 #include <grpc/support/thd.h>
44 #include <grpc/support/useful.h>
45
46 #include "src/core/iomgr/iomgr_internal.h"
47 #include "src/core/iomgr/timer.h"
48 #include "src/core/support/env.h"
49 #include "src/core/support/string.h"
50
51 static gpr_mu g_mu;
52 static gpr_cv g_rcv;
53 static int g_shutdown;
54 static grpc_iomgr_object g_root_object;
55
56 void grpc_iomgr_init(void) {
57 g_shutdown = 0;
58 gpr_mu_init(&g_mu);
59 gpr_cv_init(&g_rcv);
60 grpc_timer_list_init(gpr_now(GPR_CLOCK_MONOTONIC));
61 g_root_object.next = g_root_object.prev = &g_root_object;
62 g_root_object.name = "root";
63 grpc_iomgr_platform_init();
64 grpc_pollset_global_init();
65 }
66
67 static size_t count_objects(void) {
68 grpc_iomgr_object *obj;
69 size_t n = 0;
70 for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
71 n++;
72 }
73 return n;
74 }
75
76 static void dump_objects(const char *kind) {
77 grpc_iomgr_object *obj;
78 for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
79 gpr_log(GPR_DEBUG, "%s OBJECT: %s %p", kind, obj->name, obj);
80 }
81 }
82
83 void grpc_iomgr_shutdown(void) {
84 gpr_timespec shutdown_deadline = gpr_time_add(
85 gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(10, GPR_TIMESPAN));
86 gpr_timespec last_warning_time = gpr_now(GPR_CLOCK_REALTIME);
87 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
88
89 grpc_iomgr_platform_flush();
90
91 gpr_mu_lock(&g_mu);
92 g_shutdown = 1;
93 while (g_root_object.next != &g_root_object) {
94 if (gpr_time_cmp(
95 gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), last_warning_time),
96 gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) {
97 if (g_root_object.next != &g_root_object) {
98 gpr_log(GPR_DEBUG, "Waiting for %d iomgr objects to be destroyed",
99 count_objects());
100 }
101 last_warning_time = gpr_now(GPR_CLOCK_REALTIME);
102 }
103 if (grpc_timer_check(&exec_ctx, gpr_inf_future(GPR_CLOCK_MONOTONIC),
104 NULL)) {
105 gpr_mu_unlock(&g_mu);
106 grpc_exec_ctx_flush(&exec_ctx);
107 gpr_mu_lock(&g_mu);
108 continue;
109 }
110 if (g_root_object.next != &g_root_object) {
111 gpr_timespec short_deadline = gpr_time_add(
112 gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(100, GPR_TIMESPAN));
113 if (gpr_cv_wait(&g_rcv, &g_mu, short_deadline)) {
114 if (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), shutdown_deadline) > 0) {
115 if (g_root_object.next != &g_root_object) {
116 gpr_log(GPR_DEBUG,
117 "Failed to free %d iomgr objects before shutdown deadline: "
118 "memory leaks are likely",
119 count_objects());
120 dump_objects("LEAKED");
121 if (grpc_iomgr_abort_on_leaks()) {
122 abort();
123 }
124 }
125 break;
126 }
127 }
128 }
129 }
130 gpr_mu_unlock(&g_mu);
131
132 grpc_timer_list_shutdown(&exec_ctx);
133 grpc_exec_ctx_finish(&exec_ctx);
134
135 /* ensure all threads have left g_mu */
136 gpr_mu_lock(&g_mu);
137 gpr_mu_unlock(&g_mu);
138
139 grpc_pollset_global_shutdown();
140 grpc_iomgr_platform_shutdown();
141 gpr_mu_destroy(&g_mu);
142 gpr_cv_destroy(&g_rcv);
143 }
144
145 void grpc_iomgr_register_object(grpc_iomgr_object *obj, const char *name) {
146 obj->name = gpr_strdup(name);
147 gpr_mu_lock(&g_mu);
148 obj->next = &g_root_object;
149 obj->prev = g_root_object.prev;
150 obj->next->prev = obj->prev->next = obj;
151 gpr_mu_unlock(&g_mu);
152 }
153
154 void grpc_iomgr_unregister_object(grpc_iomgr_object *obj) {
155 gpr_mu_lock(&g_mu);
156 obj->next->prev = obj->prev;
157 obj->prev->next = obj->next;
158 gpr_cv_signal(&g_rcv);
159 gpr_mu_unlock(&g_mu);
160 gpr_free(obj->name);
161 }
162
163 bool grpc_iomgr_abort_on_leaks(void) {
164 char *env = gpr_getenv("GRPC_ABORT_ON_LEAKS");
165 if (env == NULL) return false;
166 static const char *truthy[] = {"yes", "Yes", "YES", "true",
167 "True", "TRUE", "1"};
168 for (size_t i = 0; i < GPR_ARRAY_SIZE(truthy); i++) {
169 if (0 == strcmp(env, truthy[i])) return true;
170 }
171 return false;
172 }
OLDNEW
« no previous file with comments | « third_party/grpc/src/core/iomgr/iomgr.h ('k') | third_party/grpc/src/core/iomgr/iomgr_internal.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698