Ensure a tls key is only generated once - wrap create & destroy in a mutex.
authorJeremy Allison <jra@samba.org>
Tue, 14 Apr 2009 22:19:39 +0000 (15:19 -0700)
committerJeremy Allison <jra@samba.org>
Tue, 14 Apr 2009 22:19:39 +0000 (15:19 -0700)
Change the function names to add _once to the tls_create & tls_destroy to
make this obvious.
Jeremy.

lib/util/smb_threads.c
lib/util/smb_threads.h
lib/util/smb_threads_internal.h
lib/util/talloc_stack.c

index 8e0e1cd9157d627a91f9a61f217b2f452ac2f2ad..fa2d8da18650088eb3f6079b839583f3db4330eb 100644 (file)
@@ -25,8 +25,6 @@
 
 #include "includes.h"
 
-#define NUM_GLOBAL_LOCKS 1
-
 /*********************************************************
  Functions to vector the locking primitives used internally
  by libsmbclient.
@@ -94,14 +92,18 @@ int smb_thread_set_functions(const struct smb_thread_functions *tf)
 
 SMB_THREADS_DEF_PTHREAD_IMPLEMENTATION(tf);
 
+void *pkey = NULL;
+
 /* Test function. */
 int test_threads(void)
 {
        int ret;
        void *plock = NULL;
-
        smb_thread_set_functions(&tf);
 
+       if ((ret = SMB_THREAD_CREATE_TLS_ONCE("test_tls", pkey)) != 0) {
+               printf("Create tls once error: %d\n", ret);
+       }
        if ((ret = SMB_THREAD_CREATE_MUTEX("test", plock)) != 0) {
                printf("Create lock error: %d\n", ret);
        }
@@ -112,6 +114,7 @@ int test_threads(void)
                printf("unlock error: %d\n", ret);
        }
        SMB_THREAD_DESTROY_MUTEX(plock);
+       SMB_THREAD_DESTROY_TLS_ONCE(pkey);
 
        return 0;
 }
index 2ca163be9a723d370b44f73e11633e04383b71f2..945e93803a39ec4ce49ff8eac15af99bff5358b7 100644 (file)
@@ -36,10 +36,10 @@ struct smb_thread_functions {
                        const char *location);
 
        /* Thread local storage. */
-       int (*create_tls)(const char *keyname,
+       int (*create_tls_once)(const char *keyname,
                        void **ppkey,
                        const char *location);
-       void (*destroy_tls)(void *pkey,
+       void (*destroy_tls_once)(void **pkey,
                        const char *location);
        int (*set_tls)(void *pkey, const void *pval, const char *location);
        void *(*get_tls)(void *pkey, const char *location);
@@ -53,49 +53,69 @@ extern const struct smb_thread_functions *global_tfp;
  \
 static int smb_create_mutex_pthread(const char *lockname, void **pplock, const char *location) \
 { \
-        pthread_mutex_t *pmut = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); \
-        if (!pmut) { \
-                return ENOMEM; \
-        } \
-        pthread_mutex_init(pmut, NULL); \
-        *pplock = (void *)pmut; \
-        return 0; \
+       pthread_mutex_t *pmut = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); \
+       if (!pmut) { \
+               return ENOMEM; \
+       } \
+       pthread_mutex_init(pmut, NULL); \
+       *pplock = (void *)pmut; \
+       return 0; \
 } \
  \
 static void smb_destroy_mutex_pthread(void *plock, const char *location) \
 { \
-        pthread_mutex_destroy((pthread_mutex_t *)plock); \
+       pthread_mutex_destroy((pthread_mutex_t *)plock); \
        free(plock); \
 } \
  \
 static int smb_lock_pthread(void *plock, enum smb_thread_lock_type lock_type, const char *location) \
 { \
-        if (lock_type == SMB_THREAD_UNLOCK) { \
-                return pthread_mutex_unlock((pthread_mutex_t *)plock); \
-        } else { \
-                return pthread_mutex_lock((pthread_mutex_t *)plock); \
-        } \
+       if (lock_type == SMB_THREAD_UNLOCK) { \
+               return pthread_mutex_unlock((pthread_mutex_t *)plock); \
+       } else { \
+               return pthread_mutex_lock((pthread_mutex_t *)plock); \
+       } \
 } \
  \
-static int smb_create_tls_pthread(const char *keyname, void **ppkey, const char *location) \
+static pthread_mutex_t create_tls_mutex = PTHREAD_MUTEX_INITIALIZER; \
+ \
+static int smb_create_tls_once_pthread(const char *keyname, void **ppkey, const char *location) \
 { \
-        int ret; \
-        pthread_key_t *pkey = (pthread_key_t *)malloc(sizeof(pthread_key_t)); \
-        if (!pkey) { \
-                return ENOMEM; \
-        } \
-        ret = pthread_key_create(pkey, NULL); \
-        if (ret) { \
-                return ret; \
-        } \
-        *ppkey = (void *)pkey; \
-        return 0; \
+       int ret; \
+       pthread_key_t *pkey; \
+       ret = pthread_mutex_lock(&create_tls_mutex); \
+       if (ret) { \
+               return ret; \
+       } \
+       if (*ppkey) { \
+               pthread_mutex_unlock(&create_tls_mutex); \
+               return 0; \
+       } \
+       pkey = (pthread_key_t *)malloc(sizeof(pthread_key_t)); \
+       if (!pkey) { \
+               pthread_mutex_unlock(&create_tls_mutex); \
+               return ENOMEM; \
+       } \
+       ret = pthread_key_create(pkey, NULL); \
+       if (ret) { \
+               free(pkey); \
+               pthread_mutex_unlock(&create_tls_mutex); \
+               return ret; \
+       } \
+       *ppkey = (void *)pkey; \
+       pthread_mutex_unlock(&create_tls_mutex); \
+       return 0; \
 } \
  \
-static void smb_destroy_tls_pthread(void *pkey, const char *location) \
+static void smb_destroy_tls_once_pthread(void **ppkey, const char *location) \
 { \
-        pthread_key_delete(*(pthread_key_t *)pkey); \
-        free(pkey); \
+       pthread_mutex_lock(&create_tls_mutex); \
+       if (*ppkey) { \
+               pthread_key_delete(*(pthread_key_t *)ppkey); \
+               free(*ppkey); \
+               *ppkey = NULL; \
+       } \
+       pthread_mutex_unlock(&create_tls_mutex); \
 } \
  \
 static int smb_set_tls_pthread(void *pkey, const void *pval, const char *location) \
@@ -112,8 +132,8 @@ static const struct smb_thread_functions (tf) = { \
                         smb_create_mutex_pthread, \
                         smb_destroy_mutex_pthread, \
                         smb_lock_pthread, \
-                        smb_create_tls_pthread, \
-                        smb_destroy_tls_pthread, \
+                        smb_create_tls_once_pthread, \
+                        smb_destroy_tls_once_pthread, \
                         smb_set_tls_pthread, \
                         smb_get_tls_pthread }
 
index ad05aae9a5e2291edf05ff2d846b0d2def758cd5..58c6fe3f99a11e83014804720318998c0e9f8318 100644 (file)
 #define SMB_THREAD_LOCK(plock, type) \
        (global_tfp ? global_tfp->lock_mutex((plock), (type), __location__) : 0)
 
-#define SMB_THREAD_CREATE_TLS(keyname, key) \
-       (global_tfp ? global_tfp->create_tls((keyname), &(key), __location__) : 0)
+#define SMB_THREAD_CREATE_TLS_ONCE(keyname, key) \
+       (global_tfp ? global_tfp->create_tls_once((keyname), &(key), __location__) : 0)
 
-#define SMB_THREAD_DESTROY_TLS(key) \
+#define SMB_THREAD_DESTROY_TLS_ONCE(key) \
        do { \
                if (global_tfp) { \
-                       global_tfp->destroy_tls(key); \
+                       global_tfp->destroy_tls_once(&(key), __location__); \
                }; \
        } while (0)
 
 #define SMB_THREAD_GET_TLS(key) \
        (global_tfp ? global_tfp->get_tls((key), __location__) : (key))
 
+/*
+ * Global thread lock list.
+ */
+
+#define NUM_GLOBAL_LOCKS 1
+
+#define GLOBAL_LOCK(locknum) (global_lock_array ? global_lock_array[(locknum)] : NULL)
+
 #endif
index a9b6e033cebe416367cdbf22a5fc0b7a1f9997b0..f572dd6c77c39c72d0201a03a5076e84166250d8 100644 (file)
@@ -74,7 +74,7 @@ static struct talloc_stackframe *talloc_stackframe_init(void)
 
        ZERO_STRUCTP(ts);
 
-       if (SMB_THREAD_CREATE_TLS("talloc_stackframe", global_ts)) {
+       if (SMB_THREAD_CREATE_TLS_ONCE("talloc_stackframe", global_ts)) {
                smb_panic("talloc_stackframe_init create_tls failed");
        }