cleanup: thread_count

move thread_count handling into THD:
* increment thread_count in THD constructor
* decrement thread_count in THD destructor
* never modify thread_count directly!
This commit is contained in:
Sergei Golubchik 2016-05-14 13:22:47 +02:00
parent 7425610c81
commit 562c1df7d9
14 changed files with 22 additions and 68 deletions

View file

@ -426,7 +426,6 @@ static void emb_free_embedded_thd(MYSQL *mysql)
THD *thd= (THD*)mysql->thd;
mysql_mutex_lock(&LOCK_thread_count);
thd->clear_data_list();
thread_count--;
thd->store_globals();
thd->unlink();
mysql_mutex_unlock(&LOCK_thread_count);
@ -700,7 +699,6 @@ void *create_embedded_thd(int client_flag)
bzero((char*) &thd->net, sizeof(thd->net));
mysql_mutex_lock(&LOCK_thread_count);
thread_count++;
threads.append(thd);
mysql_mutex_unlock(&LOCK_thread_count);
thd->mysys_var= 0;

View file

@ -91,7 +91,6 @@ static int prepare_for_fill(TABLE_LIST *tables)
*/
thd->variables.pseudo_thread_id= thd->thread_id;
mysql_mutex_lock(&LOCK_thread_count);
thread_count++;
threads.append(thd);
mysql_mutex_unlock(&LOCK_thread_count);
thd->thread_stack= (char*) &tables;
@ -263,7 +262,6 @@ ret:
*/
mysql_mutex_lock(&LOCK_thread_count);
thd->set_status_var_init();
thread_count--;
thd->killed= KILL_CONNECTION;
thd->unlink();
mysql_cond_broadcast(&COND_thread_count);

View file

@ -310,7 +310,6 @@ dbcontext::init_thread(const void *stack_bottom, volatile int& shutdown_flag)
}
{
thd->thread_id = next_thread_id();
thread_safe_increment32(&thread_count);
add_to_active_threads(thd);
}
@ -348,7 +347,6 @@ dbcontext::term_thread()
pthread_mutex_lock(&LOCK_thread_count);
delete thd;
thd = 0;
--thread_count;
pthread_mutex_unlock(&LOCK_thread_count);
my_thread_end();
}

View file

@ -134,7 +134,6 @@ post_init_event_thread(THD *thd)
return TRUE;
}
thread_safe_increment32(&thread_count);
add_to_active_threads(thd);
inc_thread_running();
return FALSE;

View file

@ -2906,7 +2906,6 @@ void delete_running_thd(THD *thd)
delete thd;
dec_thread_running();
dec_thread_count();
}
/*
@ -2916,14 +2915,6 @@ void delete_running_thd(THD *thd)
dec_thread_count()
*/
void dec_thread_count(void)
{
DBUG_ASSERT(thread_count > 0);
thread_safe_decrement32(&thread_count);
signal_thd_deleted();
}
/*
Send a signal to unblock close_conneciton() / rpl_slave_init_thread()
if there is no more threads running with a THD attached
@ -3116,7 +3107,6 @@ bool one_thread_per_connection_end(THD *thd, bool put_in_cache)
if (!wsrep_applier && put_in_cache && cache_thread(thd))
DBUG_RETURN(0); // Thread is reused
delete thd;
dec_thread_count();
}
DBUG_PRINT("info", ("killing thread"));
@ -6329,7 +6319,6 @@ static void bootstrap(MYSQL_FILE *file)
my_net_init(&thd->net,(st_vio*) 0, (void*) 0, MYF(0));
thd->max_client_packet_length= thd->net.max_packet;
thd->security_ctx->master_access= ~(ulong)0;
thread_count++; // Safe as only one thread running
in_bootstrap= TRUE;
bootstrap_file=file;
@ -6418,7 +6407,6 @@ void create_thread_to_handle_connection(CONNECT *connect)
/* Get thread from cache */
thread_cache.push_back(connect);
wake_thread++;
thread_safe_decrement32(&thread_count);
mysql_cond_signal(&COND_thread_cache);
mysql_mutex_unlock(&LOCK_thread_cache);
DBUG_PRINT("info",("Thread created"));
@ -6434,23 +6422,15 @@ void create_thread_to_handle_connection(CONNECT *connect)
if ((error= mysql_thread_create(key_thread_one_connection,
&connect->real_id, &connection_attrib,
handle_one_connection,
(void*) connect)))
handle_one_connection, (void*) connect)))
{
/* purecov: begin inspected */
DBUG_PRINT("error",
("Can't create thread to handle request (error %d)",
DBUG_PRINT("error", ("Can't create thread to handle request (error %d)",
error));
dec_connection_count(connect->scheduler);
statistic_increment(aborted_connects,&LOCK_status);
statistic_increment(connection_errors_internal, &LOCK_status);
my_snprintf(error_message_buff, sizeof(error_message_buff),
ER_DEFAULT(ER_CANT_CREATE_THREAD), error);
connect->close_with_error(ER_CANT_CREATE_THREAD,
error_message_buff,
connect->close_with_error(ER_CANT_CREATE_THREAD, error_message_buff,
ER_OUT_OF_RESOURCES);
/* thread_count was incremented in create_new_thread() */
dec_thread_count();
DBUG_VOID_RETURN;
/* purecov: end */
}
@ -6502,7 +6482,6 @@ static void create_new_thread(CONNECT *connect)
mysql_mutex_unlock(&LOCK_connection_count);
thread_safe_increment32(&thread_count);
connect->thread_count_incremented= 1;
/*

View file

@ -766,7 +766,6 @@ inline void dec_thread_running()
}
extern void set_server_version(void);
extern void dec_thread_count(void);
#if defined(MYSQL_DYNAMIC_PLUGIN) && defined(_WIN32)
extern "C" THD *_current_thd_noinline();

View file

@ -39,7 +39,6 @@ static bool no_threads_end(THD *thd, bool put_in_cache)
{
unlink_thd(thd);
delete thd;
dec_thread_count();
}
return 1; // Abort handle_one_connection
}

View file

@ -1077,6 +1077,7 @@ THD::THD(my_thread_id id, bool is_wsrep_applier)
save_prep_leaf_list= FALSE;
/* Restore THR_THD */
set_current_thd(old_THR_THD);
inc_thread_count();
}
@ -1766,6 +1767,7 @@ THD::~THD()
}
update_global_memory_status(status_var.global_memory_used);
set_current_thd(orig_thd == this ? 0 : orig_thd);
dec_thread_count();
DBUG_VOID_RETURN;
}

View file

@ -1906,6 +1906,20 @@ private:
inline bool is_conventional() const
{ DBUG_ASSERT(0); return Statement::is_conventional(); }
void dec_thread_count(void)
{
DBUG_ASSERT(thread_count > 0);
thread_safe_decrement32(const_cast<int32*>(&thread_count));
signal_thd_deleted();
}
void inc_thread_count(void)
{
thread_safe_increment32(const_cast<int32*>(&thread_count));
DBUG_ASSERT(!abort_loop);
}
public:
MDL_context mdl_context;

View file

@ -1403,14 +1403,7 @@ void CONNECT::close_and_delete()
if (vio)
vio_close(vio);
if (thread_count_incremented)
{
/*
Normally this is handled by THD::unlink. As we haven't yet created
a THD and put it in the thread list, we have to manage counting here.
*/
dec_thread_count();
dec_connection_count(scheduler);
}
statistic_increment(connection_errors_internal, &LOCK_status);
statistic_increment(aborted_connects,&LOCK_status);
@ -1434,23 +1427,8 @@ void CONNECT::close_with_error(uint sql_errno,
close_connection(thd, close_error);
delete thd;
set_current_thd(0);
if (thread_count_incremented)
{
dec_thread_count();
dec_connection_count(scheduler);
}
delete this;
statistic_increment(connection_errors_internal, &LOCK_status);
statistic_increment(aborted_connects,&LOCK_status);
}
else
{
/*
Out of memory; We can't generate an error, just close the connection
close_and_delete() will increment statistics.
*/
close_and_delete();
}
close_and_delete();
}

View file

@ -2073,7 +2073,6 @@ public:
delayed_lock= global_system_variables.low_priority_updates ?
TL_WRITE_LOW_PRIORITY : TL_WRITE;
mysql_mutex_unlock(&LOCK_thread_count);
thread_safe_increment32(&thread_count);
DBUG_VOID_RETURN;
}
~Delayed_insert()
@ -2104,7 +2103,6 @@ public:
my_free(thd.query());
thd.security_ctx->user= 0;
thd.security_ctx->host= 0;
dec_thread_count();
}
/* The following is for checking when we can delete ourselves */

View file

@ -1076,16 +1076,11 @@ void do_handle_bootstrap(THD *thd)
handle_bootstrap_impl(thd);
end:
in_bootstrap= FALSE;
delete thd;
#ifndef EMBEDDED_LIBRARY
DBUG_ASSERT(thread_count == 1);
in_bootstrap= FALSE;
/*
dec_thread_count will signal bootstrap() function that we have ended as
thread_count will become 0.
*/
dec_thread_count();
DBUG_ASSERT(thread_count == 0);
my_thread_end();
pthread_exit(0);
#endif

View file

@ -201,7 +201,6 @@ void threadpool_remove_connection(THD *thd)
close_connection(thd, 0);
unlink_thd(thd);
delete thd;
dec_thread_count();
/*
Free resources associated with this connection:

View file

@ -1778,7 +1778,6 @@ pthread_handler_t start_wsrep_THD(void *arg)
}
thd->real_id=pthread_self(); // Keep purify happy
thread_count++;
thread_created++;
threads.append(thd);
@ -1876,7 +1875,6 @@ pthread_handler_t start_wsrep_THD(void *arg)
thd->unlink();
mysql_mutex_unlock(&LOCK_thread_count);
delete thd;
dec_thread_count();
}
my_thread_end();
return(NULL);