1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include "src/core/lib/iomgr/iomgr.h"
20
21 #include <grpc/support/alloc.h>
22 #include <grpc/support/port_platform.h>
23 #include <grpc/support/string_util.h>
24 #include <grpc/support/sync.h>
25 #include <inttypes.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "absl/log/log.h"
30 #include "src/core/config/config_vars.h"
31 #include "src/core/lib/iomgr/buffer_list.h"
32 #include "src/core/lib/iomgr/exec_ctx.h"
33 #include "src/core/lib/iomgr/executor.h"
34 #include "src/core/lib/iomgr/internal_errqueue.h"
35 #include "src/core/lib/iomgr/iomgr_internal.h"
36 #include "src/core/lib/iomgr/timer.h"
37 #include "src/core/lib/iomgr/timer_manager.h"
38 #include "src/core/util/crash.h"
39 #include "src/core/util/string.h"
40 #include "src/core/util/thd.h"
41 #include "src/core/util/useful.h"
42
43 static gpr_mu g_mu;
44 static gpr_cv g_rcv;
45 static int g_shutdown;
46 static grpc_iomgr_object g_root_object;
47
grpc_iomgr_init()48 void grpc_iomgr_init() {
49 grpc_core::ExecCtx exec_ctx;
50 if (!grpc_have_determined_iomgr_platform()) {
51 grpc_set_default_iomgr_platform();
52 }
53 g_shutdown = 0;
54 gpr_mu_init(&g_mu);
55 gpr_cv_init(&g_rcv);
56 grpc_core::Executor::InitAll();
57 g_root_object.next = g_root_object.prev = &g_root_object;
58 g_root_object.name = const_cast<char*>("root");
59 grpc_iomgr_platform_init();
60 grpc_timer_list_init();
61 }
62
grpc_iomgr_start()63 void grpc_iomgr_start() { grpc_timer_manager_init(); }
64
count_objects(void)65 static size_t count_objects(void) {
66 grpc_iomgr_object* obj;
67 size_t n = 0;
68 for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
69 n++;
70 }
71 return n;
72 }
73
grpc_iomgr_count_objects_for_testing(void)74 size_t grpc_iomgr_count_objects_for_testing(void) {
75 gpr_mu_lock(&g_mu);
76 size_t ret = count_objects();
77 gpr_mu_unlock(&g_mu);
78 return ret;
79 }
80
dump_objects(const char * kind)81 static void dump_objects(const char* kind) {
82 grpc_iomgr_object* obj;
83 for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
84 VLOG(2) << kind << " OBJECT: " << obj->name << " " << obj;
85 }
86 }
87
grpc_iomgr_shutdown()88 void grpc_iomgr_shutdown() {
89 gpr_timespec shutdown_deadline = gpr_time_add(
90 gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(10, GPR_TIMESPAN));
91 gpr_timespec last_warning_time = gpr_now(GPR_CLOCK_REALTIME);
92
93 {
94 grpc_timer_manager_shutdown();
95 grpc_iomgr_platform_flush();
96
97 gpr_mu_lock(&g_mu);
98 g_shutdown = 1;
99 while (g_root_object.next != &g_root_object) {
100 if (gpr_time_cmp(
101 gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), last_warning_time),
102 gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) {
103 if (g_root_object.next != &g_root_object) {
104 VLOG(2) << "Waiting for " << count_objects()
105 << " iomgr objects to be destroyed";
106 }
107 last_warning_time = gpr_now(GPR_CLOCK_REALTIME);
108 }
109 grpc_core::ExecCtx::Get()->SetNowIomgrShutdown();
110 if (grpc_timer_check(nullptr) == GRPC_TIMERS_FIRED) {
111 gpr_mu_unlock(&g_mu);
112 grpc_core::ExecCtx::Get()->Flush();
113 grpc_iomgr_platform_flush();
114 gpr_mu_lock(&g_mu);
115 continue;
116 }
117 if (g_root_object.next != &g_root_object) {
118 if (grpc_iomgr_abort_on_leaks()) {
119 VLOG(2) << "Failed to free " << count_objects()
120 << " iomgr objects before shutdown deadline: "
121 << "memory leaks are likely";
122 dump_objects("LEAKED");
123 abort();
124 }
125 gpr_timespec short_deadline =
126 gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC),
127 gpr_time_from_millis(100, GPR_TIMESPAN));
128 if (gpr_cv_wait(&g_rcv, &g_mu, short_deadline)) {
129 if (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), shutdown_deadline) >
130 0) {
131 if (g_root_object.next != &g_root_object) {
132 VLOG(2) << "Failed to free " << count_objects()
133 << " iomgr objects before shutdown deadline: "
134 << "memory leaks are likely";
135 dump_objects("LEAKED");
136 }
137 break;
138 }
139 }
140 }
141 }
142 gpr_mu_unlock(&g_mu);
143 grpc_timer_list_shutdown();
144 grpc_core::ExecCtx::Get()->Flush();
145 grpc_core::Executor::ShutdownAll();
146 }
147
148 // ensure all threads have left g_mu
149 gpr_mu_lock(&g_mu);
150 gpr_mu_unlock(&g_mu);
151
152 grpc_iomgr_platform_shutdown();
153 gpr_mu_destroy(&g_mu);
154 gpr_cv_destroy(&g_rcv);
155 }
156
grpc_iomgr_shutdown_background_closure()157 void grpc_iomgr_shutdown_background_closure() {
158 grpc_iomgr_platform_shutdown_background_closure();
159 }
160
grpc_iomgr_is_any_background_poller_thread()161 bool grpc_iomgr_is_any_background_poller_thread() {
162 return grpc_iomgr_platform_is_any_background_poller_thread();
163 }
164
grpc_iomgr_add_closure_to_background_poller(grpc_closure * closure,grpc_error_handle error)165 bool grpc_iomgr_add_closure_to_background_poller(grpc_closure* closure,
166 grpc_error_handle error) {
167 return grpc_iomgr_platform_add_closure_to_background_poller(closure, error);
168 }
169
grpc_iomgr_register_object(grpc_iomgr_object * obj,const char * name)170 void grpc_iomgr_register_object(grpc_iomgr_object* obj, const char* name) {
171 obj->name = gpr_strdup(name);
172 gpr_mu_lock(&g_mu);
173 obj->next = &g_root_object;
174 obj->prev = g_root_object.prev;
175 obj->next->prev = obj->prev->next = obj;
176 gpr_mu_unlock(&g_mu);
177 }
178
grpc_iomgr_unregister_object(grpc_iomgr_object * obj)179 void grpc_iomgr_unregister_object(grpc_iomgr_object* obj) {
180 gpr_mu_lock(&g_mu);
181 obj->next->prev = obj->prev;
182 obj->prev->next = obj->next;
183 gpr_cv_signal(&g_rcv);
184 gpr_mu_unlock(&g_mu);
185 gpr_free(obj->name);
186 }
187
grpc_iomgr_abort_on_leaks(void)188 bool grpc_iomgr_abort_on_leaks(void) {
189 return grpc_core::ConfigVars::Get().AbortOnLeaks();
190 }
191