21ab4e
From 49c87456f9b460c41f0564d86644a6a768aac3cc Mon Sep 17 00:00:00 2001
21ab4e
From: Jeff Darcy <jdarcy@fb.com>
21ab4e
Date: Fri, 30 Jun 2017 07:07:16 -0700
21ab4e
Subject: [PATCH 589/593] libglusterfs: add mem_pools_fini
21ab4e
21ab4e
This also makes mem_pools_init and mem_pools_fini re-callable, so GFAPI
21ab4e
can go through infinite init/fini cycles if they want to.  Not saying
21ab4e
that's a good idea, but at least it's safe.
21ab4e
21ab4e
Upstream reference :
21ab4e
>Change-Id: I617913410bcff54568b802cb653f48bdd533bd65
21ab4e
>Signed-off-by: Jeff Darcy <jdarcy@fb.com>
21ab4e
>Reviewed-on: https://review.gluster.org/17662
21ab4e
>Smoke: Gluster Build System <jenkins@build.gluster.org>
21ab4e
>Tested-by: Jeff Darcy <jeff@pl.atyp.us>
21ab4e
>Reviewed-by: Niels de Vos <ndevos@redhat.com>
21ab4e
>CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
21ab4e
>Reviewed-by: Jeff Darcy <jeff@pl.atyp.us>
21ab4e
21ab4e
Change-Id: I617913410bcff54568b802cb653f48bdd533bd65
21ab4e
BUG: 1477668
21ab4e
Signed-off-by: Jiffin Tony Thottan <jthottan@redhat.com>
21ab4e
Reviewed-on: https://code.engineering.redhat.com/gerrit/114349
21ab4e
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
21ab4e
---
21ab4e
 libglusterfs/src/mem-pool.c | 35 ++++++++++++++++++++++++++++++-----
21ab4e
 libglusterfs/src/mem-pool.h |  1 +
21ab4e
 2 files changed, 31 insertions(+), 5 deletions(-)
21ab4e
21ab4e
diff --git a/libglusterfs/src/mem-pool.c b/libglusterfs/src/mem-pool.c
21ab4e
index 9f5f762..3295bdc 100644
21ab4e
--- a/libglusterfs/src/mem-pool.c
21ab4e
+++ b/libglusterfs/src/mem-pool.c
21ab4e
@@ -462,6 +462,7 @@ pool_sweeper (void *arg)
21ab4e
 
21ab4e
         for (;;) {
21ab4e
                 sleep (POOL_SWEEP_SECS);
21ab4e
+                (void) pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, NULL);
21ab4e
                 INIT_LIST_HEAD (&state.death_row);
21ab4e
                 state.n_cold_lists = 0;
21ab4e
 
21ab4e
@@ -497,6 +498,7 @@ pool_sweeper (void *arg)
21ab4e
                 for (i = 0; i < state.n_cold_lists; ++i) {
21ab4e
                         free_obj_list (state.cold_lists[i]);
21ab4e
                 }
21ab4e
+                (void) pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
21ab4e
         }
21ab4e
 }
21ab4e
 
21ab4e
@@ -535,15 +537,38 @@ mem_pools_preinit (void)
21ab4e
 #endif
21ab4e
 }
21ab4e
 
21ab4e
+#if !defined(GF_DISABLE_MEMPOOL)
21ab4e
+static pthread_mutex_t  init_mutex      = PTHREAD_MUTEX_INITIALIZER;
21ab4e
+static unsigned int     init_count;
21ab4e
+static pthread_t        sweeper_tid;
21ab4e
+
21ab4e
+
21ab4e
 void
21ab4e
 mem_pools_init (void)
21ab4e
 {
21ab4e
-        pthread_t       kid;
21ab4e
-
21ab4e
-        (void) pthread_create (&kid, NULL, pool_sweeper, NULL);
21ab4e
-        (void) pthread_detach (kid);
21ab4e
+        pthread_mutex_lock (&init_mutex);
21ab4e
+        if ((init_count++) == 0) {
21ab4e
+                (void) pthread_create (&sweeper_tid, NULL, pool_sweeper, NULL);
21ab4e
+        }
21ab4e
+        pthread_mutex_unlock (&init_mutex);
21ab4e
 }
21ab4e
- 
21ab4e
+void
21ab4e
+mem_pools_fini (void)
21ab4e
+{
21ab4e
+        pthread_mutex_lock (&init_mutex);
21ab4e
+        GF_ASSERT (init_count > 0);
21ab4e
+        if ((--init_count) == 0) {
21ab4e
+                (void) pthread_cancel (sweeper_tid);
21ab4e
+                (void) pthread_join (sweeper_tid, NULL);
21ab4e
+        }
21ab4e
+        pthread_mutex_unlock (&init_mutex);
21ab4e
+}
21ab4e
+
21ab4e
+#else
21ab4e
+void mem_pools_init (void) {}
21ab4e
+void mem_pools_fini (void) {}
21ab4e
+#endif
21ab4e
+
21ab4e
 struct mem_pool *
21ab4e
 mem_pool_new_fn (unsigned long sizeof_type,
21ab4e
                  unsigned long count, char *name)
21ab4e
diff --git a/libglusterfs/src/mem-pool.h b/libglusterfs/src/mem-pool.h
21ab4e
index 1b27119..5a1bff5 100644
21ab4e
--- a/libglusterfs/src/mem-pool.h
21ab4e
+++ b/libglusterfs/src/mem-pool.h
21ab4e
@@ -257,6 +257,7 @@ struct mem_pool {
21ab4e
 };
21ab4e
 
21ab4e
 void mem_pools_init (void);
21ab4e
+void mem_pools_fini (void);
21ab4e
 
21ab4e
 struct mem_pool *
21ab4e
 mem_pool_new_fn (unsigned long sizeof_type, unsigned long count, char *name);
21ab4e
-- 
21ab4e
1.8.3.1
21ab4e