Imported Upstream version 1.36.0
[platform/upstream/grpc.git] / src / core / lib / surface / init.cc
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/channel/handshaker_registry.h"
36 #include "src/core/lib/debug/stats.h"
37 #include "src/core/lib/debug/trace.h"
38 #include "src/core/lib/gprpp/fork.h"
39 #include "src/core/lib/gprpp/sync.h"
40 #include "src/core/lib/http/parser.h"
41 #include "src/core/lib/iomgr/call_combiner.h"
42 #include "src/core/lib/iomgr/combiner.h"
43 #include "src/core/lib/iomgr/exec_ctx.h"
44 #include "src/core/lib/iomgr/executor.h"
45 #include "src/core/lib/iomgr/iomgr.h"
46 #include "src/core/lib/iomgr/resource_quota.h"
47 #include "src/core/lib/iomgr/timer_manager.h"
48 #include "src/core/lib/profiling/timers.h"
49 #include "src/core/lib/slice/slice_internal.h"
50 #include "src/core/lib/surface/api_trace.h"
51 #include "src/core/lib/surface/call.h"
52 #include "src/core/lib/surface/channel_init.h"
53 #include "src/core/lib/surface/completion_queue.h"
54 #include "src/core/lib/surface/lame_client.h"
55 #include "src/core/lib/surface/server.h"
56 #include "src/core/lib/transport/bdp_estimator.h"
57 #include "src/core/lib/transport/connectivity_state.h"
58 #include "src/core/lib/transport/transport_impl.h"
59
60 /* (generated) built in registry of plugins */
61 extern void grpc_register_built_in_plugins(void);
62
63 #define MAX_PLUGINS 128
64
65 static gpr_once g_basic_init = GPR_ONCE_INIT;
66 static grpc_core::Mutex* g_init_mu;
67 static int g_initializations;
68 static grpc_core::CondVar* g_shutting_down_cv;
69 static bool g_shutting_down;
70
71 static void do_basic_init(void) {
72   gpr_log_verbosity_init();
73   g_init_mu = new grpc_core::Mutex();
74   g_shutting_down_cv = new grpc_core::CondVar();
75   g_shutting_down = false;
76   grpc_register_built_in_plugins();
77   grpc_cq_global_init();
78   grpc_core::grpc_executor_global_init();
79   gpr_time_init();
80   g_initializations = 0;
81 }
82
83 static bool append_filter(grpc_channel_stack_builder* builder, void* arg) {
84   return grpc_channel_stack_builder_append_filter(
85       builder, static_cast<const grpc_channel_filter*>(arg), nullptr, nullptr);
86 }
87
88 static bool prepend_filter(grpc_channel_stack_builder* builder, void* arg) {
89   return grpc_channel_stack_builder_prepend_filter(
90       builder, static_cast<const grpc_channel_filter*>(arg), nullptr, nullptr);
91 }
92
93 static void register_builtin_channel_init() {
94   grpc_channel_init_register_stage(GRPC_CLIENT_SUBCHANNEL,
95                                    GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
96                                    grpc_add_connected_filter, nullptr);
97   grpc_channel_init_register_stage(GRPC_CLIENT_DIRECT_CHANNEL,
98                                    GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
99                                    grpc_add_connected_filter, nullptr);
100   grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL,
101                                    GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
102                                    grpc_add_connected_filter, nullptr);
103   grpc_channel_init_register_stage(
104       GRPC_CLIENT_LAME_CHANNEL, GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
105       append_filter, const_cast<grpc_channel_filter*>(&grpc_lame_filter));
106   grpc_channel_init_register_stage(
107       GRPC_SERVER_CHANNEL, INT_MAX, prepend_filter,
108       const_cast<grpc_channel_filter*>(&grpc_core::Server::kServerTopFilter));
109 }
110
111 typedef struct grpc_plugin {
112   void (*init)();
113   void (*destroy)();
114 } grpc_plugin;
115
116 static grpc_plugin g_all_of_the_plugins[MAX_PLUGINS];
117 static int g_number_of_plugins = 0;
118
119 void grpc_register_plugin(void (*init)(void), void (*destroy)(void)) {
120   GRPC_API_TRACE("grpc_register_plugin(init=%p, destroy=%p)", 2,
121                  ((void*)(intptr_t)init, (void*)(intptr_t)destroy));
122   GPR_ASSERT(g_number_of_plugins != MAX_PLUGINS);
123   g_all_of_the_plugins[g_number_of_plugins].init = init;
124   g_all_of_the_plugins[g_number_of_plugins].destroy = destroy;
125   g_number_of_plugins++;
126 }
127
128 void grpc_init(void) {
129   int i;
130   gpr_once_init(&g_basic_init, do_basic_init);
131
132   grpc_core::MutexLock lock(g_init_mu);
133   if (++g_initializations == 1) {
134     if (g_shutting_down) {
135       g_shutting_down = false;
136       g_shutting_down_cv->SignalAll();
137     }
138     grpc_core::Fork::GlobalInit();
139     grpc_fork_handlers_auto_register();
140     grpc_stats_init();
141     grpc_init_static_metadata_ctx();
142     grpc_slice_intern_init();
143     grpc_mdctx_global_init();
144     grpc_channel_init_init();
145     grpc_core::channelz::ChannelzRegistry::Init();
146     grpc_security_pre_init();
147     grpc_core::ApplicationCallbackExecCtx::GlobalInit();
148     grpc_core::ExecCtx::GlobalInit();
149     grpc_iomgr_init();
150     gpr_timers_global_init();
151     grpc_core::HandshakerRegistry::Init();
152     grpc_security_init();
153     for (i = 0; i < g_number_of_plugins; i++) {
154       if (g_all_of_the_plugins[i].init != nullptr) {
155         g_all_of_the_plugins[i].init();
156       }
157     }
158     /* register channel finalization AFTER all plugins, to ensure that it's run
159      * at the appropriate time */
160     grpc_register_security_filters();
161     register_builtin_channel_init();
162     grpc_tracer_init();
163     /* no more changes to channel init pipelines */
164     grpc_channel_init_finalize();
165     grpc_iomgr_start();
166   }
167
168   GRPC_API_TRACE("grpc_init(void)", 0, ());
169 }
170
171 void grpc_shutdown_internal_locked(void) {
172   int i;
173   {
174     grpc_core::ExecCtx exec_ctx(0);
175     grpc_iomgr_shutdown_background_closure();
176     {
177       grpc_timer_manager_set_threading(false);  // shutdown timer_manager thread
178       grpc_core::Executor::ShutdownAll();
179       for (i = g_number_of_plugins; i >= 0; i--) {
180         if (g_all_of_the_plugins[i].destroy != nullptr) {
181           g_all_of_the_plugins[i].destroy();
182         }
183       }
184     }
185     grpc_iomgr_shutdown();
186     gpr_timers_global_destroy();
187     grpc_tracer_shutdown();
188     grpc_mdctx_global_shutdown();
189     grpc_core::HandshakerRegistry::Shutdown();
190     grpc_slice_intern_shutdown();
191     grpc_core::channelz::ChannelzRegistry::Shutdown();
192     grpc_stats_shutdown();
193     grpc_core::Fork::GlobalShutdown();
194   }
195   grpc_core::ExecCtx::GlobalShutdown();
196   grpc_core::ApplicationCallbackExecCtx::GlobalShutdown();
197   g_shutting_down = false;
198   g_shutting_down_cv->SignalAll();
199   // Absolute last action will be to delete static metadata context.
200   grpc_destroy_static_metadata_ctx();
201 }
202
203 void grpc_shutdown_internal(void* /*ignored*/) {
204   GRPC_API_TRACE("grpc_shutdown_internal", 0, ());
205   grpc_core::MutexLock lock(g_init_mu);
206   // We have released lock from the shutdown thread and it is possible that
207   // another grpc_init has been called, and do nothing if that is the case.
208   if (--g_initializations != 0) {
209     return;
210   }
211   grpc_shutdown_internal_locked();
212 }
213
214 void grpc_shutdown(void) {
215   GRPC_API_TRACE("grpc_shutdown(void)", 0, ());
216   grpc_core::MutexLock lock(g_init_mu);
217
218   if (--g_initializations == 0) {
219     grpc_core::ApplicationCallbackExecCtx* acec =
220         grpc_core::ApplicationCallbackExecCtx::Get();
221     if (!grpc_iomgr_is_any_background_poller_thread() &&
222         (acec == nullptr ||
223          (acec->Flags() & GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD) ==
224              0)) {
225       // just run clean-up when this is called on non-executor thread.
226       gpr_log(GPR_DEBUG, "grpc_shutdown starts clean-up now");
227       g_shutting_down = true;
228       grpc_shutdown_internal_locked();
229     } else {
230       // spawn a detached thread to do the actual clean up in case we are
231       // currently in an executor thread.
232       gpr_log(GPR_DEBUG, "grpc_shutdown spawns clean-up thread");
233       g_initializations++;
234       g_shutting_down = true;
235       grpc_core::Thread cleanup_thread(
236           "grpc_shutdown", grpc_shutdown_internal, nullptr, nullptr,
237           grpc_core::Thread::Options().set_joinable(false).set_tracked(false));
238       cleanup_thread.Start();
239     }
240   }
241 }
242
243 void grpc_shutdown_blocking(void) {
244   GRPC_API_TRACE("grpc_shutdown_blocking(void)", 0, ());
245   grpc_core::MutexLock lock(g_init_mu);
246   if (--g_initializations == 0) {
247     g_shutting_down = true;
248     grpc_shutdown_internal_locked();
249   }
250 }
251
252 int grpc_is_initialized(void) {
253   int r;
254   gpr_once_init(&g_basic_init, do_basic_init);
255   grpc_core::MutexLock lock(g_init_mu);
256   r = g_initializations > 0;
257   return r;
258 }
259
260 void grpc_maybe_wait_for_async_shutdown(void) {
261   gpr_once_init(&g_basic_init, do_basic_init);
262   grpc_core::MutexLock lock(g_init_mu);
263   while (g_shutting_down) {
264     g_shutting_down_cv->Wait(g_init_mu);
265   }
266 }