LCOV - code coverage report
Current view: top level - core/surface - init.c (source / functions) Hit Total Coverage
Test: tmp.CaZ6RjdVn2 Lines: 68 69 98.6 %
Date: 2015-12-10 22:15:08 Functions: 5 5 100.0 %

          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/executor.h"
      51             : #include "src/core/iomgr/iomgr.h"
      52             : #include "src/core/profiling/timers.h"
      53             : #include "src/core/surface/api_trace.h"
      54             : #include "src/core/surface/call.h"
      55             : #include "src/core/surface/completion_queue.h"
      56             : #include "src/core/surface/init.h"
      57             : #include "src/core/surface/surface_trace.h"
      58             : #include "src/core/transport/chttp2_transport.h"
      59             : #include "src/core/transport/connectivity_state.h"
      60             : 
      61             : #ifndef GRPC_DEFAULT_NAME_PREFIX
      62             : #define GRPC_DEFAULT_NAME_PREFIX "dns:///"
      63             : #endif
      64             : 
      65             : #define MAX_PLUGINS 128
      66             : 
      67             : static gpr_once g_basic_init = GPR_ONCE_INIT;
      68             : static gpr_mu g_init_mu;
      69             : static int g_initializations;
      70             : 
      71         903 : static void do_basic_init(void) {
      72         903 :   gpr_mu_init(&g_init_mu);
      73         903 :   g_initializations = 0;
      74         903 : }
      75             : 
      76             : typedef struct grpc_plugin {
      77             :   void (*init)();
      78             :   void (*destroy)();
      79             : } grpc_plugin;
      80             : 
      81             : static grpc_plugin g_all_of_the_plugins[MAX_PLUGINS];
      82             : static int g_number_of_plugins = 0;
      83             : 
      84           1 : void grpc_register_plugin(void (*init)(void), void (*destroy)(void)) {
      85           1 :   GRPC_API_TRACE("grpc_register_plugin(init=%lx, destroy=%lx)", 2,
      86             :                  ((unsigned long)init, (unsigned long)destroy));
      87           1 :   GPR_ASSERT(g_number_of_plugins != MAX_PLUGINS);
      88           1 :   g_all_of_the_plugins[g_number_of_plugins].init = init;
      89           1 :   g_all_of_the_plugins[g_number_of_plugins].destroy = destroy;
      90           1 :   g_number_of_plugins++;
      91           1 : }
      92             : 
      93      425467 : void grpc_init(void) {
      94             :   int i;
      95      425467 :   gpr_once_init(&g_basic_init, do_basic_init);
      96             : 
      97      425415 :   gpr_mu_lock(&g_init_mu);
      98      425520 :   if (++g_initializations == 1) {
      99        3452 :     gpr_time_init();
     100        3452 :     grpc_mdctx_global_init();
     101        3452 :     grpc_lb_policy_registry_init(grpc_pick_first_lb_factory_create());
     102        3452 :     grpc_register_lb_policy(grpc_pick_first_lb_factory_create());
     103        3452 :     grpc_register_lb_policy(grpc_round_robin_lb_factory_create());
     104        3452 :     grpc_resolver_registry_init(GRPC_DEFAULT_NAME_PREFIX);
     105        3452 :     grpc_register_resolver_type(grpc_dns_resolver_factory_create());
     106        3452 :     grpc_register_resolver_type(grpc_ipv4_resolver_factory_create());
     107        3452 :     grpc_register_resolver_type(grpc_ipv6_resolver_factory_create());
     108             : #ifdef GPR_POSIX_SOCKET
     109        3452 :     grpc_register_resolver_type(grpc_unix_resolver_factory_create());
     110             : #endif
     111        3452 :     grpc_register_tracer("api", &grpc_api_trace);
     112        3452 :     grpc_register_tracer("channel", &grpc_trace_channel);
     113        3452 :     grpc_register_tracer("http", &grpc_http_trace);
     114        3452 :     grpc_register_tracer("flowctl", &grpc_flowctl_trace);
     115        3452 :     grpc_register_tracer("connectivity_state", &grpc_connectivity_state_trace);
     116        3452 :     grpc_security_pre_init();
     117        3452 :     grpc_iomgr_init();
     118        3452 :     grpc_executor_init();
     119        3452 :     grpc_tracer_init("GRPC_TRACE");
     120             :     /* Only initialize census if noone else has. */
     121        3452 :     if (census_enabled() == CENSUS_FEATURE_NONE) {
     122        3452 :       if (census_initialize(census_supported())) { /* enable all features. */
     123           0 :         gpr_log(GPR_ERROR, "Could not initialize census.");
     124             :       }
     125             :     }
     126        3452 :     gpr_timers_global_init();
     127        3452 :     grpc_cq_global_init();
     128        3453 :     for (i = 0; i < g_number_of_plugins; i++) {
     129           1 :       if (g_all_of_the_plugins[i].init != NULL) {
     130           1 :         g_all_of_the_plugins[i].init();
     131             :       }
     132             :     }
     133             :   }
     134      425520 :   gpr_mu_unlock(&g_init_mu);
     135      425511 :   GRPC_API_TRACE("grpc_init(void)", 0, ());
     136      425511 : }
     137             : 
     138      425496 : void grpc_shutdown(void) {
     139             :   int i;
     140      425496 :   GRPC_API_TRACE("grpc_shutdown(void)", 0, ());
     141      425496 :   gpr_mu_lock(&g_init_mu);
     142      425515 :   if (--g_initializations == 0) {
     143        3450 :     grpc_executor_shutdown();
     144        3450 :     grpc_cq_global_shutdown();
     145        3450 :     grpc_iomgr_shutdown();
     146        3450 :     census_shutdown();
     147        3450 :     gpr_timers_global_destroy();
     148        3450 :     grpc_tracer_shutdown();
     149        3450 :     grpc_resolver_registry_shutdown();
     150        3450 :     grpc_lb_policy_registry_shutdown();
     151        3451 :     for (i = 0; i < g_number_of_plugins; i++) {
     152           1 :       if (g_all_of_the_plugins[i].destroy != NULL) {
     153           1 :         g_all_of_the_plugins[i].destroy();
     154             :       }
     155             :     }
     156        3450 :     grpc_mdctx_global_shutdown();
     157             :   }
     158      425515 :   gpr_mu_unlock(&g_init_mu);
     159      425515 : }
     160             : 
     161       10246 : int grpc_is_initialized(void) {
     162             :   int r;
     163       10246 :   gpr_once_init(&g_basic_init, do_basic_init);
     164       10246 :   gpr_mu_lock(&g_init_mu);
     165       10246 :   r = g_initializations > 0;
     166       10246 :   gpr_mu_unlock(&g_init_mu);
     167       10246 :   return r;
     168             : }

Generated by: LCOV version 1.11