aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/surface
diff options
context:
space:
mode:
authorGravatar Craig Tiller <ctiller@google.com>2015-02-17 11:24:15 -0800
committerGravatar Craig Tiller <ctiller@google.com>2015-02-17 11:24:15 -0800
commit35108f65279cb46c2948dd5c8a57867f3206620d (patch)
tree996b4d0733664de993e1f32f651b2257263f1fee /src/core/surface
parent44f77a3eb34e8d8f0802e87bf86bf62a29aeaf18 (diff)
Allow grpc_init to be called multiple times
Diffstat (limited to 'src/core/surface')
-rw-r--r--src/core/surface/init.c28
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);
}
+