@@ -1157,6 +1157,17 @@ int PFS_status_variable_cache::do_materialize_global(void) {
11571157 false , /* threads */
11581158 true , /* THDs */
11591159 &visitor);
1160+
1161+ /*
1162+ Because of the reason described in
1163+ PFS_status_variable_cache::do_materialize_all(THD *unsafe_thd),
1164+ PFS_status_variable_cache::do_materialize_session(THD *unsafe_thd) and
1165+ PFS_status_variable_cache::do_materialize_session(PFS_thread *pfs_thread),
1166+ count_num_thread_running() cannot put together with
1167+ get_num_thread_running(), so count_num_thread_running() is put here.
1168+ */
1169+ Global_THD_manager::get_instance ()->count_num_thread_running ();
1170+
11601171 /*
11611172 Build the status variable cache using the SHOW_VAR array as a reference.
11621173 Use the status totals collected from all threads.
@@ -1200,6 +1211,22 @@ int PFS_status_variable_cache::do_materialize_all(THD *unsafe_thd) {
12001211 init_show_var_array (OPT_SESSION, false );
12011212 }
12021213
1214+ /*
1215+ count_num_thread_running() counts the total number of running threads
1216+ from global thread list, using LOCK_thd_list to protect sharded
1217+ global thread list. In lock_order_dependencies.txt, the lock order
1218+ is that LOCK_thd_list must be locked before LOCK_thd_data. In this
1219+ function, LOCK_thd_data is already locked in get_THD(), Then manifest()
1220+ will call get_num_thread_running(). If get_num_thread_running() counts
1221+ and returns the num, the lock order will be incorrect, which may
1222+ lead to dead lock. To prevent this situation, get_num_thread_running()
1223+ is split into two part, one is still called get_num_thread_running()
1224+ which returns the num, the other is called count_num_thread_running()
1225+ which counts the num and should be called before get_THD() and
1226+ get_num_thread_running(). So count_num_thread_running() is put here.
1227+ */
1228+ Global_THD_manager::get_instance ()->count_num_thread_running ();
1229+
12031230 /* Get and lock a validated THD from the thread manager. */
12041231 if ((m_safe_thd = get_THD (unsafe_thd)) != nullptr ) {
12051232 /*
@@ -1249,6 +1276,22 @@ int PFS_status_variable_cache::do_materialize_session(THD *unsafe_thd) {
12491276 init_show_var_array (OPT_SESSION, true );
12501277 }
12511278
1279+ /*
1280+ count_num_thread_running() counts the total number of running threads
1281+ from global thread list, using LOCK_thd_list to protect sharded
1282+ global thread list. In lock_order_dependencies.txt, the lock order
1283+ is that LOCK_thd_list must be locked before LOCK_thd_data. In this
1284+ function, LOCK_thd_data is already locked in get_THD(), Then manifest()
1285+ will call get_num_thread_running(). If get_num_thread_running() counts
1286+ and returns the num, the lock order will be incorrect, which may
1287+ lead to dead lock. To prevent this situation, get_num_thread_running()
1288+ is split into two part, one is still called get_num_thread_running()
1289+ which returns the num, the other is called count_num_thread_running()
1290+ which counts the num and should be called before get_THD() and
1291+ get_num_thread_running(). So count_num_thread_running() is put here.
1292+ */
1293+ Global_THD_manager::get_instance ()->count_num_thread_running ();
1294+
12521295 /* Get and lock a validated THD from the thread manager. */
12531296 if ((m_safe_thd = get_THD (unsafe_thd)) != nullptr ) {
12541297 /*
@@ -1292,6 +1335,22 @@ int PFS_status_variable_cache::do_materialize_session(PFS_thread *pfs_thread) {
12921335 /* The SHOW_VAR array must be initialized externally. */
12931336 assert (m_initialized);
12941337
1338+ /*
1339+ count_num_thread_running() counts the total number of running threads
1340+ from global thread list, using LOCK_thd_list to protect sharded
1341+ global thread list. In lock_order_dependencies.txt, the lock order
1342+ is that LOCK_thd_list must be locked before LOCK_thd_data. In this
1343+ function, LOCK_thd_data is already locked in get_THD(), Then manifest()
1344+ will call get_num_thread_running(). If get_num_thread_running() counts
1345+ and returns the num, the lock order will be incorrect, which may
1346+ lead to dead lock. To prevent this situation, get_num_thread_running()
1347+ is split into two part, one is still called get_num_thread_running()
1348+ which returns the num, the other is called count_num_thread_running()
1349+ which counts the num and should be called before get_THD() and
1350+ get_num_thread_running(). So count_num_thread_running() is put here.
1351+ */
1352+ Global_THD_manager::get_instance ()->count_num_thread_running ();
1353+
12951354 /* Get and lock a validated THD from the thread manager. */
12961355 if ((m_safe_thd = get_THD (pfs_thread)) != nullptr ) {
12971356 /*
@@ -1343,6 +1402,16 @@ int PFS_status_variable_cache::do_materialize_client(PFS_client *pfs_client) {
13431402 */
13441403 m_sum_client_status (pfs_client, &status_totals);
13451404
1405+ /*
1406+ Because of the reason described in
1407+ PFS_status_variable_cache::do_materialize_all(THD *unsafe_thd),
1408+ PFS_status_variable_cache::do_materialize_session(THD *unsafe_thd) and
1409+ PFS_status_variable_cache::do_materialize_session(PFS_thread *pfs_thread),
1410+ count_num_thread_running() cannot put together with
1411+ get_num_thread_running(), so count_num_thread_running() is put here.
1412+ */
1413+ Global_THD_manager::get_instance ()->count_num_thread_running ();
1414+
13461415 /*
13471416 Build the status variable cache using the SHOW_VAR array as a reference and
13481417 the status totals collected from threads associated with this client.
0 commit comments