Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, 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 :
45 : #include "src/core/iomgr/iomgr_internal.h"
46 : #include "src/core/iomgr/alarm_internal.h"
47 : #include "src/core/support/string.h"
48 :
49 : static gpr_mu g_mu;
50 : static gpr_cv g_rcv;
51 : static int g_shutdown;
52 : static grpc_iomgr_object g_root_object;
53 :
54 2506 : void grpc_iomgr_init(void) {
55 2506 : g_shutdown = 0;
56 2506 : gpr_mu_init(&g_mu);
57 2506 : gpr_cv_init(&g_rcv);
58 2506 : grpc_alarm_list_init(gpr_now(GPR_CLOCK_MONOTONIC));
59 2506 : g_root_object.next = g_root_object.prev = &g_root_object;
60 2506 : g_root_object.name = "root";
61 2506 : grpc_iomgr_platform_init();
62 2506 : grpc_pollset_global_init();
63 2506 : }
64 :
65 0 : static size_t count_objects(void) {
66 : grpc_iomgr_object *obj;
67 0 : size_t n = 0;
68 0 : for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
69 0 : n++;
70 : }
71 0 : return n;
72 : }
73 :
74 0 : static void dump_objects(const char *kind) {
75 : grpc_iomgr_object *obj;
76 0 : for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
77 0 : gpr_log(GPR_DEBUG, "%s OBJECT: %s %p", kind, obj->name, obj);
78 : }
79 0 : }
80 :
81 2506 : void grpc_iomgr_shutdown(void) {
82 2506 : gpr_timespec shutdown_deadline = gpr_time_add(
83 : gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(10, GPR_TIMESPAN));
84 2506 : gpr_timespec last_warning_time = gpr_now(GPR_CLOCK_REALTIME);
85 2506 : grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
86 :
87 2506 : grpc_iomgr_platform_flush();
88 :
89 2506 : gpr_mu_lock(&g_mu);
90 2506 : g_shutdown = 1;
91 5589 : while (g_root_object.next != &g_root_object) {
92 577 : if (gpr_time_cmp(
93 : gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), last_warning_time),
94 : gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) {
95 0 : if (g_root_object.next != &g_root_object) {
96 0 : gpr_log(GPR_DEBUG, "Waiting for %d iomgr objects to be destroyed",
97 : count_objects());
98 : }
99 0 : last_warning_time = gpr_now(GPR_CLOCK_REALTIME);
100 : }
101 577 : if (grpc_alarm_check(&exec_ctx, gpr_inf_future(GPR_CLOCK_MONOTONIC),
102 : NULL)) {
103 238 : gpr_mu_unlock(&g_mu);
104 238 : grpc_exec_ctx_flush(&exec_ctx);
105 238 : gpr_mu_lock(&g_mu);
106 238 : continue;
107 : }
108 339 : if (g_root_object.next != &g_root_object) {
109 339 : gpr_timespec short_deadline = gpr_time_add(
110 : gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(100, GPR_TIMESPAN));
111 339 : if (gpr_cv_wait(&g_rcv, &g_mu, short_deadline)) {
112 0 : if (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), shutdown_deadline) > 0) {
113 0 : if (g_root_object.next != &g_root_object) {
114 0 : gpr_log(GPR_DEBUG,
115 : "Failed to free %d iomgr objects before shutdown deadline: "
116 : "memory leaks are likely",
117 : count_objects());
118 0 : dump_objects("LEAKED");
119 : }
120 0 : break;
121 : }
122 : }
123 : }
124 : }
125 2506 : gpr_mu_unlock(&g_mu);
126 :
127 2506 : grpc_alarm_list_shutdown(&exec_ctx);
128 2506 : grpc_exec_ctx_finish(&exec_ctx);
129 :
130 : /* ensure all threads have left g_mu */
131 2506 : gpr_mu_lock(&g_mu);
132 2506 : gpr_mu_unlock(&g_mu);
133 :
134 2506 : grpc_pollset_global_shutdown();
135 2506 : grpc_iomgr_platform_shutdown();
136 2506 : gpr_mu_destroy(&g_mu);
137 2506 : gpr_cv_destroy(&g_rcv);
138 2506 : }
139 :
140 16596 : void grpc_iomgr_register_object(grpc_iomgr_object *obj, const char *name) {
141 16596 : obj->name = gpr_strdup(name);
142 16596 : gpr_mu_lock(&g_mu);
143 16596 : obj->next = &g_root_object;
144 16596 : obj->prev = g_root_object.prev;
145 16596 : obj->next->prev = obj->prev->next = obj;
146 16596 : gpr_mu_unlock(&g_mu);
147 16596 : }
148 :
149 16596 : void grpc_iomgr_unregister_object(grpc_iomgr_object *obj) {
150 16596 : gpr_mu_lock(&g_mu);
151 16596 : obj->next->prev = obj->prev;
152 16596 : obj->prev->next = obj->next;
153 16596 : gpr_cv_signal(&g_rcv);
154 16596 : gpr_mu_unlock(&g_mu);
155 16596 : gpr_free(obj->name);
156 16596 : }
|