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