mirror of
https://github.com/samba-team/samba.git
synced 2025-03-27 22:50:26 +03:00
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.
This commit is contained in:
parent
4721be488c
commit
8e06e945b6
@ -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;
|
||||
}
|
||||
|
@ -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 }
|
||||
|
||||
|
@ -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
|
||||
|
@ -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");
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user