From ef6b991d63f6c170a1886d66a27f8387ef7818e6 Mon Sep 17 00:00:00 2001 From: David Garcia Quintas Date: Mon, 11 Jul 2016 22:18:59 -0700 Subject: [PATCH] Fix tsan race --- src/core/lib/iomgr/network_status_tracker.c | 27 +++++++++------------ 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/core/lib/iomgr/network_status_tracker.c b/src/core/lib/iomgr/network_status_tracker.c index 38a1c9b7d41..ccbe136db92 100644 --- a/src/core/lib/iomgr/network_status_tracker.c +++ b/src/core/lib/iomgr/network_status_tracker.c @@ -42,27 +42,24 @@ typedef struct endpoint_ll_node { static endpoint_ll_node *head = NULL; static gpr_mu g_endpoint_mutex; -static bool g_init_done = false; +static gpr_once g_once_init = GPR_ONCE_INIT; -void grpc_initialize_network_status_monitor() { - g_init_done = true; - gpr_mu_init(&g_endpoint_mutex); - // TODO(makarandd): Install callback with OS to monitor network status. -} - -void grpc_destroy_network_status_monitor() { - for (endpoint_ll_node *curr = head; curr != NULL;) { - endpoint_ll_node *next = curr->next; - gpr_free(curr); - curr = next; +static void destroy_network_status_monitor(void) { + if (head != NULL) { + gpr_log(GPR_ERROR, + "Memory leaked as all network endpoints were not shut down"); } gpr_mu_destroy(&g_endpoint_mutex); } +static void initialize_network_status_monitor(void) { + gpr_mu_init(&g_endpoint_mutex); + atexit(destroy_network_status_monitor); + // TODO(makarandd): Install callback with OS to monitor network status. +} + void grpc_network_status_register_endpoint(grpc_endpoint *ep) { - if (!g_init_done) { - grpc_initialize_network_status_monitor(); - } + gpr_once_init(&g_once_init, initialize_network_status_monitor); gpr_mu_lock(&g_endpoint_mutex); if (head == NULL) { head = (endpoint_ll_node *)gpr_malloc(sizeof(endpoint_ll_node));