Initialize lttng_ht_seed on hashtable creation
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 20 Oct 2015 14:45:28 +0000 (10:45 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 20 Oct 2015 14:45:28 +0000 (10:45 -0400)
This is done to get rid of the need for constructors to be executed
such as in the case of a static build.

Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/common/hashtable/hashtable.c

index 5b861c8004d04e4f125e2c294cc980f42e0869d6..4fb19cffda923bf1f19062be891f6180f0e48c14 100644 (file)
@@ -27,6 +27,9 @@
 #include "hashtable.h"
 #include "utils.h"
 
 #include "hashtable.h"
 #include "utils.h"
 
+/* seed_lock protects both seed_init and lttng_ht_seed. */
+static pthread_mutex_t seed_lock = PTHREAD_MUTEX_INITIALIZER;
+static bool seed_init;
 unsigned long lttng_ht_seed;
 
 static unsigned long min_hash_alloc_size = 1;
 unsigned long lttng_ht_seed;
 
 static unsigned long min_hash_alloc_size = 1;
@@ -95,6 +98,13 @@ struct lttng_ht *lttng_ht_new(unsigned long size, int type)
        if (!size)
                size = DEFAULT_HT_SIZE;
 
        if (!size)
                size = DEFAULT_HT_SIZE;
 
+       pthread_mutex_lock(&seed_lock);
+       if (!seed_init) {
+               lttng_ht_seed = (unsigned long) time(NULL);
+               seed_init = true;
+       }
+       pthread_mutex_unlock(&seed_lock);
+
        ht = zmalloc(sizeof(*ht));
        if (ht == NULL) {
                PERROR("zmalloc lttng_ht");
        ht = zmalloc(sizeof(*ht));
        if (ht == NULL) {
                PERROR("zmalloc lttng_ht");
@@ -578,12 +588,3 @@ struct lttng_ht_node_two_u64 *lttng_ht_iter_get_node_two_u64(
        }
        return caa_container_of(node, struct lttng_ht_node_two_u64, node);
 }
        }
        return caa_container_of(node, struct lttng_ht_node_two_u64, node);
 }
-
-/*
- * lib constructor
- */
-static void __attribute__((constructor)) _init(void)
-{
-       /* Init hash table seed */
-       lttng_ht_seed = (unsigned long) time(NULL);
-}
This page took 0.026169 seconds and 4 git commands to generate.