diff options
Diffstat (limited to 'src/core/surface/init.c')
-rw-r--r-- | src/core/surface/init.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/core/surface/init.c b/src/core/surface/init.c index 442bc72f21..d9044549f2 100644 --- a/src/core/surface/init.c +++ b/src/core/surface/init.c @@ -33,8 +33,11 @@ #include <grpc/support/port_platform.h> +#include <memory.h> + #include <grpc/census.h> #include <grpc/grpc.h> +#include <grpc/support/alloc.h> #include <grpc/support/time.h> #include "src/core/channel/channel_stack.h" #include "src/core/client_config/resolver_registry.h" @@ -49,6 +52,8 @@ #include "src/core/transport/chttp2_transport.h" #include "src/core/transport/connectivity_state.h" +#define MAX_PLUGINS 128 + static gpr_once g_basic_init = GPR_ONCE_INIT; static gpr_mu g_init_mu; static int g_initializations; @@ -58,7 +63,23 @@ static void do_basic_init(void) { g_initializations = 0; } +typedef struct grpc_plugin { + void (*init)(); + void (*destroy)(); +} grpc_plugin; + +static grpc_plugin g_all_of_the_plugins[MAX_PLUGINS]; +static int g_number_of_plugins = 0; + +void grpc_register_plugin(void (*init)(void), void (*destroy)(void)) { + GPR_ASSERT(g_number_of_plugins != MAX_PLUGINS); + g_all_of_the_plugins[g_number_of_plugins].init = init; + g_all_of_the_plugins[g_number_of_plugins].destroy = destroy; + g_number_of_plugins++; +} + void grpc_init(void) { + int i; gpr_once_init(&g_basic_init, do_basic_init); gpr_mu_lock(&g_init_mu); @@ -87,11 +108,17 @@ void grpc_init(void) { } } grpc_timers_global_init(); + for (i = 0; i < g_number_of_plugins; i++) { + if (g_all_of_the_plugins[i].init != NULL) { + g_all_of_the_plugins[i].init(); + } + } } gpr_mu_unlock(&g_init_mu); } void grpc_shutdown(void) { + int i; gpr_mu_lock(&g_init_mu); if (--g_initializations == 0) { grpc_iomgr_shutdown(); @@ -99,6 +126,11 @@ void grpc_shutdown(void) { grpc_timers_global_destroy(); grpc_tracer_shutdown(); grpc_resolver_registry_shutdown(); + for (i = 0; i < g_number_of_plugins; i++) { + if (g_all_of_the_plugins[i].destroy != NULL) { + g_all_of_the_plugins[i].destroy(); + } + } } gpr_mu_unlock(&g_init_mu); } |