Skip to content

Commit

Permalink
cleanup: move thread_count to THD_count::value()
Browse files Browse the repository at this point in the history
because the name was misleading, it counts not threads, but THDs,
and as THD_count is the only way to increment/decrement it, it
could as well be declared inside THD_count.
  • Loading branch information
vuvova committed Jul 24, 2021
1 parent 1fb71c7 commit b34cafe
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 39 deletions.
4 changes: 2 additions & 2 deletions extra/mariabackup/xtrabackup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Street, Fifth Floor, Boston, MA 02110-1335 USA
#include <my_getopt.h>
#include <mysql_com.h>
#include <my_default.h>
#include <mysqld.h>
#include <sql_class.h>

#include <fcntl.h>
#include <string.h>
Expand Down Expand Up @@ -6199,7 +6199,7 @@ static bool xtrabackup_prepare_func(char** argv)
// See innobase_end() and thd_destructor_proxy()
while (srv_fast_shutdown == 0 &&
(trx_sys.any_active_transactions() ||
static_cast<uint>(thread_count) > srv_n_purge_threads + 1))
THD_count::value() > srv_n_purge_threads + 1))
os_thread_sleep(1000);

srv_shutdown_bg_undo_sources();
Expand Down
15 changes: 7 additions & 8 deletions sql/mysqld.cc
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ ulong delay_key_write_options;
uint protocol_version;
uint lower_case_table_names;
ulong tc_heuristic_recover= 0;
Atomic_counter<uint32_t> thread_count;
Atomic_counter<uint32_t> THD_count::count;
bool shutdown_wait_for_slaves;
Atomic_counter<uint32_t> slave_open_temp_tables;
ulong thread_created;
Expand Down Expand Up @@ -1745,9 +1745,9 @@ static void close_connections(void)
much smaller than even 2 seconds, this is only a safety fallback against
stuck threads so server shutdown is not held up forever.
*/
DBUG_PRINT("info", ("thread_count: %u", uint32_t(thread_count)));
DBUG_PRINT("info", ("THD_count: %u", THD_count::value()));

for (int i= 0; (thread_count - binlog_dump_thread_count) && i < 1000; i++)
for (int i= 0; (THD_count::value() - binlog_dump_thread_count) && i < 1000; i++)
my_sleep(20000);

if (global_system_variables.log_warnings)
Expand All @@ -1760,15 +1760,14 @@ static void close_connections(void)
}
#endif
/* All threads has now been aborted */
DBUG_PRINT("quit", ("Waiting for threads to die (count=%u)",
uint32_t(thread_count)));
DBUG_PRINT("quit", ("Waiting for threads to die (count=%u)", THD_count::value()));

while (thread_count - binlog_dump_thread_count)
while (THD_count::value() - binlog_dump_thread_count)
my_sleep(1000);

/* Kill phase 2 */
server_threads.iterate(kill_thread_phase_2);
for (uint64 i= 0; thread_count; i++)
for (uint64 i= 0; THD_count::value(); i++)
{
/*
This time the warnings are emitted within the loop to provide a
Expand Down Expand Up @@ -8037,7 +8036,7 @@ static int mysql_init_variables(void)
mqh_used= 0;
cleanup_done= 0;
select_errors= dropping_tables= ha_open_options=0;
thread_count= kill_cached_threads= wake_thread= 0;
THD_count::count= kill_cached_threads= wake_thread= 0;
slave_open_temp_tables= 0;
cached_thread_count= 0;
opt_endinfo= using_udf_functions= 0;
Expand Down
1 change: 0 additions & 1 deletion sql/mysqld.h
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,6 @@ extern mysql_rwlock_t LOCK_ssl_refresh;
extern mysql_prlock_t LOCK_system_variables_hash;
extern mysql_cond_t COND_start_thread;
extern mysql_cond_t COND_manager;
extern Atomic_counter<uint32_t> thread_count;

extern char *opt_ssl_ca, *opt_ssl_capath, *opt_ssl_cert, *opt_ssl_cipher,
*opt_ssl_key, *opt_ssl_crl, *opt_ssl_crlpath;
Expand Down
2 changes: 1 addition & 1 deletion sql/signal_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ extern "C" sig_handler handle_fatal_signal(int sig)
(uint) thread_scheduler->max_threads +
(uint) extra_max_connections);

my_safe_printf_stderr("thread_count=%u\n", (uint) thread_count);
my_safe_printf_stderr("thread_count=%u\n", THD_count::value());

if (dflt_key_cache && thread_scheduler)
{
Expand Down
33 changes: 17 additions & 16 deletions sql/sql_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,23 @@ static inline bool is_supported_parser_charset(CHARSET_INFO *cs)
return MY_TEST(cs->mbminlen == 1 && cs->number != 17 /* filename */);
}

/**
A counter of THDs
It must be specified as a first base class of THD, so that increment is
done before any other THD constructors and decrement - after any other THD
destructors.
Destructor unblocks close_conneciton() if there are no more THD's left.
*/
struct THD_count
{
static Atomic_counter<uint32_t> count;
static uint value() { return static_cast<uint>(count); }
THD_count() { count++; }
~THD_count() { count--; }
};

#ifdef MYSQL_SERVER

void free_tmp_table(THD *thd, TABLE *entry);
Expand Down Expand Up @@ -2155,22 +2172,6 @@ struct wait_for_commit
extern "C" void my_message_sql(uint error, const char *str, myf MyFlags);


/**
A wrapper around thread_count.
It must be specified as a first base class of THD, so that increment is
done before any other THD constructors and decrement - after any other THD
destructors.
Destructor unblocks close_conneciton() if there are no more THD's left.
*/
struct THD_count
{
THD_count() { thread_count++; }
~THD_count() { thread_count--; }
};


/**
@class THD
For each client connection we create a separate thread with THD serving as
Expand Down
8 changes: 3 additions & 5 deletions sql/sql_parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2232,15 +2232,13 @@ bool dispatch_command(enum enum_server_command command, THD *thd,
size_t length=
#endif
my_snprintf(buff, buff_len - 1,
"Uptime: %lu Threads: %d Questions: %lu "
"Uptime: %lu Threads: %u Questions: %lu "
"Slow queries: %lu Opens: %lu Flush tables: %lld "
"Open tables: %u Queries per second avg: %u.%03u",
uptime,
(int) thread_count, (ulong) thd->query_id,
uptime, THD_count::value(), (ulong) thd->query_id,
current_global_status_var->long_query_count,
current_global_status_var->opened_tables,
tdc_refresh_version(),
tc_records(),
tdc_refresh_version(), tc_records(),
(uint) (queries_per_second1000 / 1000),
(uint) (queries_per_second1000 % 1000));
#ifdef EMBEDDED_LIBRARY
Expand Down
8 changes: 3 additions & 5 deletions sql/wsrep_mysqld.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2511,15 +2511,13 @@ void wsrep_close_client_connections(my_bool wait_to_end, THD* except_caller_thd)
*/
server_threads.iterate(kill_remaining_threads, except_caller_thd);

DBUG_PRINT("quit", ("Waiting for threads to die (count=%u)",
uint32_t(thread_count)));
WSREP_DEBUG("waiting for client connections to close: %u",
uint32_t(thread_count));
DBUG_PRINT("quit", ("Waiting for threads to die (count=%u)", THD_count::value()));
WSREP_DEBUG("waiting for client connections to close: %u", THD_count::value());

while (wait_to_end && server_threads.iterate(have_client_connections))
{
sleep(1);
DBUG_PRINT("quit",("One thread died (count=%u)", uint32_t(thread_count)));
DBUG_PRINT("quit",("One thread died (count=%u)", THD_count::value()));
}

/* All client connection threads have now been aborted */
Expand Down
2 changes: 1 addition & 1 deletion storage/innobase/handler/ha_innodb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ thd_destructor_proxy(void *)

while (srv_fast_shutdown == 0 &&
(trx_sys.any_active_transactions() ||
(uint)thread_count > srv_n_purge_threads + 1)) {
THD_count::value() > srv_n_purge_threads + 1)) {
thd_proc_info(thd, "InnoDB slow shutdown wait");
os_thread_sleep(1000);
}
Expand Down

0 comments on commit b34cafe

Please sign in to comment.