Browse Source

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.
pull/1528/head
Eugene Kosov 6 years ago
parent
commit
89ff4176c1
  1. 6
      extra/mariabackup/xtrabackup.cc
  2. 16
      include/my_pthread.h
  3. 11
      libmysqld/lib_sql.cc
  4. 3
      plugin/feedback/sender_thread.cc
  5. 4
      plugin/handler_socket/handlersocket/database.cpp
  6. 3
      sql/event_scheduler.cc
  7. 18
      sql/mysqld.cc
  8. 8
      sql/mysqld.h
  9. 11
      sql/slave.cc
  10. 12
      sql/sql_class.cc
  11. 2
      sql/sql_class.h
  12. 18
      sql/sql_connect.cc
  13. 2
      sql/sql_connect.h
  14. 5
      sql/sql_insert.cc
  15. 5
      sql/threadpool_common.cc
  16. 12
      sql/wsrep_mysqld.cc
  17. 2
      sql/wsrep_sst.cc
  18. 4
      sql/wsrep_thd.cc
  19. 2
      sql/wsrep_thd.h
  20. 2
      sql/wsrep_utils.cc
  21. 20
      storage/spider/spd_conn.cc
  22. 8
      storage/spider/spd_table.cc
  23. 3
      storage/spider/spd_trx.cc

6
extra/mariabackup/xtrabackup.cc

@ -6021,9 +6021,6 @@ int main(int argc, char **argv)
init_signals();
MY_INIT(argv[0]);
pthread_key_create(&THR_THD, NULL);
my_pthread_setspecific_ptr(THR_THD, NULL);
xb_regex_init();
capture_tool_command(argc, argv);
@ -6070,9 +6067,6 @@ int main(int argc, char **argv)
}
#endif
if (THR_THD)
(void) pthread_key_delete(THR_THD);
logger.cleanup_base();
cleanup_errmsgs();
free_error_messages();

16
include/my_pthread.h

@ -360,22 +360,8 @@ size_t my_setstacksize(pthread_attr_t *attr, size_t stacksize);
#ifdef MYSQL_CLIENT
#define _current_thd() NULL
#elif defined(_WIN32)
#ifdef __cplusplus
extern "C"
#endif
MYSQL_THD _current_thd_noinline();
#define _current_thd() _current_thd_noinline()
#else
/*
THR_THD is a key which will be used to set/get THD* for a thread,
using my_pthread_setspecific_ptr()/my_thread_getspecific_ptr().
*/
extern pthread_key(MYSQL_THD, THR_THD);
static inline MYSQL_THD _current_thd(void)
{
return my_pthread_getspecific_ptr(MYSQL_THD,THR_THD);
}
MYSQL_THD _current_thd();
#endif
/* safe_mutex adds checking to mutex for easier debugging */

11
libmysqld/lib_sql.cc

@ -437,7 +437,7 @@ static void emb_free_embedded_thd(MYSQL *mysql)
thd->clear_data_list();
thd->store_globals();
delete thd;
my_pthread_setspecific_ptr(THR_THD, 0);
set_current_thd(nullptr);
mysql->thd=0;
}
@ -683,11 +683,7 @@ void *create_embedded_thd(int client_flag)
THD * thd= new THD(next_thread_id());
thd->thread_stack= (char*) &thd;
if (thd->store_globals())
{
fprintf(stderr,"store_globals failed.\n");
goto err;
}
thd->store_globals();
lex_start(thd);
/* TODO - add init_connect command execution */
@ -714,9 +710,6 @@ void *create_embedded_thd(int client_flag)
thd->mysys_var= 0;
thd->reset_globals();
return thd;
err:
delete(thd);
return NULL;
}

3
plugin/feedback/sender_thread.cc

@ -92,8 +92,7 @@ static int prepare_for_fill(TABLE_LIST *tables)
thd->variables.pseudo_thread_id= thd->thread_id;
server_threads.insert(thd);
thd->thread_stack= (char*) &tables;
if (thd->store_globals())
return 1;
thd->store_globals();
thd->mysys_var->current_cond= &sleep_condition;
thd->mysys_var->current_mutex= &sleep_mutex;

4
plugin/handler_socket/handlersocket/database.cpp

@ -302,7 +302,7 @@ dbcontext::init_thread(const void *stack_bottom, volatile int& shutdown_flag)
thd->db.length= sizeof("handlersocket")-1;
}
thd->variables.option_bits |= OPTION_TABLE_LOCK;
my_pthread_setspecific_ptr(THR_THD, thd);
set_current_thd(thd);
DBG_THR(fprintf(stderr, "HNDSOCK x0 %p\n", thd));
}
{
@ -339,7 +339,7 @@ dbcontext::term_thread()
{
DBG_THR(fprintf(stderr, "HNDSOCK thread end %p\n", thd));
close_tables_if();
my_pthread_setspecific_ptr(THR_THD, 0);
set_current_thd(nullptr);
{
delete thd;
thd = 0;

3
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;
}

18
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;

8
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

11
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();

12
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;
}
/**

2
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()
{

18
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 (;;)
{

2
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,

5
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()
/*

5
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;

12
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();

2
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. */

4
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)

2
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.

2
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);
}
}

20
storage/spider/spd_conn.cc

@ -2519,7 +2519,7 @@ void *spider_bg_conn_action(
pthread_cond_signal(&conn->bg_conn_sync_cond);
pthread_mutex_unlock(&conn->bg_conn_sync_mutex);
#if !defined(MYSQL_DYNAMIC_PLUGIN) || !defined(_WIN32)
my_pthread_setspecific_ptr(THR_THD, NULL);
set_current_thd(nullptr);
#endif
my_thread_end();
DBUG_RETURN(NULL);
@ -2583,7 +2583,7 @@ void *spider_bg_conn_action(
pthread_mutex_unlock(&conn->bg_conn_mutex);
pthread_mutex_unlock(&conn->bg_conn_sync_mutex);
#if !defined(MYSQL_DYNAMIC_PLUGIN) || !defined(_WIN32)
my_pthread_setspecific_ptr(THR_THD, NULL);
set_current_thd(nullptr);
#endif
my_thread_end();
DBUG_RETURN(NULL);
@ -3035,7 +3035,7 @@ void *spider_bg_sts_action(
share->bg_sts_init = FALSE;
pthread_mutex_unlock(&share->sts_mutex);
#if !defined(MYSQL_DYNAMIC_PLUGIN) || !defined(_WIN32)
my_pthread_setspecific_ptr(THR_THD, NULL);
set_current_thd(nullptr);
#endif
my_thread_end();
my_afree(need_mons);
@ -3095,7 +3095,7 @@ void *spider_bg_sts_action(
share->bg_sts_init = FALSE;
pthread_mutex_unlock(&share->sts_mutex);
#if !defined(MYSQL_DYNAMIC_PLUGIN) || !defined(_WIN32)
my_pthread_setspecific_ptr(THR_THD, NULL);
set_current_thd(nullptr);
#endif
my_thread_end();
my_afree(need_mons);
@ -3124,7 +3124,7 @@ void *spider_bg_sts_action(
pthread_cond_signal(&share->bg_sts_sync_cond);
pthread_mutex_unlock(&share->sts_mutex);
#if !defined(MYSQL_DYNAMIC_PLUGIN) || !defined(_WIN32)
my_pthread_setspecific_ptr(THR_THD, NULL);
set_current_thd(nullptr);
#endif
my_thread_end();
my_afree(need_mons);
@ -3401,7 +3401,7 @@ void *spider_bg_crd_action(
share->bg_crd_init = FALSE;
pthread_mutex_unlock(&share->crd_mutex);
#if !defined(MYSQL_DYNAMIC_PLUGIN) || !defined(_WIN32)
my_pthread_setspecific_ptr(THR_THD, NULL);
set_current_thd(nullptr);
#endif
my_thread_end();
my_afree(need_mons);
@ -3465,7 +3465,7 @@ void *spider_bg_crd_action(
share->bg_crd_init = FALSE;
pthread_mutex_unlock(&share->crd_mutex);
#if !defined(MYSQL_DYNAMIC_PLUGIN) || !defined(_WIN32)
my_pthread_setspecific_ptr(THR_THD, NULL);
set_current_thd(nullptr);
#endif
my_thread_end();
my_afree(need_mons);
@ -3494,7 +3494,7 @@ void *spider_bg_crd_action(
pthread_cond_signal(&share->bg_crd_sync_cond);
pthread_mutex_unlock(&share->crd_mutex);
#if !defined(MYSQL_DYNAMIC_PLUGIN) || !defined(_WIN32)
my_pthread_setspecific_ptr(THR_THD, NULL);
set_current_thd(nullptr);
#endif
my_thread_end();
my_afree(need_mons);
@ -3893,7 +3893,7 @@ void *spider_bg_mon_action(
pthread_cond_signal(&share->bg_mon_conds[link_idx]);
pthread_mutex_unlock(&share->bg_mon_mutexes[link_idx]);
#if !defined(MYSQL_DYNAMIC_PLUGIN) || !defined(_WIN32)
my_pthread_setspecific_ptr(THR_THD, NULL);
set_current_thd(nullptr);
#endif
my_thread_end();
DBUG_RETURN(NULL);
@ -3932,7 +3932,7 @@ void *spider_bg_mon_action(
spider_free_trx(trx, TRUE);
delete thd;
#if !defined(MYSQL_DYNAMIC_PLUGIN) || !defined(_WIN32)
my_pthread_setspecific_ptr(THR_THD, NULL);
set_current_thd(nullptr);
#endif
my_thread_end();
DBUG_RETURN(NULL);

8
storage/spider/spd_table.cc

@ -9967,7 +9967,7 @@ void *spider_table_bg_sts_action(
thread->killed = FALSE;
pthread_mutex_unlock(&thread->mutex);
#if !defined(MYSQL_DYNAMIC_PLUGIN) || !defined(_WIN32)
my_pthread_setspecific_ptr(THR_THD, NULL);
set_current_thd(nullptr);
#endif
my_thread_end();
DBUG_RETURN(NULL);
@ -10034,7 +10034,7 @@ void *spider_table_bg_sts_action(
pthread_cond_signal(&thread->sync_cond);
pthread_mutex_unlock(&thread->mutex);
#if !defined(MYSQL_DYNAMIC_PLUGIN) || !defined(_WIN32)
my_pthread_setspecific_ptr(THR_THD, NULL);
set_current_thd(nullptr);
#endif
my_thread_end();
DBUG_RETURN(NULL);
@ -10166,7 +10166,7 @@ void *spider_table_bg_crd_action(
thread->killed = FALSE;
pthread_mutex_unlock(&thread->mutex);
#if !defined(MYSQL_DYNAMIC_PLUGIN) || !defined(_WIN32)
my_pthread_setspecific_ptr(THR_THD, NULL);
set_current_thd(nullptr);
#endif
my_thread_end();
DBUG_RETURN(NULL);
@ -10186,7 +10186,7 @@ void *spider_table_bg_crd_action(
pthread_cond_signal(&thread->sync_cond);
pthread_mutex_unlock(&thread->mutex);
#if !defined(MYSQL_DYNAMIC_PLUGIN) || !defined(_WIN32)
my_pthread_setspecific_ptr(THR_THD, NULL);
set_current_thd(nullptr);
#endif
my_thread_end();
DBUG_RETURN(NULL);

3
storage/spider/spd_trx.cc

@ -4173,8 +4173,7 @@ THD *spider_create_tmp_thd()
thd->thread_id = thd->variables.pseudo_thread_id = 0;
#endif
thd->thread_stack = (char*) &thd;
if (thd->store_globals())
DBUG_RETURN(NULL);
thd->store_globals();
lex_start(thd);
DBUG_RETURN(thd);
}

Loading…
Cancel
Save