summaryrefslogtreecommitdiffstats
path: root/libglusterfs/src/globals.c
diff options
context:
space:
mode:
authorVijay Bellur <vbellur@redhat.com>2015-01-11 23:13:08 +0530
committerVijay Bellur <vbellur@redhat.com>2015-01-28 06:06:19 -0800
commit88136b53f59e3b81aacc28df18bda575da35b02d (patch)
treef420c5cd9b0a3515ae2cd4df9e255ebec9e138c9 /libglusterfs/src/globals.c
parentc8a6904396142b832ec31c37f43f44b139d24e1f (diff)
libglusterfs: Avoid initializing per process globals more than once.
gfapi consumers can invoke glusters_globals_init() multiple times through glfs_new(). This will result in re-initialization of already inited variables and non deterministic behavior. To avoid this, a new function gf_globals_init_once() has been added. The invocation of this function is guarded through pthread_once(), thereby ensuring single initialization of per process globals. Change-Id: I0ecde02ee49e0c7379c2eb0f1c879d89774ec82f BUG: 1184366 Signed-off-by: Vijay Bellur <vbellur@redhat.com> Signed-off-by: Raghavendra Bhat <raghavendra@redhat.com> Reviewed-on: http://review.gluster.org/9430 Tested-by: Gluster Build System <jenkins@build.gluster.com>
Diffstat (limited to 'libglusterfs/src/globals.c')
-rw-r--r--libglusterfs/src/globals.c28
1 files changed, 24 insertions, 4 deletions
diff --git a/libglusterfs/src/globals.c b/libglusterfs/src/globals.c
index cf707c7af4b..57467ecde1d 100644
--- a/libglusterfs/src/globals.c
+++ b/libglusterfs/src/globals.c
@@ -82,6 +82,7 @@ static char global_uuid_buf[GF_UUID_BUF_SIZE];
static pthread_key_t lkowner_buf_key;
static char global_lkowner_buf[GF_LKOWNER_BUF_SIZE];
static int gf_global_mem_acct_enable = 1;
+static pthread_once_t globals_inited = PTHREAD_ONCE_INIT;
int
@@ -329,13 +330,11 @@ glusterfs_lkowner_buf_get ()
return buf;
}
-int
-glusterfs_globals_init (glusterfs_ctx_t *ctx)
+static void
+gf_globals_init_once ()
{
int ret = 0;
- gf_log_globals_init (ctx);
-
ret = glusterfs_this_init ();
if (ret) {
gf_log ("", GF_LOG_CRITICAL,
@@ -371,5 +370,26 @@ glusterfs_globals_init (glusterfs_ctx_t *ctx)
goto out;
}
out:
+
+ if (ret) {
+ gf_log ("", GF_LOG_CRITICAL, "Exiting as global "
+ "initialization failed");
+ exit (ret);
+ }
+}
+
+int
+glusterfs_globals_init (glusterfs_ctx_t *ctx)
+{
+ int ret = 0;
+
+ gf_log_globals_init (ctx);
+
+ ret = pthread_once (&globals_inited, gf_globals_init_once);
+
+ if (ret)
+ gf_log ("", GF_LOG_CRITICAL, "pthread_once failed with: %d",
+ ret);
+
return ret;
}