summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugene Kosov <claprix@yandex.ru>2019-06-23 12:21:06 +0300
committerEugene Kosov <claprix@yandex.ru>2019-06-23 12:21:06 +0300
commitc285bd71beaf7b898847394b153fd440d0a20730 (patch)
tree148928d1c80f31d9f44fd16721125ddf4fe7d989
parenta82e42fd133949b560790c9b74a4072f899baee4 (diff)
downloadmariadb-git-bb-10.4-MDEV-19080-thread_local.tar.gz
-rw-r--r--include/my_pthread.h1
-rw-r--r--mysys/CMakeLists.txt2
-rw-r--r--mysys/my_init.c8
-rw-r--r--mysys/my_thr_init.cc (renamed from mysys/my_thr_init.c)42
-rw-r--r--sql/threadpool_common.cc11
5 files changed, 20 insertions, 44 deletions
diff --git a/include/my_pthread.h b/include/my_pthread.h
index 1b35d0fdd0d..b28d16f705f 100644
--- a/include/my_pthread.h
+++ b/include/my_pthread.h
@@ -746,6 +746,7 @@ extern void **my_thread_var_dbug(void);
extern safe_mutex_t **my_thread_var_mutex_in_use(void);
extern uint my_thread_end_wait_time;
extern my_bool safe_mutex_deadlock_detector;
+extern void set_mysys_var(struct st_my_thread_var *mysys_var);
#define my_thread_var (_my_thread_var())
#define my_errno my_thread_var->thr_errno
/*
diff --git a/mysys/CMakeLists.txt b/mysys/CMakeLists.txt
index 6990d1350e3..a24721e2d41 100644
--- a/mysys/CMakeLists.txt
+++ b/mysys/CMakeLists.txt
@@ -31,7 +31,7 @@ SET(MYSYS_SOURCES array.c charset-def.c charset.c checksum.c my_default.c
my_lib.c my_lock.c my_malloc.c my_mess.c
my_mkdir.c my_mmap.c my_once.c my_open.c my_pread.c my_pthread.c
my_quick.c my_read.c my_redel.c my_rename.c my_seek.c my_sleep.c
- my_static.c my_symlink.c my_symlink2.c my_sync.c my_thr_init.c
+ my_static.c my_symlink.c my_symlink2.c my_sync.c my_thr_init.cc
my_basename.c
my_write.c ptr_cmp.c queues.c stacktrace.c
string.c thr_alarm.c thr_lock.c thr_mutex.c
diff --git a/mysys/my_init.c b/mysys/my_init.c
index d8fb2003052..464fac84866 100644
--- a/mysys/my_init.c
+++ b/mysys/my_init.c
@@ -36,8 +36,6 @@ static my_bool win32_init_tcp_ip();
#define my_win_init()
#endif
-extern pthread_key(struct st_my_thread_var*, THR_KEY_mysys);
-
#define SCALE_SEC 100
#define SCALE_USEC 10000
@@ -249,10 +247,8 @@ Voluntary context switches %ld, Involuntary context switches %ld\n",
if (have_tcpip)
WSACleanup();
#endif /* __WIN__ */
-
- /* At very last, delete mysys key, it is used everywhere including DBUG */
- pthread_key_delete(THR_KEY_mysys);
- my_init_done= my_thr_key_mysys_exists= 0;
+
+ my_init_done= 0;
} /* my_end */
#ifdef DBUG_ASSERT_EXISTS
diff --git a/mysys/my_thr_init.c b/mysys/my_thr_init.cc
index b298c7b1bd7..4e908a223c5 100644
--- a/mysys/my_thr_init.c
+++ b/mysys/my_thr_init.cc
@@ -23,7 +23,7 @@
#include <m_string.h>
#include <signal.h>
-pthread_key(struct st_my_thread_var*, THR_KEY_mysys);
+thread_local st_my_thread_var *THR_KEY_mysys;
mysql_mutex_t THR_LOCK_malloc, THR_LOCK_open,
THR_LOCK_lock, THR_LOCK_myisam, THR_LOCK_heap,
THR_LOCK_net, THR_LOCK_charset, THR_LOCK_threads,
@@ -44,8 +44,6 @@ static uint get_thread_lib(void);
/** True if @c my_thread_global_init() has been called. */
static my_bool my_thread_global_init_done= 0;
-/* True if THR_KEY_mysys is created */
-my_bool my_thr_key_mysys_exists= 0;
/*
@@ -141,7 +139,7 @@ void my_thread_global_reinit(void)
my_thread_destroy_internal_mutex();
my_thread_init_internal_mutex();
- tmp= my_pthread_getspecific(struct st_my_thread_var*, THR_KEY_mysys);
+ tmp= THR_KEY_mysys;
DBUG_ASSERT(tmp);
my_thread_destory_thr_mutex(tmp);
@@ -169,21 +167,6 @@ my_bool my_thread_global_init(void)
return 0;
my_thread_global_init_done= 1;
- /*
- THR_KEY_mysys is deleted in my_end() as DBUG libraries are using it even
- after my_thread_global_end() is called.
- my_thr_key_mysys_exist is used to protect against application like QT
- that calls my_thread_global_init() + my_thread_global_end() multiple times
- without calling my_init() + my_end().
- */
- if (!my_thr_key_mysys_exists &&
- (pth_ret= pthread_key_create(&THR_KEY_mysys, NULL)) != 0)
- {
- fprintf(stderr, "Can't initialize threads: error %d\n", pth_ret);
- return 1;
- }
- my_thr_key_mysys_exists= 1;
-
/* Mutex used by my_thread_init() and after my_thread_destroy_mutex() */
my_thread_init_internal_mutex();
@@ -279,7 +262,7 @@ my_bool my_thread_init(void)
fprintf(stderr,"my_thread_init(): pthread_self: %p\n", pthread_self());
#endif
- if (my_pthread_getspecific(struct st_my_thread_var *,THR_KEY_mysys))
+ if (THR_KEY_mysys)
{
#ifdef EXTRA_DEBUG_THREADS
fprintf(stderr,"my_thread_init() called more than once in thread 0x%lx\n",
@@ -297,7 +280,7 @@ my_bool my_thread_init(void)
error= 1;
goto end;
}
- pthread_setspecific(THR_KEY_mysys,tmp);
+ THR_KEY_mysys= tmp;
tmp->pthread_self= pthread_self();
my_thread_init_thr_mutex(tmp);
@@ -334,7 +317,7 @@ end:
void my_thread_end(void)
{
struct st_my_thread_var *tmp;
- tmp= my_pthread_getspecific(struct st_my_thread_var*,THR_KEY_mysys);
+ tmp= THR_KEY_mysys;
#ifdef EXTRA_DEBUG_THREADS
fprintf(stderr,"my_thread_end(): tmp: %p pthread_self: %p thread_id: %ld\n",
@@ -355,7 +338,7 @@ void my_thread_end(void)
as the key is used by DBUG.
*/
DBUG_POP();
- pthread_setspecific(THR_KEY_mysys,0);
+ THR_KEY_mysys= nullptr;
if (tmp && tmp->init)
{
@@ -387,14 +370,11 @@ void my_thread_end(void)
}
}
-struct st_my_thread_var *_my_thread_var(void)
-{
- return my_pthread_getspecific(struct st_my_thread_var*,THR_KEY_mysys);
-}
+struct st_my_thread_var *_my_thread_var(void) { return THR_KEY_mysys; }
-int set_mysys_var(struct st_my_thread_var *mysys_var)
+void set_mysys_var(struct st_my_thread_var *mysys_var)
{
- return my_pthread_setspecific_ptr(THR_KEY_mysys, mysys_var);
+ THR_KEY_mysys= mysys_var;
}
/****************************************************************************
@@ -439,7 +419,7 @@ extern void **my_thread_var_dbug()
struct st_my_thread_var *tmp;
if (!my_thread_global_init_done)
return NULL;
- tmp= my_pthread_getspecific(struct st_my_thread_var*,THR_KEY_mysys);
+ tmp= THR_KEY_mysys;
return tmp && tmp->init ? &tmp->dbug : 0;
}
#endif /* DBUG_OFF */
@@ -451,7 +431,7 @@ safe_mutex_t **my_thread_var_mutex_in_use()
struct st_my_thread_var *tmp;
if (!my_thread_global_init_done)
return NULL;
- tmp= my_pthread_getspecific(struct st_my_thread_var*,THR_KEY_mysys);
+ tmp= THR_KEY_mysys;
return tmp ? &tmp->mutex_in_use : 0;
}
diff --git a/sql/threadpool_common.cc b/sql/threadpool_common.cc
index 4dd0fa35b40..b741858fcb2 100644
--- a/sql/threadpool_common.cc
+++ b/sql/threadpool_common.cc
@@ -45,7 +45,6 @@ static void threadpool_remove_connection(THD *thd);
static int threadpool_process_request(THD *thd);
static THD* threadpool_add_connection(CONNECT *connect, void *scheduler_data);
-extern "C" pthread_key(struct st_my_thread_var*, THR_KEY_mysys);
extern bool do_command(THD*);
static inline TP_connection *get_TP_connection(THD *thd)
@@ -85,13 +84,13 @@ struct Worker_thread_context
void save()
{
psi_thread = PSI_CALL_get_thread();
- mysys_var= (st_my_thread_var *)pthread_getspecific(THR_KEY_mysys);
+ mysys_var= my_thread_var;
}
void restore()
{
PSI_CALL_set_thread(psi_thread);
- pthread_setspecific(THR_KEY_mysys,mysys_var);
+ set_mysys_var(mysys_var);
pthread_setspecific(THR_THD, 0);
}
};
@@ -137,7 +136,7 @@ static inline void set_thd_idle(THD *thd)
*/
static void thread_attach(THD* thd)
{
- pthread_setspecific(THR_KEY_mysys,thd->mysys_var);
+ set_mysys_var(thd->mysys_var);
thd->thread_stack=(char*)&thd;
thd->store_globals();
PSI_CALL_set_thread(thd->event_scheduler.m_psi);
@@ -222,9 +221,9 @@ static THD* threadpool_add_connection(CONNECT *connect, void *scheduler_data)
Store them in THD.
*/
- pthread_setspecific(THR_KEY_mysys, 0);
+ set_mysys_var(nullptr);
my_thread_init();
- st_my_thread_var* mysys_var= (st_my_thread_var *)pthread_getspecific(THR_KEY_mysys);
+ st_my_thread_var* mysys_var= my_thread_var;
if (!mysys_var ||!(thd= connect->create_thd(NULL)))
{
/* Out of memory? */