From 8ebca190eac550e0e1ba9eec32a2386fde705b67 Mon Sep 17 00:00:00 2001 From: minqi_shen Date: Mon, 6 Jul 2020 20:01:09 +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 | 4 ++-- mysql-5.7.27/sql/rpl_gtid_state.cc | 2 +- mysql-5.7.27/sql/rpl_info.h | 7 +++---- 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, 35 insertions(+), 43 deletions(-) diff --git a/mysql-5.7.27/CMakeLists.txt b/mysql-5.7.27/CMakeLists.txt index e24e9081..5bf33c95 100644 --- a/mysql-5.7.27/CMakeLists.txt +++ b/mysql-5.7.27/CMakeLists.txt @@ -368,10 +368,6 @@ 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 9121846f..367b1c58 100644 --- a/mysql-5.7.27/cmake/build_configurations/compiler_options.cmake +++ b/mysql-5.7.27/cmake/build_configurations/compiler_options.cmake @@ -59,8 +59,7 @@ 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) - # 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") + SET(COMMON_CXX_FLAGS "${COMMON_CXX_FLAGS} -std=gnu++03") 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 17d704e4..48a9d5cd 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.load(), sig_wait, dsp_name)); + __atomic_load_n(&thd->killed,__ATOMIC_SEQ_CST), 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 4329fa60..a5df409e 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.load())); + DBUG_PRINT("info", ("thd->killed=%d", __atomic_load_n(&thd->killed,__ATOMIC_SEQ_CST))); goto end; } diff --git a/mysql-5.7.27/sql/filesort.cc b/mysql-5.7.27/sql/filesort.cc index d751bed1..680d8e06 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; - std::atomic *killed= &thd->killed; + volatile THD::killed_state *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; - std::atomic *killed= ¤t_thd->killed; - std::atomic not_killable{THD::NOT_KILLED}; + volatile THD::killed_state *killed= ¤t_thd->killed; + THD::killed_state not_killable; 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 77754cdf..cc2384fd 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.load()) + if (__atomic_load_n(&killing_thd->current_cond,__ATOMIC_SEQ_CST)) { 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 4398df8d..1516fee5 100644 --- a/mysql-5.7.27/sql/rpl_binlog_sender.cc +++ b/mysql-5.7.27/sql/rpl_binlog_sender.cc @@ -348,9 +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.store(DBUG_EVALUATE_IF("simulate_kill_dump", + __atomic_store_n(&m_thd->killed,(DBUG_EVALUATE_IF("simulate_kill_dump", THD::KILL_CONNECTION, - m_thd->killed.load())); + __atomic_load_n(&m_thd->killed,__ATOMIC_SEQ_CST))),__ATOMIC_SEQ_CST); 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 9f0101d1..b1b64fbb 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.load()) + switch (__atomic_load_n(&thd->killed,__ATOMIC_SEQ_CST)) { 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 6400f51f..d4ee7e3e 100644 --- a/mysql-5.7.27/sql/rpl_info.h +++ b/mysql-5.7.27/sql/rpl_info.h @@ -16,7 +16,6 @@ #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 @@ -64,9 +63,9 @@ public: THD *info_thd; bool inited; - std::atomic abort_slave; - std::atomic slave_running; - std::atomic slave_run_id; + volatile bool abort_slave; + volatile uint slave_running; + volatile ulong 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 c1a3d005..68bd4786 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 2dceb72b..b9dca369 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.load(), thd->is_error(), + worker->id,__atomic_load_n(&thd->killed,__ATOMIC_SEQ_CST),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 49daf3dd..985abc09 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, - std::atomic *slave_running, + volatile uint *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, - std::atomic *slave_running, + volatile uint *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, - std::atomic *slave_running, - std::atomic *slave_run_id, + volatile uint *slave_running, + volatile ulong *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 bb2bd454..42dfde18 100644 --- a/mysql-5.7.27/sql/rpl_slave.h +++ b/mysql-5.7.27/sql/rpl_slave.h @@ -20,7 +20,6 @@ #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; @@ -392,8 +391,8 @@ bool start_slave_thread( mysql_mutex_t *start_lock, mysql_mutex_t *cond_lock, mysql_cond_t *start_cond, - std::atomic *slave_running, - std::atomic *slave_run_id, + volatile uint *slave_running, + volatile ulong *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 fb631c85..29c4c23a 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.load()) { + switch (__atomic_load_n(&thd->killed,__ATOMIC_SEQ_CST)) { 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 a334540a..8dc5501a 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.load(), thd->is_slave_error, + err_status, __atomic_load_n(&thd->killed,__ATOMIC_SEQ_CST), 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 f90ac5d5..32bd0a94 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.load() && current_mutex.load()) + if (__atomic_load_n(¤t_cond,__ATOMIC_SEQ_CST) && __atomic_load_n(¤t_mutex,__ATOMIC_SEQ_CST)) { 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 86c019b7..29b7e7c3 100644 --- a/mysql-5.7.27/sql/sql_class.h +++ b/mysql-5.7.27/sql/sql_class.h @@ -48,7 +48,6 @@ #include #include -#include #include #include "mysql/thread_type.h" @@ -1937,7 +1936,7 @@ public: The mutex used with current_cond. @see current_cond */ - std::atomic current_mutex; + mysql_mutex_t * volatile current_mutex; /** Pointer to the condition variable the thread owning this THD is currently waiting for. If the thread is not waiting, the @@ -1947,7 +1946,7 @@ public: thread will broadcast on this condition variable so that the thread can be unstuck. */ - std::atomic current_cond; + mysql_cond_t * volatile current_cond; /** Condition variable used for waiting by the THR_LOCK.c subsystem. */ @@ -2891,7 +2890,7 @@ public: KILL_TIMEOUT=ER_QUERY_TIMEOUT, KILLED_NO_VALUE /* means neither of the states */ }; - std::atomic killed; + killed_state volatile killed; /* scramble - random string sent to client on handshake */ char scramble[SCRAMBLE_LENGTH+1]; @@ -3159,7 +3158,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.load()); + mysql_mutex_assert_not_owner(__atomic_load_n(¤t_mutex,__ATOMIC_SEQ_CST)); 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 9fd39cf6..c91986d9 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.load(); + killed_status= (local_error == 0)? THD::NOT_KILLED : __atomic_load_n(&thd->killed,__ATOMIC_SEQ_CST); /* 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 bd7482a0..194114e8 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.load(); + killed_status= (error == 0) ? THD::NOT_KILLED : __atomic_load_n(&thd->killed,__ATOMIC_SEQ_CST); #endif /* diff --git a/mysql-5.7.27/sql/sql_show.cc b/mysql-5.7.27/sql/sql_show.cc index 5a02f22b..8de140ff 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.load()) + else if (__atomic_load_n(&tmp->current_cond,__ATOMIC_SEQ_CST)) 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 65c1b1ef..ffdfe6b7 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.load(); + killed_status= (local_error == 0)? THD::NOT_KILLED : __atomic_load_n(&thd->killed,__ATOMIC_SEQ_CST); THD_STAGE_INFO(thd, stage_end); /* We must invalidate the query cache before binlog writing and -- Gitee