• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/http/parser.h"
37 #include "src/core/lib/iomgr/call_combiner.h"
38 #include "src/core/lib/iomgr/combiner.h"
39 #include "src/core/lib/iomgr/executor.h"
40 #include "src/core/lib/iomgr/iomgr.h"
41 #include "src/core/lib/iomgr/resource_quota.h"
42 #include "src/core/lib/iomgr/timer_manager.h"
43 #include "src/core/lib/profiling/timers.h"
44 #include "src/core/lib/slice/slice_internal.h"
45 #include "src/core/lib/surface/api_trace.h"
46 #include "src/core/lib/surface/call.h"
47 #include "src/core/lib/surface/channel_init.h"
48 #include "src/core/lib/surface/completion_queue.h"
49 #include "src/core/lib/surface/init.h"
50 #include "src/core/lib/surface/lame_client.h"
51 #include "src/core/lib/surface/server.h"
52 #include "src/core/lib/transport/bdp_estimator.h"
53 #include "src/core/lib/transport/connectivity_state.h"
54 #include "src/core/lib/transport/transport_impl.h"
55 
56 /* (generated) built in registry of plugins */
57 extern void grpc_register_built_in_plugins(void);
58 
59 #define MAX_PLUGINS 128
60 
61 static gpr_once g_basic_init = GPR_ONCE_INIT;
62 static gpr_mu g_init_mu;
63 static int g_initializations;
64 
do_basic_init(void)65 static void do_basic_init(void) {
66   gpr_log_verbosity_init();
67   gpr_mu_init(&g_init_mu);
68   grpc_register_built_in_plugins();
69   grpc_cq_global_init();
70   g_initializations = 0;
71 }
72 
append_filter(grpc_channel_stack_builder * builder,void * arg)73 static bool append_filter(grpc_channel_stack_builder* builder, void* arg) {
74   return grpc_channel_stack_builder_append_filter(
75       builder, static_cast<const grpc_channel_filter*>(arg), nullptr, nullptr);
76 }
77 
prepend_filter(grpc_channel_stack_builder * builder,void * arg)78 static bool prepend_filter(grpc_channel_stack_builder* builder, void* arg) {
79   return grpc_channel_stack_builder_prepend_filter(
80       builder, static_cast<const grpc_channel_filter*>(arg), nullptr, nullptr);
81 }
82 
register_builtin_channel_init()83 static void register_builtin_channel_init() {
84   grpc_channel_init_register_stage(GRPC_CLIENT_SUBCHANNEL,
85                                    GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
86                                    grpc_add_connected_filter, nullptr);
87   grpc_channel_init_register_stage(GRPC_CLIENT_DIRECT_CHANNEL,
88                                    GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
89                                    grpc_add_connected_filter, nullptr);
90   grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL,
91                                    GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
92                                    grpc_add_connected_filter, nullptr);
93   grpc_channel_init_register_stage(GRPC_CLIENT_LAME_CHANNEL,
94                                    GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
95                                    append_filter, (void*)&grpc_lame_filter);
96   grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, INT_MAX, prepend_filter,
97                                    (void*)&grpc_server_top_filter);
98 }
99 
100 typedef struct grpc_plugin {
101   void (*init)();
102   void (*destroy)();
103 } grpc_plugin;
104 
105 static grpc_plugin g_all_of_the_plugins[MAX_PLUGINS];
106 static int g_number_of_plugins = 0;
107 
grpc_register_plugin(void (* init)(void),void (* destroy)(void))108 void grpc_register_plugin(void (*init)(void), void (*destroy)(void)) {
109   GRPC_API_TRACE("grpc_register_plugin(init=%p, destroy=%p)", 2,
110                  ((void*)(intptr_t)init, (void*)(intptr_t)destroy));
111   GPR_ASSERT(g_number_of_plugins != MAX_PLUGINS);
112   g_all_of_the_plugins[g_number_of_plugins].init = init;
113   g_all_of_the_plugins[g_number_of_plugins].destroy = destroy;
114   g_number_of_plugins++;
115 }
116 
grpc_init(void)117 void grpc_init(void) {
118   int i;
119   gpr_once_init(&g_basic_init, do_basic_init);
120 
121   gpr_mu_lock(&g_init_mu);
122   if (++g_initializations == 1) {
123     grpc_core::Fork::GlobalInit();
124     grpc_fork_handlers_auto_register();
125     gpr_time_init();
126     grpc_stats_init();
127     grpc_slice_intern_init();
128     grpc_mdctx_global_init();
129     grpc_channel_init_init();
130     grpc_core::channelz::ChannelzRegistry::Init();
131     grpc_security_pre_init();
132     grpc_core::ExecCtx::GlobalInit();
133     grpc_iomgr_init();
134     gpr_timers_global_init();
135     grpc_handshaker_factory_registry_init();
136     grpc_security_init();
137     for (i = 0; i < g_number_of_plugins; i++) {
138       if (g_all_of_the_plugins[i].init != nullptr) {
139         g_all_of_the_plugins[i].init();
140       }
141     }
142     /* register channel finalization AFTER all plugins, to ensure that it's run
143      * at the appropriate time */
144     grpc_register_security_filters();
145     register_builtin_channel_init();
146     grpc_tracer_init("GRPC_TRACE");
147     /* no more changes to channel init pipelines */
148     grpc_channel_init_finalize();
149     grpc_iomgr_start();
150   }
151   gpr_mu_unlock(&g_init_mu);
152 
153   GRPC_API_TRACE("grpc_init(void)", 0, ());
154 }
155 
grpc_shutdown(void)156 void grpc_shutdown(void) {
157   int i;
158   GRPC_API_TRACE("grpc_shutdown(void)", 0, ());
159   gpr_mu_lock(&g_init_mu);
160   if (--g_initializations == 0) {
161     {
162       grpc_core::ExecCtx exec_ctx(0);
163       {
164         grpc_timer_manager_set_threading(
165             false);  // shutdown timer_manager thread
166         grpc_executor_shutdown();
167         for (i = g_number_of_plugins; i >= 0; i--) {
168           if (g_all_of_the_plugins[i].destroy != nullptr) {
169             g_all_of_the_plugins[i].destroy();
170           }
171         }
172       }
173       grpc_iomgr_shutdown();
174       gpr_timers_global_destroy();
175       grpc_tracer_shutdown();
176       grpc_mdctx_global_shutdown();
177       grpc_handshaker_factory_registry_shutdown();
178       grpc_slice_intern_shutdown();
179       grpc_core::channelz::ChannelzRegistry::Shutdown();
180       grpc_stats_shutdown();
181       grpc_core::Fork::GlobalShutdown();
182     }
183     grpc_core::ExecCtx::GlobalShutdown();
184   }
185   gpr_mu_unlock(&g_init_mu);
186 }
187 
grpc_is_initialized(void)188 int grpc_is_initialized(void) {
189   int r;
190   gpr_once_init(&g_basic_init, do_basic_init);
191   gpr_mu_lock(&g_init_mu);
192   r = g_initializations > 0;
193   gpr_mu_unlock(&g_init_mu);
194   return r;
195 }
196