diff options
author | Craig Tiller <ctiller@google.com> | 2015-02-17 11:24:15 -0800 |
---|---|---|
committer | Craig Tiller <ctiller@google.com> | 2015-02-17 11:24:15 -0800 |
commit | 35108f65279cb46c2948dd5c8a57867f3206620d (patch) | |
tree | 996b4d0733664de993e1f32f651b2257263f1fee /src | |
parent | 44f77a3eb34e8d8f0802e87bf86bf62a29aeaf18 (diff) |
Allow grpc_init to be called multiple times
Diffstat (limited to 'src')
-rw-r--r-- | src/core/surface/init.c | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/src/core/surface/init.c b/src/core/surface/init.c index b5019eb03f..4d639fcbce 100644 --- a/src/core/surface/init.c +++ b/src/core/surface/init.c @@ -35,12 +35,32 @@ #include "src/core/statistics/census_interface.h" #include "src/core/iomgr/iomgr.h" +static gpr_once g_init = GPR_ONCE_INIT; +static gpr_mu g_init_mu; +static int g_initializations; + +static void do_init() { + gpr_mu_init(&g_init_mu); + g_initializations = 0; +} + void grpc_init(void) { - grpc_iomgr_init(); - census_init(); + gpr_once_init(&g_init, do_init); + + gpr_mu_lock(&g_init_mu); + if (++g_initializations == 1) { + grpc_iomgr_init(); + census_init(); + } + gpr_mu_unlock(&g_init_mu); } void grpc_shutdown(void) { - grpc_iomgr_shutdown(); - census_shutdown(); + gpr_mu_lock(&g_init_mu); + if (--g_initializations == 0) { + grpc_iomgr_shutdown(); + census_shutdown(); + } + gpr_mu_unlock(&g_init_mu); } + |