Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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/grpc.h> |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 35 | #include "src/core/iomgr/iomgr.h" |
Craig Tiller | 6e7c622 | 2015-02-20 15:31:21 -0800 | [diff] [blame] | 36 | #include "src/core/debug/trace.h" |
| 37 | #include "src/core/statistics/census_interface.h" |
Craig Tiller | faa8480 | 2015-03-01 21:56:38 -0800 | [diff] [blame] | 38 | #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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | |
Craig Tiller | 35108f6 | 2015-02-17 11:24:15 -0800 | [diff] [blame] | 43 | static gpr_once g_init = GPR_ONCE_INIT; |
| 44 | static gpr_mu g_init_mu; |
| 45 | static int g_initializations; |
| 46 | |
Nicolas "Pixel" Noble | 94964fd | 2015-02-21 07:19:19 +0100 | [diff] [blame] | 47 | static void do_init(void) { |
Craig Tiller | 35108f6 | 2015-02-17 11:24:15 -0800 | [diff] [blame] | 48 | gpr_mu_init(&g_init_mu); |
| 49 | g_initializations = 0; |
| 50 | } |
| 51 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 52 | void grpc_init(void) { |
Craig Tiller | 35108f6 | 2015-02-17 11:24:15 -0800 | [diff] [blame] | 53 | gpr_once_init(&g_init, do_init); |
| 54 | |
| 55 | gpr_mu_lock(&g_init_mu); |
| 56 | if (++g_initializations == 1) { |
Craig Tiller | faa8480 | 2015-03-01 21:56:38 -0800 | [diff] [blame] | 57 | 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 Tiller | 35108f6 | 2015-02-17 11:24:15 -0800 | [diff] [blame] | 62 | grpc_iomgr_init(); |
| 63 | census_init(); |
| 64 | } |
| 65 | gpr_mu_unlock(&g_init_mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 68 | void grpc_shutdown(void) { |
Craig Tiller | 35108f6 | 2015-02-17 11:24:15 -0800 | [diff] [blame] | 69 | 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 75 | } |
Craig Tiller | 60fd361 | 2015-03-05 16:24:22 -0800 | [diff] [blame^] | 76 | |
| 77 | int 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 | |