Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc-cloned / deps / grpc / 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_init_static_metadata_ctx();
138     grpc_slice_intern_init();
139     grpc_mdctx_global_init();
140     grpc_channel_init_init();
141     grpc_core::channelz::ChannelzRegistry::Init();
142     grpc_security_pre_init();
143     grpc_core::ApplicationCallbackExecCtx::GlobalInit();
144     grpc_core::ExecCtx::GlobalInit();
145     grpc_iomgr_init();
146     gpr_timers_global_init();
147     grpc_core::HandshakerRegistry::Init();
148     grpc_security_init();
149     for (i = 0; i < g_number_of_plugins; i++) {
150       if (g_all_of_the_plugins[i].init != nullptr) {
151         g_all_of_the_plugins[i].init();
152       }
153     }
154     /* register channel finalization AFTER all plugins, to ensure that it's run
155      * at the appropriate time */
156     grpc_register_security_filters();
157     register_builtin_channel_init();
158     grpc_tracer_init();
159     /* no more changes to channel init pipelines */
160     grpc_channel_init_finalize();
161     grpc_iomgr_start();
162   }
163
164   GRPC_API_TRACE("grpc_init(void)", 0, ());
165 }
166
167 void grpc_shutdown_internal_locked(void) {
168   int i;
169   {
170     grpc_core::ExecCtx exec_ctx(0);
171     grpc_iomgr_shutdown_background_closure();
172     {
173       grpc_timer_manager_set_threading(false);  // shutdown timer_manager thread
174       grpc_core::Executor::ShutdownAll();
175       for (i = g_number_of_plugins; i >= 0; i--) {
176         if (g_all_of_the_plugins[i].destroy != nullptr) {
177           g_all_of_the_plugins[i].destroy();
178         }
179       }
180     }
181     grpc_iomgr_shutdown();
182     gpr_timers_global_destroy();
183     grpc_tracer_shutdown();
184     grpc_mdctx_global_shutdown();
185     grpc_core::HandshakerRegistry::Shutdown();
186     grpc_slice_intern_shutdown();
187     grpc_core::channelz::ChannelzRegistry::Shutdown();
188     grpc_stats_shutdown();
189     grpc_core::Fork::GlobalShutdown();
190   }
191   grpc_core::ExecCtx::GlobalShutdown();
192   grpc_core::ApplicationCallbackExecCtx::GlobalShutdown();
193   g_shutting_down = false;
194   gpr_cv_broadcast(g_shutting_down_cv);
195   // Absolute last action will be to delete static metadata context.
196   grpc_destroy_static_metadata_ctx();
197 }
198
199 void grpc_shutdown_internal(void* ignored) {
200   GRPC_API_TRACE("grpc_shutdown_internal", 0, ());
201   grpc_core::MutexLock lock(&g_init_mu);
202   // We have released lock from the shutdown thread and it is possible that
203   // another grpc_init has been called, and do nothing if that is the case.
204   if (--g_initializations != 0) {
205     return;
206   }
207   grpc_shutdown_internal_locked();
208 }
209
210 void grpc_shutdown(void) {
211   GRPC_API_TRACE("grpc_shutdown(void)", 0, ());
212   grpc_core::MutexLock lock(&g_init_mu);
213   if (--g_initializations == 0) {
214     g_initializations++;
215     g_shutting_down = true;
216     // spawn a detached thread to do the actual clean up in case we are
217     // currently in an executor thread.
218     grpc_core::Thread cleanup_thread(
219         "grpc_shutdown", grpc_shutdown_internal, nullptr, nullptr,
220         grpc_core::Thread::Options().set_joinable(false).set_tracked(false));
221     cleanup_thread.Start();
222   }
223 }
224
225 void grpc_shutdown_blocking(void) {
226   GRPC_API_TRACE("grpc_shutdown_blocking(void)", 0, ());
227   grpc_core::MutexLock lock(&g_init_mu);
228   if (--g_initializations == 0) {
229     g_shutting_down = true;
230     grpc_shutdown_internal_locked();
231   }
232 }
233
234 int grpc_is_initialized(void) {
235   int r;
236   gpr_once_init(&g_basic_init, do_basic_init);
237   grpc_core::MutexLock lock(&g_init_mu);
238   r = g_initializations > 0;
239   return r;
240 }
241
242 void grpc_maybe_wait_for_async_shutdown(void) {
243   gpr_once_init(&g_basic_init, do_basic_init);
244   grpc_core::MutexLock lock(&g_init_mu);
245   while (g_shutting_down) {
246     gpr_cv_wait(g_shutting_down_cv, &g_init_mu,
247                 gpr_inf_future(GPR_CLOCK_REALTIME));
248   }
249 }