From 3d4a7390c1a94ef6e07b04b52ea94a95878cda1b Mon Sep 17 00:00:00 2001 From: Monty Date: Mon, 1 Feb 2016 12:45:39 +0200 Subject: MDEV-6150 Speed up connection speed by moving creation of THD to new thread Creating a CONNECT object on client connect and pass this to the working thread which creates the THD. Split LOCK_thread_count to different mutexes Added LOCK_thread_start to syncronize threads Moved most usage of LOCK_thread_count to dedicated functions Use next_thread_id() instead of thread_id++ Other things: - Thread id now starts from 1 instead of 2 - Added cast for thread_id as thread id is now of type my_thread_id - Made THD->host const (To ensure it's not changed) - Removed some DBUG_PRINT() about entering/exiting mutex as these was already logged by mutex code - Fixed that aborted_connects and connection_errors_internal are counted in all cases - Don't take locks for current_linfo when we set it (not needed as it was 0 before) --- sql/sql_insert.cc | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'sql/sql_insert.cc') diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index e27cc6de073..fb020a9a302 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -2068,6 +2068,7 @@ 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() @@ -2081,17 +2082,24 @@ public: close_thread_tables(&thd); thd.mdl_context.release_transactional_locks(); } - mysql_mutex_lock(&LOCK_thread_count); mysql_mutex_destroy(&mutex); mysql_cond_destroy(&cond); mysql_cond_destroy(&cond_client); + + /* + We could use unlink_not_visible_threads() here, but as + delayed_insert_threads also needs to be protected by + the LOCK_thread_count mutex, we open code this. + */ + mysql_mutex_lock(&LOCK_thread_count); thd.unlink(); // Must be unlinked under lock - my_free(thd.query()); - thd.security_ctx->user= thd.security_ctx->host=0; delayed_insert_threads--; mysql_mutex_unlock(&LOCK_thread_count); - thread_safe_decrement32(&thread_count); - mysql_cond_broadcast(&COND_thread_count); /* Tell main we are ready */ + + 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 */ @@ -2226,8 +2234,6 @@ bool delayed_get_table(THD *thd, MDL_request *grl_protection_request, if (!(di= new Delayed_insert(thd->lex->current_select))) goto end_create; - thread_safe_increment32(&thread_count); - /* Annotating delayed inserts is not supported. */ @@ -2803,15 +2809,13 @@ pthread_handler_t handle_delayed_insert(void *arg) pthread_detach_this_thread(); /* Add thread to THD list so that's it's visible in 'show processlist' */ - mysql_mutex_lock(&LOCK_thread_count); - thd->thread_id= thd->variables.pseudo_thread_id= thread_id++; + thd->thread_id= thd->variables.pseudo_thread_id= next_thread_id(); thd->set_current_time(); - threads.append(thd); + add_to_active_threads(thd); if (abort_loop) thd->killed= KILL_CONNECTION; else thd->reset_killed(); - mysql_mutex_unlock(&LOCK_thread_count); mysql_thread_set_psi_id(thd->thread_id); -- cgit v1.2.1