From 6945cd1fcd3316d0841f06f6f8448f27dca30849 Mon Sep 17 00:00:00 2001 From: minqi_shen Date: Tue, 30 Jun 2020 20:33:47 +0800 Subject: [PATCH] USE STD::ATOMIC RATHER THAN VOLATILE WHEN INSPECTING STATUS OF OTHER THREADS --- mysql-5.7.27/CMakeLists.txt | 4 ++++ .../cmake/build_configurations/compiler_options.cmake | 3 ++- mysql-5.7.27/sql/debug_sync.cc | 2 +- mysql-5.7.27/sql/event_queue.cc | 2 +- mysql-5.7.27/sql/filesort.cc | 6 +++--- mysql-5.7.27/sql/mysqld.cc | 2 +- mysql-5.7.27/sql/rpl_binlog_sender.cc | 5 +++-- mysql-5.7.27/sql/rpl_gtid_state.cc | 2 +- mysql-5.7.27/sql/rpl_info.h | 8 ++++---- mysql-5.7.27/sql/rpl_rli.cc | 8 ++++---- mysql-5.7.27/sql/rpl_rli_pdb.cc | 2 +- mysql-5.7.27/sql/rpl_slave.cc | 8 ++++---- mysql-5.7.27/sql/rpl_slave.h | 5 +++-- mysql-5.7.27/sql/signal_handler.cc | 2 +- mysql-5.7.27/sql/sp_head.cc | 2 +- mysql-5.7.27/sql/sql_class.cc | 2 +- mysql-5.7.27/sql/sql_class.h | 9 +++++---- mysql-5.7.27/sql/sql_delete.cc | 2 +- mysql-5.7.27/sql/sql_load.cc | 2 +- mysql-5.7.27/sql/sql_show.cc | 2 +- mysql-5.7.27/sql/sql_update.cc | 2 +- 21 files changed, 44 insertions(+), 36 deletions(-) diff --git a/mysql-5.7.27/CMakeLists.txt b/mysql-5.7.27/CMakeLists.txt index 5bf33c95..e24e9081 100644 --- a/mysql-5.7.27/CMakeLists.txt +++ b/mysql-5.7.27/CMakeLists.txt @@ -368,6 +368,10 @@ ENDIF() # Always enable debug sync for debug builds. SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DENABLED_DEBUG_SYNC") SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DENABLED_DEBUG_SYNC") +# GNUCXX change standard from gnu++03 to gnu++11 in compiler_options.cmake +# Ignore the warning caused by GNUCXX standard change +SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wno-deprecated-declarations -Wno-virtual-move-assign") +SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -Wno-deprecated-declarations -Wno-virtual-move-assign") # Older versions of ccache must be disabled: export CCACHE_DISABLE=1 # See http://www.cmake.org/Wiki/CTest/Coverage diff --git a/mysql-5.7.27/cmake/build_configurations/compiler_options.cmake b/mysql-5.7.27/cmake/build_configurations/compiler_options.cmake index 367b1c58..9121846f 100644 --- a/mysql-5.7.27/cmake/build_configurations/compiler_options.cmake +++ b/mysql-5.7.27/cmake/build_configurations/compiler_options.cmake @@ -59,7 +59,8 @@ IF(UNIX) EXECUTE_PROCESS(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion OUTPUT_VARIABLE GXX_VERSION) IF(GXX_VERSION VERSION_EQUAL 6.0 OR GXX_VERSION VERSION_GREATER 6.0) - SET(COMMON_CXX_FLAGS "${COMMON_CXX_FLAGS} -std=gnu++03") + # GNUCXX standard change from gnu++03 to gnu++11,because std::atomic of c++11 is used. + SET(COMMON_CXX_FLAGS "${COMMON_CXX_FLAGS} -std=gnu++11") ENDIF() # Disable inline optimizations for valgrind testing to avoid false positives IF(WITH_VALGRIND) diff --git a/mysql-5.7.27/sql/debug_sync.cc b/mysql-5.7.27/sql/debug_sync.cc index 12f05451..17d704e4 100644 --- a/mysql-5.7.27/sql/debug_sync.cc +++ b/mysql-5.7.27/sql/debug_sync.cc @@ -2008,7 +2008,7 @@ static void debug_sync_execute(THD *thd, st_debug_sync_action *action) if (thd->killed) DBUG_PRINT("debug_sync_exec", ("killed %d from '%s' at: '%s'", - thd->killed, sig_wait, dsp_name)); + thd->killed.load(), sig_wait, dsp_name)); else DBUG_PRINT("debug_sync_exec", ("%s from '%s' at: '%s'", diff --git a/mysql-5.7.27/sql/event_queue.cc b/mysql-5.7.27/sql/event_queue.cc index 1735b7f9..4329fa60 100644 --- a/mysql-5.7.27/sql/event_queue.cc +++ b/mysql-5.7.27/sql/event_queue.cc @@ -563,7 +563,7 @@ Event_queue::get_top_for_execution_if_time(THD *thd, /* Break loop if thd has been killed */ if (thd->killed) { - DBUG_PRINT("info", ("thd->killed=%d", thd->killed)); + DBUG_PRINT("info", ("thd->killed=%d", thd->killed.load())); goto end; } diff --git a/mysql-5.7.27/sql/filesort.cc b/mysql-5.7.27/sql/filesort.cc index 680d8e06..d751bed1 100644 --- a/mysql-5.7.27/sql/filesort.cc +++ b/mysql-5.7.27/sql/filesort.cc @@ -874,7 +874,7 @@ static ha_rows find_all_keys(Sort_param *param, QEP_TAB *qep_tab, my_off_t record; TABLE *sort_form; THD *thd= current_thd; - volatile THD::killed_state *killed= &thd->killed; + std::atomic *killed= &thd->killed; handler *file; MY_BITMAP *save_read_set, *save_write_set; bool skip_record; @@ -2062,8 +2062,8 @@ int merge_buffers(Sort_param *param, IO_CACHE *from_file, Merge_chunk *merge_chunk; Sort_param::chunk_compare_fun cmp; Merge_chunk_compare_context *first_cmp_arg; - volatile THD::killed_state *killed= ¤t_thd->killed; - THD::killed_state not_killable; + std::atomic *killed= ¤t_thd->killed; + std::atomic not_killable{THD::NOT_KILLED}; DBUG_ENTER("merge_buffers"); current_thd->inc_status_sort_merge_passes(); diff --git a/mysql-5.7.27/sql/mysqld.cc b/mysql-5.7.27/sql/mysqld.cc index 0c4ab0f0..77754cdf 100644 --- a/mysql-5.7.27/sql/mysqld.cc +++ b/mysql-5.7.27/sql/mysqld.cc @@ -967,7 +967,7 @@ public: if (killing_thd->is_killable) { mysql_mutex_lock(&killing_thd->LOCK_current_cond); - if (killing_thd->current_cond) + if (killing_thd->current_cond.load()) { mysql_mutex_lock(killing_thd->current_mutex); mysql_cond_broadcast(killing_thd->current_cond); diff --git a/mysql-5.7.27/sql/rpl_binlog_sender.cc b/mysql-5.7.27/sql/rpl_binlog_sender.cc index 3b469a15..4398df8d 100644 --- a/mysql-5.7.27/sql/rpl_binlog_sender.cc +++ b/mysql-5.7.27/sql/rpl_binlog_sender.cc @@ -348,8 +348,9 @@ my_off_t Binlog_sender::send_binlog(IO_CACHE *log_cache, my_off_t start_pos) if (send_events(log_cache, end_pos)) return 1; - m_thd->killed= DBUG_EVALUATE_IF("simulate_kill_dump", THD::KILL_CONNECTION, - m_thd->killed); + m_thd->killed.store(DBUG_EVALUATE_IF("simulate_kill_dump", + THD::KILL_CONNECTION, + m_thd->killed.load())); DBUG_EXECUTE_IF("wait_after_binlog_EOF", { diff --git a/mysql-5.7.27/sql/rpl_gtid_state.cc b/mysql-5.7.27/sql/rpl_gtid_state.cc index 233d44dd..9f0101d1 100644 --- a/mysql-5.7.27/sql/rpl_gtid_state.cc +++ b/mysql-5.7.27/sql/rpl_gtid_state.cc @@ -403,7 +403,7 @@ bool Gtid_state::wait_for_gtid_set(THD *thd, Gtid_set* wait_for, if (thd->killed) { - switch (thd->killed) + switch (thd->killed.load()) { case ER_SERVER_SHUTDOWN: case ER_QUERY_INTERRUPTED: diff --git a/mysql-5.7.27/sql/rpl_info.h b/mysql-5.7.27/sql/rpl_info.h index bd9dd61d..6400f51f 100644 --- a/mysql-5.7.27/sql/rpl_info.h +++ b/mysql-5.7.27/sql/rpl_info.h @@ -16,13 +16,13 @@ #ifndef RPL_INFO_H #define RPL_INFO_H +#include #include "my_global.h" #include "mysql_com.h" // NAME_LEN #include "rpl_info_handler.h" // Rpl_info_handler #include "rpl_reporting.h" // Slave_reporting_capability #include "atomic_class.h" // Atomic_int32 - #define CHANNEL_NAME_LENGTH NAME_LEN class Rpl_info : public Slave_reporting_capability @@ -64,9 +64,9 @@ public: THD *info_thd; bool inited; - volatile bool abort_slave; - volatile uint slave_running; - volatile ulong slave_run_id; + std::atomic abort_slave; + std::atomic slave_running; + std::atomic slave_run_id; #ifndef DBUG_OFF int events_until_exit; diff --git a/mysql-5.7.27/sql/rpl_rli.cc b/mysql-5.7.27/sql/rpl_rli.cc index 68bd4786..c1a3d005 100644 --- a/mysql-5.7.27/sql/rpl_rli.cc +++ b/mysql-5.7.27/sql/rpl_rli.cc @@ -904,8 +904,8 @@ int Relay_log_info::wait_for_pos(THD* thd, String* log_name, err: mysql_mutex_unlock(&data_lock); thd->EXIT_COND(&old_stage); - DBUG_PRINT("exit",("killed: %d abort: %d slave_running: %d \ -improper_arguments: %d timed_out: %d", + DBUG_PRINT("exit",("killed: %d abort: %d slave_running: %d " + "improper_arguments: %d timed_out: %d", thd->killed_errno(), (int) (init_abort_pos_wait != abort_pos_wait), (int) slave_running, @@ -1069,8 +1069,8 @@ int Relay_log_info::wait_for_gtid_set(THD* thd, const Gtid_set* wait_gtid_set, mysql_mutex_unlock(&data_lock); thd->EXIT_COND(&old_stage); - DBUG_PRINT("exit",("killed: %d abort: %d slave_running: %d \ -improper_arguments: %d timed_out: %d", + DBUG_PRINT("exit",("killed: %d abort: %d slave_running: %d " + "improper_arguments: %d timed_out: %d", thd->killed_errno(), (int) (init_abort_pos_wait != abort_pos_wait), (int) slave_running, diff --git a/mysql-5.7.27/sql/rpl_rli_pdb.cc b/mysql-5.7.27/sql/rpl_rli_pdb.cc index 7f2766f5..2dceb72b 100644 --- a/mysql-5.7.27/sql/rpl_rli_pdb.cc +++ b/mysql-5.7.27/sql/rpl_rli_pdb.cc @@ -2732,7 +2732,7 @@ err: report_error_to_coordinator(worker); DBUG_PRINT("info", ("Worker %lu is exiting: killed %i, error %i, " "running_status %d", - worker->id, thd->killed, thd->is_error(), + worker->id, thd->killed.load(), thd->is_error(), worker->running_status)); worker->slave_worker_ends_group(ev, error); /* last done sets post exec */ } diff --git a/mysql-5.7.27/sql/rpl_slave.cc b/mysql-5.7.27/sql/rpl_slave.cc index 985abc09..49daf3dd 100644 --- a/mysql-5.7.27/sql/rpl_slave.cc +++ b/mysql-5.7.27/sql/rpl_slave.cc @@ -213,7 +213,7 @@ bool queue_event(Master_info* mi,const char* buf,ulong event_len); static int terminate_slave_thread(THD *thd, mysql_mutex_t *term_lock, mysql_cond_t *term_cond, - volatile uint *slave_running, + std::atomic *slave_running, ulong *stop_wait_timeout, bool need_lock_term); static bool check_io_slave_killed(THD *thd, Master_info *mi, const char *info); @@ -1795,7 +1795,7 @@ static int terminate_slave_thread(THD *thd, mysql_mutex_t *term_lock, mysql_cond_t *term_cond, - volatile uint *slave_running, + std::atomic *slave_running, ulong *stop_wait_timeout, bool need_lock_term) { @@ -1885,8 +1885,8 @@ bool start_slave_thread( my_start_routine h_func, mysql_mutex_t *start_lock, mysql_mutex_t *cond_lock, mysql_cond_t *start_cond, - volatile uint *slave_running, - volatile ulong *slave_run_id, + std::atomic *slave_running, + std::atomic *slave_run_id, Master_info* mi) { bool is_error= false; diff --git a/mysql-5.7.27/sql/rpl_slave.h b/mysql-5.7.27/sql/rpl_slave.h index 42dfde18..bb2bd454 100644 --- a/mysql-5.7.27/sql/rpl_slave.h +++ b/mysql-5.7.27/sql/rpl_slave.h @@ -20,6 +20,7 @@ #include "my_thread.h" // my_start_routine #include "mysql/psi/mysql_thread.h" // mysql_mutex_t #include "rpl_channel_service_interface.h" // enum_channel_type +#include class Log_event; class Master_info; @@ -391,8 +392,8 @@ bool start_slave_thread( mysql_mutex_t *start_lock, mysql_mutex_t *cond_lock, mysql_cond_t *start_cond, - volatile uint *slave_running, - volatile ulong *slave_run_id, + std::atomic *slave_running, + std::atomic *slave_run_id, Master_info *mi); /* retrieve table from master and copy to slave*/ diff --git a/mysql-5.7.27/sql/signal_handler.cc b/mysql-5.7.27/sql/signal_handler.cc index 1e6a236a..fb631c85 100644 --- a/mysql-5.7.27/sql/signal_handler.cc +++ b/mysql-5.7.27/sql/signal_handler.cc @@ -150,7 +150,7 @@ extern "C" void handle_fatal_signal(int sig) if (thd) { const char *kreason= "UNKNOWN"; - switch (thd->killed) { + switch (thd->killed.load()) { case THD::NOT_KILLED: kreason= "NOT_KILLED"; break; diff --git a/mysql-5.7.27/sql/sp_head.cc b/mysql-5.7.27/sql/sp_head.cc index c824499b..a334540a 100644 --- a/mysql-5.7.27/sql/sp_head.cc +++ b/mysql-5.7.27/sql/sp_head.cc @@ -926,7 +926,7 @@ bool sp_head::execute(THD *thd, bool merge_da_on_success) done: DBUG_PRINT("info", ("err_status: %d killed: %d is_slave_error: %d report_error: %d", - err_status, thd->killed, thd->is_slave_error, + err_status, thd->killed.load(), thd->is_slave_error, thd->is_error())); if (thd->killed) diff --git a/mysql-5.7.27/sql/sql_class.cc b/mysql-5.7.27/sql/sql_class.cc index 49ac2015..f90ac5d5 100644 --- a/mysql-5.7.27/sql/sql_class.cc +++ b/mysql-5.7.27/sql/sql_class.cc @@ -2097,7 +2097,7 @@ void THD::awake(THD::killed_state state_to_set) However, there is still a small chance of failure on platforms with instruction or memory write reordering. */ - if (current_cond && current_mutex) + if (current_cond.load() && current_mutex.load()) { DBUG_EXECUTE_IF("before_dump_thread_acquires_current_mutex", { diff --git a/mysql-5.7.27/sql/sql_class.h b/mysql-5.7.27/sql/sql_class.h index 1db14c77..86c019b7 100644 --- a/mysql-5.7.27/sql/sql_class.h +++ b/mysql-5.7.27/sql/sql_class.h @@ -48,6 +48,7 @@ #include #include +#include #include #include "mysql/thread_type.h" @@ -1936,7 +1937,7 @@ public: The mutex used with current_cond. @see current_cond */ - mysql_mutex_t * volatile current_mutex; + std::atomic current_mutex; /** Pointer to the condition variable the thread owning this THD is currently waiting for. If the thread is not waiting, the @@ -1946,7 +1947,7 @@ public: thread will broadcast on this condition variable so that the thread can be unstuck. */ - mysql_cond_t * volatile current_cond; + std::atomic current_cond; /** Condition variable used for waiting by the THR_LOCK.c subsystem. */ @@ -2890,7 +2891,7 @@ public: KILL_TIMEOUT=ER_QUERY_TIMEOUT, KILLED_NO_VALUE /* means neither of the states */ }; - killed_state volatile killed; + std::atomic killed; /* scramble - random string sent to client on handshake */ char scramble[SCRAMBLE_LENGTH+1]; @@ -3158,7 +3159,7 @@ public: locked (if that would not be the case, you'll get a deadlock if someone does a THD::awake() on you). */ - mysql_mutex_assert_not_owner(current_mutex); + mysql_mutex_assert_not_owner(current_mutex.load()); mysql_mutex_lock(&LOCK_current_cond); current_mutex= NULL; current_cond= NULL; diff --git a/mysql-5.7.27/sql/sql_delete.cc b/mysql-5.7.27/sql/sql_delete.cc index 9af7199c..9fd39cf6 100644 --- a/mysql-5.7.27/sql/sql_delete.cc +++ b/mysql-5.7.27/sql/sql_delete.cc @@ -1323,7 +1323,7 @@ bool Query_result_delete::send_eof() /* compute a total error to know if something failed */ local_error= local_error || error; - killed_status= (local_error == 0)? THD::NOT_KILLED : thd->killed; + killed_status= (local_error == 0)? THD::NOT_KILLED : thd->killed.load(); /* reset used flags */ THD_STAGE_INFO(thd, stage_end); diff --git a/mysql-5.7.27/sql/sql_load.cc b/mysql-5.7.27/sql/sql_load.cc index 3070de1b..bd7482a0 100644 --- a/mysql-5.7.27/sql/sql_load.cc +++ b/mysql-5.7.27/sql/sql_load.cc @@ -593,7 +593,7 @@ int mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list, };); #ifndef EMBEDDED_LIBRARY - killed_status= (error == 0) ? THD::NOT_KILLED : thd->killed; + killed_status= (error == 0) ? THD::NOT_KILLED : thd->killed.load(); #endif /* diff --git a/mysql-5.7.27/sql/sql_show.cc b/mysql-5.7.27/sql/sql_show.cc index 4fa9125f..5a02f22b 100644 --- a/mysql-5.7.27/sql/sql_show.cc +++ b/mysql-5.7.27/sql/sql_show.cc @@ -2200,7 +2200,7 @@ static const char *thread_state_info(THD *tmp) Mutex_lock lock(&tmp->LOCK_current_cond); if (tmp->proc_info) return tmp->proc_info; - else if (tmp->current_cond) + else if (tmp->current_cond.load()) return "Waiting on cond"; else return NULL; diff --git a/mysql-5.7.27/sql/sql_update.cc b/mysql-5.7.27/sql/sql_update.cc index f92e384c..65c1b1ef 100644 --- a/mysql-5.7.27/sql/sql_update.cc +++ b/mysql-5.7.27/sql/sql_update.cc @@ -2775,7 +2775,7 @@ bool Query_result_update::send_eof() if local_error is not set ON until after do_updates() then later carried out killing should not affect binlogging. */ - killed_status= (local_error == 0)? THD::NOT_KILLED : thd->killed; + killed_status= (local_error == 0)? THD::NOT_KILLED : thd->killed.load(); THD_STAGE_INFO(thd, stage_end); /* We must invalidate the query cache before binlog writing and -- Gitee