Blob Blame History Raw
From 49c87456f9b460c41f0564d86644a6a768aac3cc Mon Sep 17 00:00:00 2001
From: Jeff Darcy <jdarcy@fb.com>
Date: Fri, 30 Jun 2017 07:07:16 -0700
Subject: [PATCH 589/593] libglusterfs: add mem_pools_fini

This also makes mem_pools_init and mem_pools_fini re-callable, so GFAPI
can go through infinite init/fini cycles if they want to.  Not saying
that's a good idea, but at least it's safe.

Upstream reference :
>Change-Id: I617913410bcff54568b802cb653f48bdd533bd65
>Signed-off-by: Jeff Darcy <jdarcy@fb.com>
>Reviewed-on: https://review.gluster.org/17662
>Smoke: Gluster Build System <jenkins@build.gluster.org>
>Tested-by: Jeff Darcy <jeff@pl.atyp.us>
>Reviewed-by: Niels de Vos <ndevos@redhat.com>
>CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
>Reviewed-by: Jeff Darcy <jeff@pl.atyp.us>

Change-Id: I617913410bcff54568b802cb653f48bdd533bd65
BUG: 1477668
Signed-off-by: Jiffin Tony Thottan <jthottan@redhat.com>
Reviewed-on: https://code.engineering.redhat.com/gerrit/114349
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
---
 libglusterfs/src/mem-pool.c | 35 ++++++++++++++++++++++++++++++-----
 libglusterfs/src/mem-pool.h |  1 +
 2 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/libglusterfs/src/mem-pool.c b/libglusterfs/src/mem-pool.c
index 9f5f762..3295bdc 100644
--- a/libglusterfs/src/mem-pool.c
+++ b/libglusterfs/src/mem-pool.c
@@ -462,6 +462,7 @@ pool_sweeper (void *arg)
 
         for (;;) {
                 sleep (POOL_SWEEP_SECS);
+                (void) pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, NULL);
                 INIT_LIST_HEAD (&state.death_row);
                 state.n_cold_lists = 0;
 
@@ -497,6 +498,7 @@ pool_sweeper (void *arg)
                 for (i = 0; i < state.n_cold_lists; ++i) {
                         free_obj_list (state.cold_lists[i]);
                 }
+                (void) pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
         }
 }
 
@@ -535,15 +537,38 @@ mem_pools_preinit (void)
 #endif
 }
 
+#if !defined(GF_DISABLE_MEMPOOL)
+static pthread_mutex_t  init_mutex      = PTHREAD_MUTEX_INITIALIZER;
+static unsigned int     init_count;
+static pthread_t        sweeper_tid;
+
+
 void
 mem_pools_init (void)
 {
-        pthread_t       kid;
-
-        (void) pthread_create (&kid, NULL, pool_sweeper, NULL);
-        (void) pthread_detach (kid);
+        pthread_mutex_lock (&init_mutex);
+        if ((init_count++) == 0) {
+                (void) pthread_create (&sweeper_tid, NULL, pool_sweeper, NULL);
+        }
+        pthread_mutex_unlock (&init_mutex);
 }
- 
+void
+mem_pools_fini (void)
+{
+        pthread_mutex_lock (&init_mutex);
+        GF_ASSERT (init_count > 0);
+        if ((--init_count) == 0) {
+                (void) pthread_cancel (sweeper_tid);
+                (void) pthread_join (sweeper_tid, NULL);
+        }
+        pthread_mutex_unlock (&init_mutex);
+}
+
+#else
+void mem_pools_init (void) {}
+void mem_pools_fini (void) {}
+#endif
+
 struct mem_pool *
 mem_pool_new_fn (unsigned long sizeof_type,
                  unsigned long count, char *name)
diff --git a/libglusterfs/src/mem-pool.h b/libglusterfs/src/mem-pool.h
index 1b27119..5a1bff5 100644
--- a/libglusterfs/src/mem-pool.h
+++ b/libglusterfs/src/mem-pool.h
@@ -257,6 +257,7 @@ struct mem_pool {
 };
 
 void mem_pools_init (void);
+void mem_pools_fini (void);
 
 struct mem_pool *
 mem_pool_new_fn (unsigned long sizeof_type, unsigned long count, char *name);
-- 
1.8.3.1