OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 #include <grpc/support/port_platform.h> |
| 35 |
| 36 #include <memory.h> |
| 37 |
| 38 #include <grpc/census.h> |
| 39 #include <grpc/grpc.h> |
| 40 #include <grpc/support/alloc.h> |
| 41 #include <grpc/support/time.h> |
| 42 #include "src/core/channel/channel_stack.h" |
| 43 #include "src/core/client_config/lb_policy_registry.h" |
| 44 #include "src/core/client_config/lb_policies/pick_first.h" |
| 45 #include "src/core/client_config/lb_policies/round_robin.h" |
| 46 #include "src/core/client_config/resolver_registry.h" |
| 47 #include "src/core/client_config/resolvers/dns_resolver.h" |
| 48 #include "src/core/client_config/resolvers/sockaddr_resolver.h" |
| 49 #include "src/core/client_config/subchannel.h" |
| 50 #include "src/core/client_config/subchannel_index.h" |
| 51 #include "src/core/debug/trace.h" |
| 52 #include "src/core/iomgr/executor.h" |
| 53 #include "src/core/iomgr/iomgr.h" |
| 54 #include "src/core/profiling/timers.h" |
| 55 #include "src/core/surface/api_trace.h" |
| 56 #include "src/core/surface/call.h" |
| 57 #include "src/core/surface/completion_queue.h" |
| 58 #include "src/core/surface/init.h" |
| 59 #include "src/core/surface/surface_trace.h" |
| 60 #include "src/core/transport/chttp2_transport.h" |
| 61 #include "src/core/transport/connectivity_state.h" |
| 62 |
| 63 #ifndef GRPC_DEFAULT_NAME_PREFIX |
| 64 #define GRPC_DEFAULT_NAME_PREFIX "dns:///" |
| 65 #endif |
| 66 |
| 67 #define MAX_PLUGINS 128 |
| 68 |
| 69 static gpr_once g_basic_init = GPR_ONCE_INIT; |
| 70 static gpr_mu g_init_mu; |
| 71 static int g_initializations; |
| 72 |
| 73 static void do_basic_init(void) { |
| 74 gpr_mu_init(&g_init_mu); |
| 75 g_initializations = 0; |
| 76 } |
| 77 |
| 78 typedef struct grpc_plugin { |
| 79 void (*init)(); |
| 80 void (*destroy)(); |
| 81 } grpc_plugin; |
| 82 |
| 83 static grpc_plugin g_all_of_the_plugins[MAX_PLUGINS]; |
| 84 static int g_number_of_plugins = 0; |
| 85 |
| 86 void grpc_register_plugin(void (*init)(void), void (*destroy)(void)) { |
| 87 GRPC_API_TRACE("grpc_register_plugin(init=%p, destroy=%p)", 2, |
| 88 ((void*)(intptr_t)init, (void*)(intptr_t)destroy)); |
| 89 GPR_ASSERT(g_number_of_plugins != MAX_PLUGINS); |
| 90 g_all_of_the_plugins[g_number_of_plugins].init = init; |
| 91 g_all_of_the_plugins[g_number_of_plugins].destroy = destroy; |
| 92 g_number_of_plugins++; |
| 93 } |
| 94 |
| 95 void grpc_init(void) { |
| 96 int i; |
| 97 gpr_once_init(&g_basic_init, do_basic_init); |
| 98 |
| 99 gpr_mu_lock(&g_init_mu); |
| 100 if (++g_initializations == 1) { |
| 101 gpr_time_init(); |
| 102 grpc_mdctx_global_init(); |
| 103 grpc_lb_policy_registry_init(grpc_pick_first_lb_factory_create()); |
| 104 grpc_register_lb_policy(grpc_pick_first_lb_factory_create()); |
| 105 grpc_register_lb_policy(grpc_round_robin_lb_factory_create()); |
| 106 grpc_resolver_registry_init(GRPC_DEFAULT_NAME_PREFIX); |
| 107 grpc_register_resolver_type(grpc_dns_resolver_factory_create()); |
| 108 grpc_register_resolver_type(grpc_ipv4_resolver_factory_create()); |
| 109 grpc_register_resolver_type(grpc_ipv6_resolver_factory_create()); |
| 110 #ifdef GPR_POSIX_SOCKET |
| 111 grpc_register_resolver_type(grpc_unix_resolver_factory_create()); |
| 112 #endif |
| 113 grpc_register_tracer("api", &grpc_api_trace); |
| 114 grpc_register_tracer("channel", &grpc_trace_channel); |
| 115 grpc_register_tracer("http", &grpc_http_trace); |
| 116 grpc_register_tracer("flowctl", &grpc_flowctl_trace); |
| 117 grpc_register_tracer("connectivity_state", &grpc_connectivity_state_trace); |
| 118 grpc_security_pre_init(); |
| 119 grpc_iomgr_init(); |
| 120 grpc_executor_init(); |
| 121 grpc_tracer_init("GRPC_TRACE"); |
| 122 /* Only initialize census if no one else has and some features are |
| 123 * available. */ |
| 124 if (census_enabled() == CENSUS_FEATURE_NONE && |
| 125 census_supported() != CENSUS_FEATURE_NONE) { |
| 126 if (census_initialize(census_supported())) { /* enable all features. */ |
| 127 gpr_log(GPR_ERROR, "Could not initialize census."); |
| 128 } |
| 129 } |
| 130 gpr_timers_global_init(); |
| 131 grpc_cq_global_init(); |
| 132 grpc_subchannel_index_init(); |
| 133 for (i = 0; i < g_number_of_plugins; i++) { |
| 134 if (g_all_of_the_plugins[i].init != NULL) { |
| 135 g_all_of_the_plugins[i].init(); |
| 136 } |
| 137 } |
| 138 } |
| 139 gpr_mu_unlock(&g_init_mu); |
| 140 GRPC_API_TRACE("grpc_init(void)", 0, ()); |
| 141 } |
| 142 |
| 143 void grpc_shutdown(void) { |
| 144 int i; |
| 145 GRPC_API_TRACE("grpc_shutdown(void)", 0, ()); |
| 146 gpr_mu_lock(&g_init_mu); |
| 147 if (--g_initializations == 0) { |
| 148 grpc_executor_shutdown(); |
| 149 grpc_cq_global_shutdown(); |
| 150 grpc_iomgr_shutdown(); |
| 151 grpc_subchannel_index_shutdown(); |
| 152 census_shutdown(); |
| 153 gpr_timers_global_destroy(); |
| 154 grpc_tracer_shutdown(); |
| 155 grpc_resolver_registry_shutdown(); |
| 156 grpc_lb_policy_registry_shutdown(); |
| 157 for (i = 0; i < g_number_of_plugins; i++) { |
| 158 if (g_all_of_the_plugins[i].destroy != NULL) { |
| 159 g_all_of_the_plugins[i].destroy(); |
| 160 } |
| 161 } |
| 162 grpc_mdctx_global_shutdown(); |
| 163 } |
| 164 gpr_mu_unlock(&g_init_mu); |
| 165 } |
| 166 |
| 167 int grpc_is_initialized(void) { |
| 168 int r; |
| 169 gpr_once_init(&g_basic_init, do_basic_init); |
| 170 gpr_mu_lock(&g_init_mu); |
| 171 r = g_initializations > 0; |
| 172 gpr_mu_unlock(&g_init_mu); |
| 173 return r; |
| 174 } |
OLD | NEW |