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 <grpc/support/port_platform.h>
20 
21 #include "src/core/lib/surface/init.h"
22 
23 #include <limits.h>
24 #include <memory.h>
25 
26 #include <grpc/fork.h>
27 #include <grpc/grpc.h>
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
30 #include <grpc/support/time.h>
31 
32 #include "src/core/lib/channel/channel_stack.h"
33 #include "src/core/lib/channel/channelz_registry.h"
34 #include "src/core/lib/channel/connected_channel.h"
35 #include "src/core/lib/debug/stats.h"
36 #include "src/core/lib/debug/trace.h"
37 #include "src/core/lib/gprpp/fork.h"
38 #include "src/core/lib/gprpp/sync.h"
39 #include "src/core/lib/http/parser.h"
40 #include "src/core/lib/iomgr/call_combiner.h"
41 #include "src/core/lib/iomgr/combiner.h"
42 #include "src/core/lib/iomgr/exec_ctx.h"
43 #include "src/core/lib/iomgr/executor.h"
44 #include "src/core/lib/iomgr/iomgr.h"
45 #include "src/core/lib/iomgr/resource_quota.h"
46 #include "src/core/lib/iomgr/timer_manager.h"
47 #include "src/core/lib/profiling/timers.h"
48 #include "src/core/lib/slice/slice_internal.h"
49 #include "src/core/lib/surface/api_trace.h"
50 #include "src/core/lib/surface/call.h"
51 #include "src/core/lib/surface/completion_queue.h"
52 #include "src/core/lib/surface/lame_client.h"
53 #include "src/core/lib/surface/server.h"
54 #include "src/core/lib/transport/bdp_estimator.h"
55 #include "src/core/lib/transport/connectivity_state.h"
56 #include "src/core/lib/transport/transport_impl.h"
57 
58 /* (generated) built in registry of plugins */
59 extern void grpc_register_built_in_plugins(void);
60 
61 #define MAX_PLUGINS 128
62 
63 static gpr_once g_basic_init = GPR_ONCE_INIT;
64 static grpc_core::Mutex* g_init_mu;
65 static int g_initializations ABSL_GUARDED_BY(g_init_mu) = 0;
66 static grpc_core::CondVar* g_shutting_down_cv;
67 static bool g_shutting_down ABSL_GUARDED_BY(g_init_mu) = false;
68 
do_basic_init(void)69 static void do_basic_init(void) {
70   gpr_log_verbosity_init();
71   g_init_mu = new grpc_core::Mutex();
72   g_shutting_down_cv = new grpc_core::CondVar();
73   grpc_register_built_in_plugins();
74   grpc_cq_global_init();
75   grpc_core::grpc_executor_global_init();
76   gpr_time_init();
77 }
78 
79 typedef struct grpc_plugin {
80   void (*init)();
81   void (*destroy)();
82 } grpc_plugin;
83 
84 static grpc_plugin g_all_of_the_plugins[MAX_PLUGINS];
85 static int g_number_of_plugins = 0;
86 
grpc_register_plugin(void (* init)(void),void (* destroy)(void))87 void grpc_register_plugin(void (*init)(void), void (*destroy)(void)) {
88   GRPC_API_TRACE("grpc_register_plugin(init=%p, destroy=%p)", 2,
89                  ((void*)(intptr_t)init, (void*)(intptr_t)destroy));
90   GPR_ASSERT(g_number_of_plugins != MAX_PLUGINS);
91   g_all_of_the_plugins[g_number_of_plugins].init = init;
92   g_all_of_the_plugins[g_number_of_plugins].destroy = destroy;
93   g_number_of_plugins++;
94 }
95 
grpc_init(void)96 void grpc_init(void) {
97   gpr_once_init(&g_basic_init, do_basic_init);
98 
99   grpc_core::MutexLock lock(g_init_mu);
100   if (++g_initializations == 1) {
101     if (g_shutting_down) {
102       g_shutting_down = false;
103       g_shutting_down_cv->SignalAll();
104     }
105     grpc_core::Fork::GlobalInit();
106     grpc_fork_handlers_auto_register();
107     grpc_stats_init();
108     grpc_slice_intern_init();
109     grpc_mdctx_global_init();
110     grpc_core::channelz::ChannelzRegistry::Init();
111     grpc_security_pre_init();
112     grpc_core::ApplicationCallbackExecCtx::GlobalInit();
113     grpc_core::ExecCtx::GlobalInit();
114     grpc_iomgr_init();
115     gpr_timers_global_init();
116     for (int i = 0; i < g_number_of_plugins; i++) {
117       if (g_all_of_the_plugins[i].init != nullptr) {
118         g_all_of_the_plugins[i].init();
119       }
120     }
121     grpc_tracer_init();
122     grpc_iomgr_start();
123   }
124 
125   GRPC_API_TRACE("grpc_init(void)", 0, ());
126 }
127 
grpc_shutdown_internal_locked(void)128 void grpc_shutdown_internal_locked(void)
129     ABSL_EXCLUSIVE_LOCKS_REQUIRED(g_init_mu) {
130   int i;
131   {
132     grpc_core::ExecCtx exec_ctx(0);
133     grpc_iomgr_shutdown_background_closure();
134     {
135       grpc_timer_manager_set_threading(false);  // shutdown timer_manager thread
136       for (i = g_number_of_plugins; i >= 0; i--) {
137         if (g_all_of_the_plugins[i].destroy != nullptr) {
138           g_all_of_the_plugins[i].destroy();
139         }
140       }
141     }
142     grpc_iomgr_shutdown();
143     gpr_timers_global_destroy();
144     grpc_tracer_shutdown();
145     grpc_mdctx_global_shutdown();
146     grpc_slice_intern_shutdown();
147     grpc_core::channelz::ChannelzRegistry::Shutdown();
148     grpc_stats_shutdown();
149     grpc_core::Fork::GlobalShutdown();
150   }
151   grpc_core::ExecCtx::GlobalShutdown();
152   grpc_core::ApplicationCallbackExecCtx::GlobalShutdown();
153   g_shutting_down = false;
154   g_shutting_down_cv->SignalAll();
155 }
156 
grpc_shutdown_internal(void *)157 void grpc_shutdown_internal(void* /*ignored*/) {
158   GRPC_API_TRACE("grpc_shutdown_internal", 0, ());
159   grpc_core::MutexLock lock(g_init_mu);
160   // We have released lock from the shutdown thread and it is possible that
161   // another grpc_init has been called, and do nothing if that is the case.
162   if (--g_initializations != 0) {
163     return;
164   }
165   grpc_shutdown_internal_locked();
166 }
167 
grpc_shutdown(void)168 void grpc_shutdown(void) {
169   GRPC_API_TRACE("grpc_shutdown(void)", 0, ());
170   grpc_core::MutexLock lock(g_init_mu);
171 
172   if (--g_initializations == 0) {
173     grpc_core::ApplicationCallbackExecCtx* acec =
174         grpc_core::ApplicationCallbackExecCtx::Get();
175     if (!grpc_iomgr_is_any_background_poller_thread() &&
176         (acec == nullptr ||
177          (acec->Flags() & GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD) ==
178              0)) {
179       // just run clean-up when this is called on non-executor thread.
180       gpr_log(GPR_DEBUG, "grpc_shutdown starts clean-up now");
181       g_shutting_down = true;
182       grpc_shutdown_internal_locked();
183     } else {
184       // spawn a detached thread to do the actual clean up in case we are
185       // currently in an executor thread.
186       gpr_log(GPR_DEBUG, "grpc_shutdown spawns clean-up thread");
187       g_initializations++;
188       g_shutting_down = true;
189       grpc_core::Thread cleanup_thread(
190           "grpc_shutdown", grpc_shutdown_internal, nullptr, nullptr,
191           grpc_core::Thread::Options().set_joinable(false).set_tracked(false));
192       cleanup_thread.Start();
193     }
194   }
195 }
196 
grpc_shutdown_blocking(void)197 void grpc_shutdown_blocking(void) {
198   GRPC_API_TRACE("grpc_shutdown_blocking(void)", 0, ());
199   grpc_core::MutexLock lock(g_init_mu);
200   if (--g_initializations == 0) {
201     g_shutting_down = true;
202     grpc_shutdown_internal_locked();
203   }
204 }
205 
grpc_is_initialized(void)206 int grpc_is_initialized(void) {
207   int r;
208   gpr_once_init(&g_basic_init, do_basic_init);
209   grpc_core::MutexLock lock(g_init_mu);
210   r = g_initializations > 0;
211   return r;
212 }
213 
grpc_maybe_wait_for_async_shutdown(void)214 void grpc_maybe_wait_for_async_shutdown(void) {
215   gpr_once_init(&g_basic_init, do_basic_init);
216   grpc_core::MutexLock lock(g_init_mu);
217   while (g_shutting_down) {
218     g_shutting_down_cv->Wait(g_init_mu);
219   }
220 }
221