blob: 52e0ee1c443e1ccc393f85d2b5219b15721fd5ae [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
Craig Tilleracf0f072015-06-29 08:24:16 -070019#include <grpc/support/port_platform.h>
20
Craig Tiller178edfa2016-02-17 20:54:46 -080021#include <limits.h>
Hongwei Wanga3780a82015-07-17 15:27:18 -070022#include <memory.h>
23
Nicolas "Pixel" Noble18a68372017-11-20 23:33:22 +010024#include <grpc/fork.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080025#include <grpc/grpc.h>
Hongwei Wanga3780a82015-07-17 15:27:18 -070026#include <grpc/support/alloc.h>
Yuchen Zeng95e4c482016-04-15 10:38:24 -070027#include <grpc/support/log.h>
Craig Tillerf3756c12015-07-01 17:21:01 -070028#include <grpc/support/time.h>
Craig Tiller9533d042016-03-25 17:11:06 -070029#include "src/core/lib/channel/channel_stack.h"
ncteisen3b42f832018-03-19 13:22:35 -070030#include "src/core/lib/channel/channel_trace_registry.h"
Craig Tiller9533d042016-03-25 17:11:06 -070031#include "src/core/lib/channel/connected_channel.h"
Mark D. Roth1f0f23c2017-01-06 13:07:19 -080032#include "src/core/lib/channel/handshaker_registry.h"
Craig Tiller28086682017-07-18 14:22:19 -070033#include "src/core/lib/debug/stats.h"
Craig Tiller9533d042016-03-25 17:11:06 -070034#include "src/core/lib/debug/trace.h"
Mark D. Rothdbdf4952018-01-18 11:21:12 -080035#include "src/core/lib/gpr/fork.h"
Vijay Paida693552018-02-16 22:59:03 -080036#include "src/core/lib/gprpp/thd.h"
Craig Tiller3ab2fe02016-04-11 20:11:18 -070037#include "src/core/lib/http/parser.h"
Mark D. Roth764cf042017-09-01 09:00:06 -070038#include "src/core/lib/iomgr/call_combiner.h"
Craig Tillerc3df7b42016-07-18 15:51:26 -070039#include "src/core/lib/iomgr/combiner.h"
Craig Tiller9533d042016-03-25 17:11:06 -070040#include "src/core/lib/iomgr/executor.h"
41#include "src/core/lib/iomgr/iomgr.h"
Craig Tiller20afa3d2016-10-17 14:52:14 -070042#include "src/core/lib/iomgr/resource_quota.h"
Juanli Shen6502ecc2017-09-13 13:10:54 -070043#include "src/core/lib/iomgr/timer_manager.h"
Craig Tiller9533d042016-03-25 17:11:06 -070044#include "src/core/lib/profiling/timers.h"
Craig Tiller7c70b6c2017-01-23 07:48:42 -080045#include "src/core/lib/slice/slice_internal.h"
Craig Tiller9533d042016-03-25 17:11:06 -070046#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"
Craig Tillerefbd7c22017-01-27 14:07:44 -080053#include "src/core/lib/transport/bdp_estimator.h"
Craig Tiller9533d042016-03-25 17:11:06 -070054#include "src/core/lib/transport/connectivity_state.h"
55#include "src/core/lib/transport/transport_impl.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080056
Craig Tiller3113ef42016-03-29 09:03:14 -070057/* (generated) built in registry of plugins */
58extern void grpc_register_built_in_plugins(void);
59
Hongwei Wang85ad6852015-08-13 16:13:10 -070060#define MAX_PLUGINS 128
61
Craig Tiller8ace0bc2015-03-05 17:16:09 -080062static gpr_once g_basic_init = GPR_ONCE_INIT;
Craig Tiller35108f62015-02-17 11:24:15 -080063static gpr_mu g_init_mu;
64static int g_initializations;
65
Craig Tiller8ace0bc2015-03-05 17:16:09 -080066static void do_basic_init(void) {
Yuchen Zeng95e4c482016-04-15 10:38:24 -070067 gpr_log_verbosity_init();
Nicolas "Pixel" Noble18a68372017-11-20 23:33:22 +010068 grpc_fork_support_init();
Craig Tiller35108f62015-02-17 11:24:15 -080069 gpr_mu_init(&g_init_mu);
Craig Tiller3113ef42016-03-29 09:03:14 -070070 grpc_register_built_in_plugins();
Ken Payson42bd87e2017-10-20 10:32:30 -070071 grpc_cq_global_init();
Craig Tiller35108f62015-02-17 11:24:15 -080072 g_initializations = 0;
Nicolas "Pixel" Noble18a68372017-11-20 23:33:22 +010073 grpc_fork_handlers_auto_register();
Craig Tiller35108f62015-02-17 11:24:15 -080074}
75
Yash Tibrewal8cf14702017-12-06 09:47:54 -080076static bool append_filter(grpc_channel_stack_builder* builder, void* arg) {
vjpaie7077b52016-03-21 20:58:44 -070077 return grpc_channel_stack_builder_append_filter(
Noah Eisenbe82e642018-02-09 09:16:55 -080078 builder, static_cast<const grpc_channel_filter*>(arg), nullptr, nullptr);
Craig Tiller178edfa2016-02-17 20:54:46 -080079}
80
Yash Tibrewal8cf14702017-12-06 09:47:54 -080081static bool prepend_filter(grpc_channel_stack_builder* builder, void* arg) {
vjpaie7077b52016-03-21 20:58:44 -070082 return grpc_channel_stack_builder_prepend_filter(
Noah Eisenbe82e642018-02-09 09:16:55 -080083 builder, static_cast<const grpc_channel_filter*>(arg), nullptr, nullptr);
Craig Tiller178edfa2016-02-17 20:54:46 -080084}
85
Craig Tiller178edfa2016-02-17 20:54:46 -080086static void register_builtin_channel_init() {
Craig Tillerf82ddc42016-04-05 17:15:07 -070087 grpc_channel_init_register_stage(GRPC_CLIENT_SUBCHANNEL,
88 GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
Craig Tiller4782d922017-11-10 09:53:21 -080089 grpc_add_connected_filter, nullptr);
Craig Tillerf82ddc42016-04-05 17:15:07 -070090 grpc_channel_init_register_stage(GRPC_CLIENT_DIRECT_CHANNEL,
91 GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
Craig Tiller4782d922017-11-10 09:53:21 -080092 grpc_add_connected_filter, nullptr);
Craig Tillerf82ddc42016-04-05 17:15:07 -070093 grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL,
94 GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
Craig Tiller4782d922017-11-10 09:53:21 -080095 grpc_add_connected_filter, nullptr);
Craig Tillerf82ddc42016-04-05 17:15:07 -070096 grpc_channel_init_register_stage(GRPC_CLIENT_LAME_CHANNEL,
97 GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
Craig Tillerbaa14a92017-11-03 09:09:36 -070098 append_filter, (void*)&grpc_lame_filter);
Craig Tiller81667322016-04-06 07:47:01 -070099 grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, INT_MAX, prepend_filter,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700100 (void*)&grpc_server_top_filter);
Craig Tiller178edfa2016-02-17 20:54:46 -0800101}
102
Hongwei Wanga3780a82015-07-17 15:27:18 -0700103typedef struct grpc_plugin {
104 void (*init)();
Hongwei Wang85ad6852015-08-13 16:13:10 -0700105 void (*destroy)();
Hongwei Wanga3780a82015-07-17 15:27:18 -0700106} grpc_plugin;
107
Hongwei Wang85ad6852015-08-13 16:13:10 -0700108static grpc_plugin g_all_of_the_plugins[MAX_PLUGINS];
109static int g_number_of_plugins = 0;
Hongwei Wanga3780a82015-07-17 15:27:18 -0700110
Hongwei Wang85ad6852015-08-13 16:13:10 -0700111void grpc_register_plugin(void (*init)(void), void (*destroy)(void)) {
Jan Tattermusch1b34aeb2015-12-17 10:37:10 -0800112 GRPC_API_TRACE("grpc_register_plugin(init=%p, destroy=%p)", 2,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700113 ((void*)(intptr_t)init, (void*)(intptr_t)destroy));
Hongwei Wang85ad6852015-08-13 16:13:10 -0700114 GPR_ASSERT(g_number_of_plugins != MAX_PLUGINS);
115 g_all_of_the_plugins[g_number_of_plugins].init = init;
116 g_all_of_the_plugins[g_number_of_plugins].destroy = destroy;
117 g_number_of_plugins++;
Hongwei Wangff6097a2015-08-12 17:48:56 -0700118}
119
Craig Tiller32946d32015-01-15 11:37:30 -0800120void grpc_init(void) {
Hongwei Wang85ad6852015-08-13 16:13:10 -0700121 int i;
Craig Tiller8ace0bc2015-03-05 17:16:09 -0800122 gpr_once_init(&g_basic_init, do_basic_init);
Craig Tiller35108f62015-02-17 11:24:15 -0800123
124 gpr_mu_lock(&g_init_mu);
125 if (++g_initializations == 1) {
Craig Tillerf3756c12015-07-01 17:21:01 -0700126 gpr_time_init();
Vijay Paida693552018-02-16 22:59:03 -0800127 grpc_core::Thread::Init();
Craig Tiller28086682017-07-18 14:22:19 -0700128 grpc_stats_init();
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800129 grpc_slice_intern_init();
Craig Tiller0e72ede2015-11-19 07:48:53 -0800130 grpc_mdctx_global_init();
Craig Tiller178edfa2016-02-17 20:54:46 -0800131 grpc_channel_init_init();
ncteisen3b42f832018-03-19 13:22:35 -0700132 grpc_channel_trace_registry_init();
Craig Tillerfaa84802015-03-01 21:56:38 -0800133 grpc_security_pre_init();
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800134 grpc_core::ExecCtx::GlobalInit();
135 grpc_iomgr_init();
Craig Tiller0ba432d2015-10-09 16:57:11 -0700136 gpr_timers_global_init();
Mark D. Roth1f0f23c2017-01-06 13:07:19 -0800137 grpc_handshaker_factory_registry_init();
138 grpc_security_init();
Hongwei Wang85ad6852015-08-13 16:13:10 -0700139 for (i = 0; i < g_number_of_plugins; i++) {
Craig Tiller4782d922017-11-10 09:53:21 -0800140 if (g_all_of_the_plugins[i].init != nullptr) {
Hongwei Wang85ad6852015-08-13 16:13:10 -0700141 g_all_of_the_plugins[i].init();
Hongwei Wang35d5a0f2015-07-24 11:14:23 -0700142 }
Hongwei Wanga3780a82015-07-17 15:27:18 -0700143 }
Craig Tiller178edfa2016-02-17 20:54:46 -0800144 /* register channel finalization AFTER all plugins, to ensure that it's run
145 * at the appropriate time */
146 grpc_register_security_filters();
147 register_builtin_channel_init();
David Garcia Quintaseb913102016-04-25 17:30:04 -0700148 grpc_tracer_init("GRPC_TRACE");
Craig Tiller178edfa2016-02-17 20:54:46 -0800149 /* no more changes to channel init pipelines */
150 grpc_channel_init_finalize();
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800151 grpc_iomgr_start();
Craig Tiller35108f62015-02-17 11:24:15 -0800152 }
153 gpr_mu_unlock(&g_init_mu);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800154
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700155 GRPC_API_TRACE("grpc_init(void)", 0, ());
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800156}
157
Craig Tiller32946d32015-01-15 11:37:30 -0800158void grpc_shutdown(void) {
Hongwei Wang85ad6852015-08-13 16:13:10 -0700159 int i;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700160 GRPC_API_TRACE("grpc_shutdown(void)", 0, ());
Craig Tiller35108f62015-02-17 11:24:15 -0800161 gpr_mu_lock(&g_init_mu);
162 if (--g_initializations == 0) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800163 {
164 grpc_core::ExecCtx exec_ctx(0);
165 {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800166 grpc_timer_manager_set_threading(
167 false); // shutdown timer_manager thread
yang-g8f659062018-01-31 15:16:41 -0800168 grpc_executor_shutdown();
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800169 for (i = g_number_of_plugins; i >= 0; i--) {
170 if (g_all_of_the_plugins[i].destroy != nullptr) {
171 g_all_of_the_plugins[i].destroy();
172 }
173 }
Hongwei Wang35d5a0f2015-07-24 11:14:23 -0700174 }
jiangtaoli2016144f5552018-03-23 11:28:48 -0700175 grpc_security_shutdown();
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800176 grpc_iomgr_shutdown();
177 gpr_timers_global_destroy();
178 grpc_tracer_shutdown();
179 grpc_mdctx_global_shutdown();
180 grpc_handshaker_factory_registry_shutdown();
181 grpc_slice_intern_shutdown();
ncteisen3b42f832018-03-19 13:22:35 -0700182 grpc_channel_trace_registry_shutdown();
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800183 grpc_stats_shutdown();
Hongwei Wanga3780a82015-07-17 15:27:18 -0700184 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800185 grpc_core::ExecCtx::GlobalShutdown();
Craig Tiller35108f62015-02-17 11:24:15 -0800186 }
187 gpr_mu_unlock(&g_init_mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800188}
Craig Tiller60fd3612015-03-05 16:24:22 -0800189
190int grpc_is_initialized(void) {
191 int r;
Craig Tiller8ace0bc2015-03-05 17:16:09 -0800192 gpr_once_init(&g_basic_init, do_basic_init);
Craig Tiller60fd3612015-03-05 16:24:22 -0800193 gpr_mu_lock(&g_init_mu);
194 r = g_initializations > 0;
195 gpr_mu_unlock(&g_init_mu);
196 return r;
197}