blob: 1f5c8d1b5d984a333d0edca6bc8c87644ba6e9e2 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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/grpc.h>
ctiller18b49ab2014-12-09 14:39:16 -080035#include "src/core/iomgr/iomgr.h"
Craig Tiller6e7c6222015-02-20 15:31:21 -080036#include "src/core/debug/trace.h"
37#include "src/core/statistics/census_interface.h"
Craig Tillerfaa84802015-03-01 21:56:38 -080038#include "src/core/channel/channel_stack.h"
39#include "src/core/surface/init.h"
40#include "src/core/surface/surface_trace.h"
41#include "src/core/transport/chttp2_transport.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042
Craig Tiller35108f62015-02-17 11:24:15 -080043static gpr_once g_init = GPR_ONCE_INIT;
44static gpr_mu g_init_mu;
45static int g_initializations;
46
Nicolas "Pixel" Noble94964fd2015-02-21 07:19:19 +010047static void do_init(void) {
Craig Tiller35108f62015-02-17 11:24:15 -080048 gpr_mu_init(&g_init_mu);
49 g_initializations = 0;
50}
51
Craig Tiller32946d32015-01-15 11:37:30 -080052void grpc_init(void) {
Craig Tiller35108f62015-02-17 11:24:15 -080053 gpr_once_init(&g_init, do_init);
54
55 gpr_mu_lock(&g_init_mu);
56 if (++g_initializations == 1) {
Craig Tillerfaa84802015-03-01 21:56:38 -080057 grpc_register_tracer("channel", &grpc_trace_channel);
58 grpc_register_tracer("surface", &grpc_surface_trace);
59 grpc_register_tracer("http", &grpc_http_trace);
60 grpc_security_pre_init();
61 grpc_tracer_init("GRPC_TRACE");
Craig Tiller35108f62015-02-17 11:24:15 -080062 grpc_iomgr_init();
63 census_init();
64 }
65 gpr_mu_unlock(&g_init_mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080066}
67
Craig Tiller32946d32015-01-15 11:37:30 -080068void grpc_shutdown(void) {
Craig Tiller35108f62015-02-17 11:24:15 -080069 gpr_mu_lock(&g_init_mu);
70 if (--g_initializations == 0) {
71 grpc_iomgr_shutdown();
72 census_shutdown();
73 }
74 gpr_mu_unlock(&g_init_mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080075}
Craig Tiller60fd3612015-03-05 16:24:22 -080076
77int grpc_is_initialized(void) {
78 int r;
79 gpr_once_init(&g_init, do_init);
80 gpr_mu_lock(&g_init_mu);
81 r = g_initializations > 0;
82 gpr_mu_unlock(&g_init_mu);
83 return r;
84}
85