diff options
author | Jeremy Allison <jra@samba.org> | 2009-04-14 15:19:39 -0700 |
---|---|---|
committer | Jeremy Allison <jra@samba.org> | 2009-04-14 15:19:39 -0700 |
commit | 8e06e945b6b03db825862f2f057f0dfe13ade3a2 (patch) | |
tree | d95c239803eec9626c9da3b16f5bc0c71672b523 /lib | |
parent | 4721be488ceb8229d564e4b63ca9937603394e51 (diff) | |
download | samba-8e06e945b6b03db825862f2f057f0dfe13ade3a2.tar.gz samba-8e06e945b6b03db825862f2f057f0dfe13ade3a2.tar.bz2 samba-8e06e945b6b03db825862f2f057f0dfe13ade3a2.zip |
Ensure a tls key is only generated once - wrap create & destroy in a mutex.
Change the function names to add _once to the tls_create & tls_destroy to
make this obvious.
Jeremy.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/util/smb_threads.c | 9 | ||||
-rw-r--r-- | lib/util/smb_threads.h | 84 | ||||
-rw-r--r-- | lib/util/smb_threads_internal.h | 16 | ||||
-rw-r--r-- | lib/util/talloc_stack.c | 2 |
4 files changed, 71 insertions, 40 deletions
diff --git a/lib/util/smb_threads.c b/lib/util/smb_threads.c index 8e0e1cd915..fa2d8da186 100644 --- a/lib/util/smb_threads.c +++ b/lib/util/smb_threads.c @@ -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; } diff --git a/lib/util/smb_threads.h b/lib/util/smb_threads.h index 2ca163be9a..945e93803a 100644 --- a/lib/util/smb_threads.h +++ b/lib/util/smb_threads.h @@ -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 } diff --git a/lib/util/smb_threads_internal.h b/lib/util/smb_threads_internal.h index ad05aae9a5..58c6fe3f99 100644 --- a/lib/util/smb_threads_internal.h +++ b/lib/util/smb_threads_internal.h @@ -33,13 +33,13 @@ #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) @@ -50,4 +50,12 @@ #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 diff --git a/lib/util/talloc_stack.c b/lib/util/talloc_stack.c index a9b6e033ce..f572dd6c77 100644 --- a/lib/util/talloc_stack.c +++ b/lib/util/talloc_stack.c @@ -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"); } |