Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, 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/debug/trace.h"
50 : #include "src/core/iomgr/iomgr.h"
51 : #include "src/core/profiling/timers.h"
52 : #include "src/core/surface/api_trace.h"
53 : #include "src/core/surface/call.h"
54 : #include "src/core/surface/init.h"
55 : #include "src/core/surface/surface_trace.h"
56 : #include "src/core/transport/chttp2_transport.h"
57 : #include "src/core/transport/connectivity_state.h"
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 :
65 753 : static void do_basic_init(void) {
66 753 : gpr_mu_init(&g_init_mu);
67 753 : g_initializations = 0;
68 753 : }
69 :
70 : typedef struct grpc_plugin {
71 : void (*init)();
72 : void (*destroy)();
73 : } grpc_plugin;
74 :
75 : static grpc_plugin g_all_of_the_plugins[MAX_PLUGINS];
76 : static int g_number_of_plugins = 0;
77 :
78 0 : void grpc_register_plugin(void (*init)(void), void (*destroy)(void)) {
79 0 : GRPC_API_TRACE("grpc_register_plugin(init=%lx, destroy=%lx)", 2,
80 : ((unsigned long)init, (unsigned long)destroy));
81 0 : GPR_ASSERT(g_number_of_plugins != MAX_PLUGINS);
82 0 : g_all_of_the_plugins[g_number_of_plugins].init = init;
83 0 : g_all_of_the_plugins[g_number_of_plugins].destroy = destroy;
84 0 : g_number_of_plugins++;
85 0 : }
86 :
87 323731 : void grpc_init(void) {
88 : int i;
89 323731 : gpr_once_init(&g_basic_init, do_basic_init);
90 :
91 323736 : gpr_mu_lock(&g_init_mu);
92 323773 : if (++g_initializations == 1) {
93 2501 : gpr_time_init();
94 2501 : grpc_lb_policy_registry_init(grpc_pick_first_lb_factory_create());
95 2501 : grpc_register_lb_policy(grpc_pick_first_lb_factory_create());
96 2501 : grpc_register_lb_policy(grpc_round_robin_lb_factory_create());
97 2501 : grpc_resolver_registry_init("dns:///");
98 2501 : grpc_register_resolver_type(grpc_dns_resolver_factory_create());
99 2501 : grpc_register_resolver_type(grpc_ipv4_resolver_factory_create());
100 2501 : grpc_register_resolver_type(grpc_ipv6_resolver_factory_create());
101 : #ifdef GPR_POSIX_SOCKET
102 2501 : grpc_register_resolver_type(grpc_unix_resolver_factory_create());
103 : #endif
104 2501 : grpc_register_tracer("api", &grpc_api_trace);
105 2501 : grpc_register_tracer("channel", &grpc_trace_channel);
106 2501 : grpc_register_tracer("http", &grpc_http_trace);
107 2501 : grpc_register_tracer("flowctl", &grpc_flowctl_trace);
108 2501 : grpc_register_tracer("connectivity_state", &grpc_connectivity_state_trace);
109 2501 : grpc_security_pre_init();
110 2501 : grpc_iomgr_init();
111 2501 : grpc_tracer_init("GRPC_TRACE");
112 : /* Only initialize census if noone else has. */
113 2501 : if (census_enabled() == CENSUS_FEATURE_NONE) {
114 2501 : if (census_initialize(census_supported())) { /* enable all features. */
115 0 : gpr_log(GPR_ERROR, "Could not initialize census.");
116 : }
117 : }
118 2501 : grpc_timers_global_init();
119 2501 : for (i = 0; i < g_number_of_plugins; i++) {
120 0 : if (g_all_of_the_plugins[i].init != NULL) {
121 0 : g_all_of_the_plugins[i].init();
122 : }
123 : }
124 : }
125 323773 : gpr_mu_unlock(&g_init_mu);
126 323773 : GRPC_API_TRACE("grpc_init(void)", 0, ());
127 323773 : }
128 :
129 323756 : void grpc_shutdown(void) {
130 : int i;
131 323756 : GRPC_API_TRACE("grpc_shutdown(void)", 0, ());
132 323756 : gpr_mu_lock(&g_init_mu);
133 323773 : if (--g_initializations == 0) {
134 2501 : grpc_iomgr_shutdown();
135 2501 : census_shutdown();
136 2501 : grpc_timers_global_destroy();
137 2501 : grpc_tracer_shutdown();
138 2501 : grpc_resolver_registry_shutdown();
139 2501 : for (i = 0; i < g_number_of_plugins; i++) {
140 0 : if (g_all_of_the_plugins[i].destroy != NULL) {
141 0 : g_all_of_the_plugins[i].destroy();
142 : }
143 : }
144 : }
145 323773 : gpr_mu_unlock(&g_init_mu);
146 323772 : }
147 :
148 6490 : int grpc_is_initialized(void) {
149 : int r;
150 6490 : gpr_once_init(&g_basic_init, do_basic_init);
151 6490 : gpr_mu_lock(&g_init_mu);
152 6490 : r = g_initializations > 0;
153 6490 : gpr_mu_unlock(&g_init_mu);
154 6490 : return r;
155 : }
|