summaryrefslogtreecommitdiff
path: root/sql
diff options
context:
space:
mode:
authorEugene Kosov <claprix@yandex.ru>2020-04-30 20:06:26 +0300
committerEugene Kosov <claprix@yandex.ru>2020-05-05 18:13:31 +0300
commit89ff4176c125f5993aba0732e52102227577ed96 (patch)
tree42980e30d3f4f02b09b0fda80790f7241fd699f4 /sql
parent90aad47dd9f28101b1d2c4a01c2a10db5ad5f426 (diff)
downloadmariadb-git-89ff4176c125f5993aba0732e52102227577ed96.tar.gz
MDEV-22437 make THR_THD* variable thread_local
Now all access goes through _current_thd() and set_current_thd() functions. Some functions like THD::store_globals() can not fail now.
Diffstat (limited to 'sql')
-rw-r--r--sql/event_scheduler.cc3
-rw-r--r--sql/mysqld.cc18
-rw-r--r--sql/mysqld.h8
-rw-r--r--sql/slave.cc11
-rw-r--r--sql/sql_class.cc12
-rw-r--r--sql/sql_class.h2
-rw-r--r--sql/sql_connect.cc18
-rw-r--r--sql/sql_connect.h2
-rw-r--r--sql/sql_insert.cc5
-rw-r--r--sql/threadpool_common.cc5
-rw-r--r--sql/wsrep_mysqld.cc12
-rw-r--r--sql/wsrep_sst.cc2
-rw-r--r--sql/wsrep_thd.cc4
-rw-r--r--sql/wsrep_thd.h2
-rw-r--r--sql/wsrep_utils.cc2
15 files changed, 35 insertions, 71 deletions
diff --git a/sql/event_scheduler.cc b/sql/event_scheduler.cc
index 0e8e4826939..e842d95e3b0 100644
--- a/sql/event_scheduler.cc
+++ b/sql/event_scheduler.cc
@@ -128,11 +128,12 @@ bool
post_init_event_thread(THD *thd)
{
(void) init_new_connection_handler_thread();
- if (init_thr_lock() || thd->store_globals())
+ if (init_thr_lock())
{
thd->cleanup();
return TRUE;
}
+ thd->store_globals();
return FALSE;
}
diff --git a/sql/mysqld.cc b/sql/mysqld.cc
index 1c3af7349b9..faf7a244647 100644
--- a/sql/mysqld.cc
+++ b/sql/mysqld.cc
@@ -666,7 +666,10 @@ static std::atomic<char*> shutdown_user;
/* Thread specific variables */
-pthread_key(THD*, THR_THD);
+static thread_local THD *THR_THD;
+
+MYSQL_THD _current_thd() { return THR_THD; }
+void set_current_thd(THD *thd) { THR_THD= thd; }
/*
LOCK_start_thread is used to syncronize thread start and stop with
@@ -1917,13 +1920,6 @@ extern "C" void unireg_abort(int exit_code)
}
-static void cleanup_tls()
-{
- if (THR_THD)
- (void)pthread_key_delete(THR_THD);
-}
-
-
static void mysqld_exit(int exit_code)
{
DBUG_ENTER("mysqld_exit");
@@ -1952,7 +1948,6 @@ static void mysqld_exit(int exit_code)
if (exit_code == 0)
SAFEMALLOC_REPORT_MEMORY(0);
}
- cleanup_tls();
DBUG_LEAVE;
sd_notify(0, "STATUS=MariaDB server is down");
exit(exit_code); /* purecov: inspected */
@@ -3691,11 +3686,6 @@ static const char *rpl_make_log_name(PSI_memory_key key, const char *opt,
static int init_early_variables()
{
- if (pthread_key_create(&THR_THD, NULL))
- {
- fprintf(stderr, "Fatal error: Can't create thread-keys\n");
- return 1;
- }
set_current_thd(0);
set_malloc_size_cb(my_malloc_size_cb_func);
global_status_var.global_memory_used= 0;
diff --git a/sql/mysqld.h b/sql/mysqld.h
index 8adfda0dfec..9cd0fe0a7fe 100644
--- a/sql/mysqld.h
+++ b/sql/mysqld.h
@@ -769,8 +769,6 @@ extern char *opt_ssl_ca, *opt_ssl_capath, *opt_ssl_cert, *opt_ssl_cipher,
*opt_ssl_key, *opt_ssl_crl, *opt_ssl_crlpath;
extern ulonglong tls_version;
-extern MYSQL_PLUGIN_IMPORT pthread_key(THD*, THR_THD);
-
#ifdef MYSQL_SERVER
/**
@@ -928,11 +926,7 @@ inline void table_case_convert(char * name, uint length)
extern void set_server_version(char *buf, size_t size);
#define current_thd _current_thd()
-inline int set_current_thd(THD *thd)
-{
- return my_pthread_setspecific_ptr(THR_THD, thd);
-}
-
+void set_current_thd(THD *thd);
/*
@todo remove, make it static in ha_maria.cc
diff --git a/sql/slave.cc b/sql/slave.cc
index 381417c353b..fb26856f811 100644
--- a/sql/slave.cc
+++ b/sql/slave.cc
@@ -3628,9 +3628,16 @@ static int init_slave_thread(THD* thd, Master_info *mi,
thd->system_thread = (thd_type == SLAVE_THD_SQL) ?
SYSTEM_THREAD_SLAVE_SQL : SYSTEM_THREAD_SLAVE_IO;
+ if (init_thr_lock())
+ {
+ thd->cleanup();
+ DBUG_RETURN(-1);
+ }
+
/* We must call store_globals() before doing my_net_init() */
- if (init_thr_lock() || thd->store_globals() ||
- my_net_init(&thd->net, 0, thd, MYF(MY_THREAD_SPECIFIC)) ||
+ thd->store_globals();
+
+ if (my_net_init(&thd->net, 0, thd, MYF(MY_THREAD_SPECIFIC)) ||
IF_DBUG(simulate_error & (1<< thd_type), 0))
{
thd->cleanup();
diff --git a/sql/sql_class.cc b/sql/sql_class.cc
index 03383a744fc..591828b4306 100644
--- a/sql/sql_class.cc
+++ b/sql/sql_class.cc
@@ -1214,11 +1214,6 @@ void thd_gmt_sec_to_TIME(MYSQL_THD thd, MYSQL_TIME *ltime, my_time_t t)
#ifdef _WIN32
-extern "C" THD *_current_thd_noinline(void)
-{
- return my_pthread_getspecific_ptr(THD*,THR_THD);
-}
-
extern "C" my_thread_id next_thread_id_noinline()
{
#undef next_thread_id
@@ -2152,7 +2147,7 @@ void THD::reset_killed()
the structure for the net buffer
*/
-bool THD::store_globals()
+void THD::store_globals()
{
/*
Assert that thread_stack is initialized: it's necessary to be able
@@ -2160,8 +2155,7 @@ bool THD::store_globals()
*/
DBUG_ASSERT(thread_stack);
- if (set_current_thd(this))
- return 1;
+ set_current_thd(this);
/*
mysys_var is concurrently readable by a killer thread.
It is protected by LOCK_thd_kill, it is not needed to lock while the
@@ -2203,8 +2197,6 @@ bool THD::store_globals()
created in another thread
*/
thr_lock_info_init(&lock_info, mysys_var);
-
- return 0;
}
/**
diff --git a/sql/sql_class.h b/sql/sql_class.h
index 96ebfca3723..1069b49790a 100644
--- a/sql/sql_class.h
+++ b/sql/sql_class.h
@@ -3390,7 +3390,7 @@ public:
void cleanup_after_query();
void free_connection();
void reset_for_reuse();
- bool store_globals();
+ void store_globals();
void reset_globals();
bool trace_started()
{
diff --git a/sql/sql_connect.cc b/sql/sql_connect.cc
index aa9e561a717..dbe66650476 100644
--- a/sql/sql_connect.cc
+++ b/sql/sql_connect.cc
@@ -1105,16 +1105,9 @@ static int check_connection(THD *thd)
In this case we will close the connection and increment status
*/
-bool setup_connection_thread_globals(THD *thd)
+void setup_connection_thread_globals(THD *thd)
{
- if (thd->store_globals())
- {
- close_connection(thd, ER_OUT_OF_RESOURCES);
- statistic_increment(aborted_connects,&LOCK_status);
- statistic_increment(connection_errors_internal, &LOCK_status);
- return 1; // Error
- }
- return 0;
+ thd->store_globals();
}
@@ -1397,12 +1390,7 @@ void do_handle_one_connection(CONNECT *connect, bool put_in_cache)
stack overruns.
*/
thd->thread_stack= (char*) &thd;
- if (setup_connection_thread_globals(thd))
- {
- unlink_thd(thd);
- delete thd;
- return;
- }
+ setup_connection_thread_globals(thd);
for (;;)
{
diff --git a/sql/sql_connect.h b/sql/sql_connect.h
index 4d62834a6f9..152bd71afd7 100644
--- a/sql/sql_connect.h
+++ b/sql/sql_connect.h
@@ -82,7 +82,7 @@ void decrease_user_connections(USER_CONN *uc);
#define decrease_user_connections(X) do { } while(0) /* nothing */
#endif
bool thd_init_client_charset(THD *thd, uint cs_number);
-bool setup_connection_thread_globals(THD *thd);
+void setup_connection_thread_globals(THD *thd);
bool thd_prepare_connection(THD *thd);
bool thd_is_connection_alive(THD *thd);
int thd_set_peer_addr(THD *thd, sockaddr_storage *addr,
diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc
index 2a2b74405f3..0914b4ac497 100644
--- a/sql/sql_insert.cc
+++ b/sql/sql_insert.cc
@@ -3087,15 +3087,16 @@ pthread_handler_t handle_delayed_insert(void *arg)
{
DBUG_ENTER("handle_delayed_insert");
thd->thread_stack= (char*) &thd;
- if (init_thr_lock() || thd->store_globals())
+ if (init_thr_lock())
{
- /* Can't use my_error since store_globals has perhaps failed */
thd->get_stmt_da()->set_error_status(ER_OUT_OF_RESOURCES);
di->handler_thread_initialized= TRUE;
thd->fatal_error();
goto err;
}
+ thd->store_globals();
+
thd->lex->sql_command= SQLCOM_INSERT; // For innodb::store_lock()
/*
diff --git a/sql/threadpool_common.cc b/sql/threadpool_common.cc
index 213b1baf5ab..50cc9aa43f2 100644
--- a/sql/threadpool_common.cc
+++ b/sql/threadpool_common.cc
@@ -96,7 +96,7 @@ struct Worker_thread_context
{
PSI_CALL_set_thread(psi_thread);
set_mysys_var(mysys_var);
- pthread_setspecific(THR_THD, 0);
+ set_current_thd(nullptr);
}
};
@@ -255,8 +255,7 @@ static THD* threadpool_add_connection(CONNECT *connect, void *scheduler_data)
thd->start_utime= now;
thd->thr_create_utime= now;
- if (setup_connection_thread_globals(thd))
- goto end;
+ setup_connection_thread_globals(thd);
if (thd_prepare_connection(thd))
goto end;
diff --git a/sql/wsrep_mysqld.cc b/sql/wsrep_mysqld.cc
index 49880b2e827..7d54aab3ac0 100644
--- a/sql/wsrep_mysqld.cc
+++ b/sql/wsrep_mysqld.cc
@@ -1103,7 +1103,7 @@ void wsrep_shutdown_replication()
node_uuid= WSREP_UUID_UNDEFINED;
/* Undocking the thread specific data. */
- my_pthread_setspecific_ptr(THR_THD, NULL);
+ set_current_thd(nullptr);
}
bool wsrep_start_replication()
@@ -2926,15 +2926,7 @@ void* start_wsrep_THD(void *arg)
thd->thread_stack= (char*) &thd;
wsrep_assign_from_threadvars(thd);
- if (wsrep_store_threadvars(thd))
- {
- close_connection(thd, ER_OUT_OF_RESOURCES);
- statistic_increment(aborted_connects,&LOCK_status);
- unlink_thd(thd);
- delete thd;
- delete thd_args;
- goto error;
- }
+ wsrep_store_threadvars(thd);
thd->system_thread= SYSTEM_THREAD_SLAVE_SQL;
thd->security_ctx->skip_grants();
diff --git a/sql/wsrep_sst.cc b/sql/wsrep_sst.cc
index 8ffcb32e10a..93b7bc74489 100644
--- a/sql/wsrep_sst.cc
+++ b/sql/wsrep_sst.cc
@@ -364,7 +364,7 @@ void wsrep_sst_received (THD* thd,
wsrep_store_threadvars(thd);
}
else {
- my_pthread_setspecific_ptr(THR_THD, NULL);
+ set_current_thd(nullptr);
}
/* During sst WSREP(thd) is not yet set for joiner. */
diff --git a/sql/wsrep_thd.cc b/sql/wsrep_thd.cc
index 0f72c132d84..21a1c03d33e 100644
--- a/sql/wsrep_thd.cc
+++ b/sql/wsrep_thd.cc
@@ -454,13 +454,13 @@ void wsrep_restore_threadvars(const Wsrep_threadvars& globals)
pthread_setspecific(THR_KEY_mysys, globals.mysys_var);
}
-int wsrep_store_threadvars(THD *thd)
+void wsrep_store_threadvars(THD *thd)
{
if (thread_handling == SCHEDULER_TYPES_COUNT)
{
pthread_setspecific(THR_KEY_mysys, thd->mysys_var);
}
- return thd->store_globals();
+ thd->store_globals();
}
void wsrep_reset_threadvars(THD *thd)
diff --git a/sql/wsrep_thd.h b/sql/wsrep_thd.h
index d24d8e6358f..c7350c79ee7 100644
--- a/sql/wsrep_thd.h
+++ b/sql/wsrep_thd.h
@@ -162,7 +162,7 @@ void wsrep_restore_threadvars(const Wsrep_threadvars&);
/**
Store variables into thread local storage.
*/
-int wsrep_store_threadvars(THD *);
+void wsrep_store_threadvars(THD *);
/**
Reset thread local storage.
diff --git a/sql/wsrep_utils.cc b/sql/wsrep_utils.cc
index 59fa68db6c0..148def54aaf 100644
--- a/sql/wsrep_utils.cc
+++ b/sql/wsrep_utils.cc
@@ -436,7 +436,7 @@ thd::~thd ()
if (ptr)
{
delete ptr;
- my_pthread_setspecific_ptr (THR_THD, 0);
+ set_current_thd(nullptr);
}
}