Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Craig Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 19 | #include <grpc/support/port_platform.h> |
| 20 | |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 21 | #include <limits.h> |
Hongwei Wang | a3780a8 | 2015-07-17 15:27:18 -0700 | [diff] [blame] | 22 | #include <memory.h> |
| 23 | |
Nicolas "Pixel" Noble | 18a6837 | 2017-11-20 23:33:22 +0100 | [diff] [blame] | 24 | #include <grpc/fork.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 25 | #include <grpc/grpc.h> |
Hongwei Wang | a3780a8 | 2015-07-17 15:27:18 -0700 | [diff] [blame] | 26 | #include <grpc/support/alloc.h> |
Yuchen Zeng | 95e4c48 | 2016-04-15 10:38:24 -0700 | [diff] [blame] | 27 | #include <grpc/support/log.h> |
Craig Tiller | f3756c1 | 2015-07-01 17:21:01 -0700 | [diff] [blame] | 28 | #include <grpc/support/time.h> |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 29 | #include "src/core/lib/channel/channel_stack.h" |
ncteisen | bba8840 | 2018-05-11 11:54:41 -0400 | [diff] [blame] | 30 | #include "src/core/lib/channel/channelz_registry.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 31 | #include "src/core/lib/channel/connected_channel.h" |
Mark D. Roth | 1f0f23c | 2017-01-06 13:07:19 -0800 | [diff] [blame] | 32 | #include "src/core/lib/channel/handshaker_registry.h" |
Craig Tiller | 2808668 | 2017-07-18 14:22:19 -0700 | [diff] [blame] | 33 | #include "src/core/lib/debug/stats.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/debug/trace.h" |
kpayson64 | 701e70d | 2018-04-30 17:40:00 -0700 | [diff] [blame] | 35 | #include "src/core/lib/gprpp/fork.h" |
Craig Tiller | 3ab2fe0 | 2016-04-11 20:11:18 -0700 | [diff] [blame] | 36 | #include "src/core/lib/http/parser.h" |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 37 | #include "src/core/lib/iomgr/call_combiner.h" |
Craig Tiller | c3df7b4 | 2016-07-18 15:51:26 -0700 | [diff] [blame] | 38 | #include "src/core/lib/iomgr/combiner.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 39 | #include "src/core/lib/iomgr/executor.h" |
| 40 | #include "src/core/lib/iomgr/iomgr.h" |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 41 | #include "src/core/lib/iomgr/resource_quota.h" |
Juanli Shen | 6502ecc | 2017-09-13 13:10:54 -0700 | [diff] [blame] | 42 | #include "src/core/lib/iomgr/timer_manager.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 43 | #include "src/core/lib/profiling/timers.h" |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 44 | #include "src/core/lib/slice/slice_internal.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 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" |
Craig Tiller | efbd7c2 | 2017-01-27 14:07:44 -0800 | [diff] [blame] | 52 | #include "src/core/lib/transport/bdp_estimator.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 53 | #include "src/core/lib/transport/connectivity_state.h" |
| 54 | #include "src/core/lib/transport/transport_impl.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 55 | |
Craig Tiller | 3113ef4 | 2016-03-29 09:03:14 -0700 | [diff] [blame] | 56 | /* (generated) built in registry of plugins */ |
| 57 | extern void grpc_register_built_in_plugins(void); |
| 58 | |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 59 | #define MAX_PLUGINS 128 |
| 60 | |
Craig Tiller | 8ace0bc | 2015-03-05 17:16:09 -0800 | [diff] [blame] | 61 | static gpr_once g_basic_init = GPR_ONCE_INIT; |
Craig Tiller | 35108f6 | 2015-02-17 11:24:15 -0800 | [diff] [blame] | 62 | static gpr_mu g_init_mu; |
| 63 | static int g_initializations; |
| 64 | |
Craig Tiller | 8ace0bc | 2015-03-05 17:16:09 -0800 | [diff] [blame] | 65 | static void do_basic_init(void) { |
Yuchen Zeng | 95e4c48 | 2016-04-15 10:38:24 -0700 | [diff] [blame] | 66 | gpr_log_verbosity_init(); |
Craig Tiller | 35108f6 | 2015-02-17 11:24:15 -0800 | [diff] [blame] | 67 | gpr_mu_init(&g_init_mu); |
Craig Tiller | 3113ef4 | 2016-03-29 09:03:14 -0700 | [diff] [blame] | 68 | grpc_register_built_in_plugins(); |
Ken Payson | 42bd87e | 2017-10-20 10:32:30 -0700 | [diff] [blame] | 69 | grpc_cq_global_init(); |
Craig Tiller | 35108f6 | 2015-02-17 11:24:15 -0800 | [diff] [blame] | 70 | g_initializations = 0; |
| 71 | } |
| 72 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 73 | static bool append_filter(grpc_channel_stack_builder* builder, void* arg) { |
vjpai | e7077b5 | 2016-03-21 20:58:44 -0700 | [diff] [blame] | 74 | return grpc_channel_stack_builder_append_filter( |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 75 | builder, static_cast<const grpc_channel_filter*>(arg), nullptr, nullptr); |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 78 | static bool prepend_filter(grpc_channel_stack_builder* builder, void* arg) { |
vjpai | e7077b5 | 2016-03-21 20:58:44 -0700 | [diff] [blame] | 79 | return grpc_channel_stack_builder_prepend_filter( |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 80 | builder, static_cast<const grpc_channel_filter*>(arg), nullptr, nullptr); |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 83 | static void register_builtin_channel_init() { |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 84 | grpc_channel_init_register_stage(GRPC_CLIENT_SUBCHANNEL, |
| 85 | GRPC_CHANNEL_INIT_BUILTIN_PRIORITY, |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 86 | grpc_add_connected_filter, nullptr); |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 87 | grpc_channel_init_register_stage(GRPC_CLIENT_DIRECT_CHANNEL, |
| 88 | GRPC_CHANNEL_INIT_BUILTIN_PRIORITY, |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 89 | grpc_add_connected_filter, nullptr); |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 90 | grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, |
| 91 | GRPC_CHANNEL_INIT_BUILTIN_PRIORITY, |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 92 | grpc_add_connected_filter, nullptr); |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 93 | grpc_channel_init_register_stage(GRPC_CLIENT_LAME_CHANNEL, |
| 94 | GRPC_CHANNEL_INIT_BUILTIN_PRIORITY, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 95 | append_filter, (void*)&grpc_lame_filter); |
Craig Tiller | 8166732 | 2016-04-06 07:47:01 -0700 | [diff] [blame] | 96 | grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, INT_MAX, prepend_filter, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 97 | (void*)&grpc_server_top_filter); |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Hongwei Wang | a3780a8 | 2015-07-17 15:27:18 -0700 | [diff] [blame] | 100 | typedef struct grpc_plugin { |
| 101 | void (*init)(); |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 102 | void (*destroy)(); |
Hongwei Wang | a3780a8 | 2015-07-17 15:27:18 -0700 | [diff] [blame] | 103 | } grpc_plugin; |
| 104 | |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 105 | static grpc_plugin g_all_of_the_plugins[MAX_PLUGINS]; |
| 106 | static int g_number_of_plugins = 0; |
Hongwei Wang | a3780a8 | 2015-07-17 15:27:18 -0700 | [diff] [blame] | 107 | |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 108 | void grpc_register_plugin(void (*init)(void), void (*destroy)(void)) { |
Jan Tattermusch | 1b34aeb | 2015-12-17 10:37:10 -0800 | [diff] [blame] | 109 | GRPC_API_TRACE("grpc_register_plugin(init=%p, destroy=%p)", 2, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 110 | ((void*)(intptr_t)init, (void*)(intptr_t)destroy)); |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 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++; |
Hongwei Wang | ff6097a | 2015-08-12 17:48:56 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 117 | void grpc_init(void) { |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 118 | int i; |
Craig Tiller | 8ace0bc | 2015-03-05 17:16:09 -0800 | [diff] [blame] | 119 | gpr_once_init(&g_basic_init, do_basic_init); |
Craig Tiller | 35108f6 | 2015-02-17 11:24:15 -0800 | [diff] [blame] | 120 | |
| 121 | gpr_mu_lock(&g_init_mu); |
| 122 | if (++g_initializations == 1) { |
kpayson64 | 701e70d | 2018-04-30 17:40:00 -0700 | [diff] [blame] | 123 | grpc_core::Fork::GlobalInit(); |
kpayson64 | 38ab21e | 2018-03-08 15:47:40 -0800 | [diff] [blame] | 124 | grpc_fork_handlers_auto_register(); |
Craig Tiller | f3756c1 | 2015-07-01 17:21:01 -0700 | [diff] [blame] | 125 | gpr_time_init(); |
Craig Tiller | 2808668 | 2017-07-18 14:22:19 -0700 | [diff] [blame] | 126 | grpc_stats_init(); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 127 | grpc_slice_intern_init(); |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 128 | grpc_mdctx_global_init(); |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 129 | grpc_channel_init_init(); |
ncteisen | 9a6c722 | 2018-07-13 12:09:55 -0700 | [diff] [blame] | 130 | grpc_core::channelz::ChannelzRegistry::Init(); |
Craig Tiller | faa8480 | 2015-03-01 21:56:38 -0800 | [diff] [blame] | 131 | grpc_security_pre_init(); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 132 | grpc_core::ExecCtx::GlobalInit(); |
| 133 | grpc_iomgr_init(); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 134 | gpr_timers_global_init(); |
Mark D. Roth | 1f0f23c | 2017-01-06 13:07:19 -0800 | [diff] [blame] | 135 | grpc_handshaker_factory_registry_init(); |
| 136 | grpc_security_init(); |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 137 | for (i = 0; i < g_number_of_plugins; i++) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 138 | if (g_all_of_the_plugins[i].init != nullptr) { |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 139 | g_all_of_the_plugins[i].init(); |
Hongwei Wang | 35d5a0f | 2015-07-24 11:14:23 -0700 | [diff] [blame] | 140 | } |
Hongwei Wang | a3780a8 | 2015-07-17 15:27:18 -0700 | [diff] [blame] | 141 | } |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 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(); |
David Garcia Quintas | eb91310 | 2016-04-25 17:30:04 -0700 | [diff] [blame] | 146 | grpc_tracer_init("GRPC_TRACE"); |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 147 | /* no more changes to channel init pipelines */ |
| 148 | grpc_channel_init_finalize(); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 149 | grpc_iomgr_start(); |
Craig Tiller | 35108f6 | 2015-02-17 11:24:15 -0800 | [diff] [blame] | 150 | } |
| 151 | gpr_mu_unlock(&g_init_mu); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 152 | |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 153 | GRPC_API_TRACE("grpc_init(void)", 0, ()); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 156 | void grpc_shutdown(void) { |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 157 | int i; |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 158 | GRPC_API_TRACE("grpc_shutdown(void)", 0, ()); |
Craig Tiller | 35108f6 | 2015-02-17 11:24:15 -0800 | [diff] [blame] | 159 | gpr_mu_lock(&g_init_mu); |
| 160 | if (--g_initializations == 0) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 161 | { |
| 162 | grpc_core::ExecCtx exec_ctx(0); |
| 163 | { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 164 | grpc_timer_manager_set_threading( |
| 165 | false); // shutdown timer_manager thread |
yang-g | 8f65906 | 2018-01-31 15:16:41 -0800 | [diff] [blame] | 166 | grpc_executor_shutdown(); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 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 | } |
Hongwei Wang | 35d5a0f | 2015-07-24 11:14:23 -0700 | [diff] [blame] | 172 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 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(); |
ncteisen | 9a6c722 | 2018-07-13 12:09:55 -0700 | [diff] [blame] | 179 | grpc_core::channelz::ChannelzRegistry::Shutdown(); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 180 | grpc_stats_shutdown(); |
kpayson64 | 701e70d | 2018-04-30 17:40:00 -0700 | [diff] [blame] | 181 | grpc_core::Fork::GlobalShutdown(); |
Hongwei Wang | a3780a8 | 2015-07-17 15:27:18 -0700 | [diff] [blame] | 182 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 183 | grpc_core::ExecCtx::GlobalShutdown(); |
Craig Tiller | 35108f6 | 2015-02-17 11:24:15 -0800 | [diff] [blame] | 184 | } |
| 185 | gpr_mu_unlock(&g_init_mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 186 | } |
Craig Tiller | 60fd361 | 2015-03-05 16:24:22 -0800 | [diff] [blame] | 187 | |
| 188 | int grpc_is_initialized(void) { |
| 189 | int r; |
Craig Tiller | 8ace0bc | 2015-03-05 17:16:09 -0800 | [diff] [blame] | 190 | gpr_once_init(&g_basic_init, do_basic_init); |
Craig Tiller | 60fd361 | 2015-03-05 16:24:22 -0800 | [diff] [blame] | 191 | gpr_mu_lock(&g_init_mu); |
| 192 | r = g_initializations > 0; |
| 193 | gpr_mu_unlock(&g_init_mu); |
| 194 | return r; |
| 195 | } |