1 /* Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation; version 2 of the License.
6 
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11 
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15 
16 #ifndef MYSQLD_INCLUDED
17 #define MYSQLD_INCLUDED
18 
19 #include "my_global.h" /* MYSQL_PLUGIN_IMPORT, FN_REFLEN, FN_EXTLEN */
20 #include "sql_bitmap.h"                         /* Bitmap */
21 #include "my_decimal.h"                         /* my_decimal */
22 #include "mysql_com.h"                     /* SERVER_VERSION_LENGTH */
23 #include "my_atomic.h"                     /* my_atomic_rwlock_t */
24 #include "mysql/psi/mysql_file.h"          /* MYSQL_FILE */
25 #include "sql_list.h"                      /* I_List */
26 
27 class THD;
28 struct handlerton;
29 class Time_zone;
30 
31 struct scheduler_functions;
32 
33 typedef struct st_mysql_const_lex_string LEX_CSTRING;
34 typedef struct st_mysql_show_var SHOW_VAR;
35 
36 /*
37   This forward declaration is used from C files where the real
38   definition is included before.  Since C does not allow repeated
39   typedef declarations, even when identical, the definition may not be
40   repeated.
41 */
42 #ifndef CHARSET_INFO_DEFINED
43 typedef struct charset_info_st CHARSET_INFO;
44 #endif  /* CHARSET_INFO_DEFINED */
45 
46 #if MAX_INDEXES <= 64
47 typedef Bitmap<64>  key_map;          /* Used for finding keys */
48 #else
49 typedef Bitmap<((MAX_INDEXES+7)/8*8)> key_map; /* Used for finding keys */
50 #endif
51 
52 	/* Bits from testflag */
53 #define TEST_PRINT_CACHED_TABLES 1
54 #define TEST_NO_KEY_GROUP	 2
55 #define TEST_MIT_THREAD		4
56 #define TEST_BLOCKING		8
57 #define TEST_KEEP_TMP_TABLES	16
58 #define TEST_READCHECK		64	/**< Force use of readcheck */
59 #define TEST_NO_EXTRA		128
60 #define TEST_CORE_ON_SIGNAL	256	/**< Give core if signal */
61 #define TEST_NO_STACKTRACE	512
62 #define TEST_SIGINT		1024	/**< Allow sigint on threads */
63 #define TEST_SYNCHRONIZATION    2048    /**< get server to do sleep in
64                                            some places */
65 /* Function prototypes */
66 void kill_mysql(void);
67 void close_connection(THD *thd, uint sql_errno= 0);
68 void handle_connection_in_main_thread(THD *thd);
69 void create_thread_to_handle_connection(THD *thd);
70 void unlink_thd(THD *thd);
71 bool one_thread_per_connection_end(THD *thd, bool put_in_cache);
72 void flush_thread_cache();
73 void refresh_status(THD *thd);
74 bool is_secure_file_path(char *path);
75 
76 extern "C" MYSQL_PLUGIN_IMPORT CHARSET_INFO *system_charset_info;
77 extern MYSQL_PLUGIN_IMPORT CHARSET_INFO *files_charset_info ;
78 extern MYSQL_PLUGIN_IMPORT CHARSET_INFO *national_charset_info;
79 extern MYSQL_PLUGIN_IMPORT CHARSET_INFO *table_alias_charset;
80 
81 /**
82   Character set of the buildin error messages loaded from errmsg.sys.
83 */
84 extern CHARSET_INFO *error_message_charset_info;
85 
86 extern CHARSET_INFO *character_set_filesystem;
87 
88 extern MY_BITMAP temp_pool;
89 extern bool opt_large_files, server_id_supplied;
90 extern bool opt_update_log, opt_bin_log, opt_error_log;
91 extern my_bool opt_log, opt_slow_log;
92 extern my_bool opt_backup_history_log;
93 extern my_bool opt_backup_progress_log;
94 extern ulonglong log_output_options;
95 extern ulong log_backup_output_options;
96 extern my_bool opt_log_queries_not_using_indexes;
97 extern bool opt_disable_networking, opt_skip_show_db;
98 extern bool opt_skip_name_resolve;
99 extern bool opt_ignore_builtin_innodb;
100 extern my_bool opt_character_set_client_handshake;
101 extern bool volatile abort_loop;
102 extern bool in_bootstrap;
103 extern uint volatile thread_count;
104 extern uint connection_count;
105 extern my_bool opt_safe_user_create;
106 extern my_bool opt_safe_show_db, opt_local_infile, opt_myisam_use_mmap;
107 extern my_bool opt_slave_compressed_protocol, use_temp_pool;
108 extern ulong slave_exec_mode_options;
109 extern ulonglong slave_type_conversions_options;
110 extern my_bool read_only, opt_readonly;
111 extern my_bool lower_case_file_system;
112 extern my_bool opt_enable_named_pipe, opt_sync_frm, opt_allow_suspicious_udfs;
113 extern my_bool opt_secure_auth;
114 extern char* opt_secure_file_priv;
115 extern char* opt_secure_backup_file_priv;
116 extern size_t opt_secure_backup_file_priv_len;
117 extern my_bool opt_log_slow_admin_statements, opt_log_slow_slave_statements;
118 extern my_bool sp_automatic_privileges, opt_noacl;
119 extern my_bool opt_old_style_user_limits, trust_function_creators;
120 extern uint opt_crash_binlog_innodb;
121 extern char *shared_memory_base_name, *mysqld_unix_port;
122 extern my_bool opt_enable_shared_memory;
123 extern char *default_tz_name;
124 extern Time_zone *default_tz;
125 extern char *default_storage_engine;
126 extern bool opt_endinfo, using_udf_functions;
127 extern my_bool locked_in_memory;
128 extern bool opt_using_transactions;
129 extern ulong max_long_data_size;
130 extern ulong current_pid;
131 extern ulong expire_logs_days;
132 extern my_bool relay_log_recovery;
133 extern uint sync_binlog_period, sync_relaylog_period,
134             sync_relayloginfo_period, sync_masterinfo_period;
135 extern ulong opt_tc_log_size, tc_log_max_pages_used, tc_log_page_size;
136 extern ulong tc_log_page_waits;
137 extern my_bool relay_log_purge, opt_innodb_safe_binlog, opt_innodb;
138 extern my_bool relay_log_recovery;
139 extern uint test_flags,select_errors,ha_open_options;
140 extern uint protocol_version, mysqld_port, dropping_tables;
141 extern ulong delay_key_write_options;
142 extern char *opt_logname, *opt_slow_logname, *opt_bin_logname,
143             *opt_relay_logname;
144 extern char *opt_backup_history_logname, *opt_backup_progress_logname,
145             *opt_backup_settings_name;
146 extern const char *log_output_str;
147 extern const char *log_backup_output_str;
148 extern char *mysql_home_ptr, *pidfile_name_ptr;
149 extern char glob_hostname[FN_REFLEN], mysql_home[FN_REFLEN];
150 extern char pidfile_name[FN_REFLEN], system_time_zone[30], *opt_init_file;
151 extern char default_logfile_name[FN_REFLEN];
152 extern char log_error_file[FN_REFLEN], *opt_tc_log_file;
153 extern const double log_10[309];
154 extern ulonglong keybuff_size;
155 extern ulonglong thd_startup_options;
156 extern ulong thread_id;
157 extern ulong binlog_cache_use, binlog_cache_disk_use;
158 extern ulong binlog_stmt_cache_use, binlog_stmt_cache_disk_use;
159 extern ulong aborted_threads,aborted_connects;
160 extern ulong delayed_insert_timeout;
161 extern ulong delayed_insert_limit, delayed_queue_size;
162 extern ulong delayed_insert_threads, delayed_insert_writes;
163 extern ulong delayed_rows_in_use,delayed_insert_errors;
164 extern ulong slave_open_temp_tables;
165 extern ulong query_cache_size, query_cache_min_res_unit;
166 extern ulong slow_launch_threads, slow_launch_time;
167 extern ulong table_cache_size, table_def_size;
168 extern MYSQL_PLUGIN_IMPORT ulong max_connections;
169 extern ulong max_connect_errors, connect_timeout;
170 extern my_bool slave_allow_batching;
171 extern my_bool allow_slave_start;
172 extern LEX_CSTRING reason_slave_blocked;
173 extern ulong slave_trans_retries;
174 extern uint  slave_net_timeout;
175 extern uint max_user_connections;
176 extern ulong what_to_log,flush_time;
177 extern ulong max_prepared_stmt_count, prepared_stmt_count;
178 extern ulong open_files_limit;
179 extern ulong binlog_cache_size, binlog_stmt_cache_size;
180 extern ulonglong max_binlog_cache_size, max_binlog_stmt_cache_size;
181 extern ulong max_binlog_size, max_relay_log_size;
182 extern ulong slave_max_allowed_packet;
183 extern ulong opt_binlog_rows_event_max_size;
184 extern ulong rpl_recovery_rank, thread_cache_size;
185 extern ulong stored_program_cache_size;
186 extern ulong back_log;
187 extern char language[FN_REFLEN];
188 extern "C" MYSQL_PLUGIN_IMPORT ulong server_id;
189 extern ulong concurrency;
190 extern time_t server_start_time, flush_status_time;
191 extern char *opt_mysql_tmpdir, mysql_charsets_dir[];
192 extern int mysql_unpacked_real_data_home_len;
193 extern MYSQL_PLUGIN_IMPORT MY_TMPDIR mysql_tmpdir_list;
194 extern const char *first_keyword, *delayed_user, *binary_keyword;
195 extern MYSQL_PLUGIN_IMPORT const char  *my_localhost;
196 extern MYSQL_PLUGIN_IMPORT const char **errmesg;			/* Error messages */
197 extern const char *myisam_recover_options_str;
198 extern const char *in_left_expr_name, *in_additional_cond, *in_having_cond;
199 extern SHOW_VAR status_vars[];
200 extern struct system_variables max_system_variables;
201 extern struct system_status_var global_status_var;
202 extern struct rand_struct sql_rand;
203 extern const char *opt_date_time_formats[];
204 extern handlerton *partition_hton;
205 extern handlerton *myisam_hton;
206 extern handlerton *heap_hton;
207 extern const char *load_default_groups[];
208 extern struct my_option my_long_options[];
209 extern int mysqld_server_started;
210 extern "C" MYSQL_PLUGIN_IMPORT int orig_argc;
211 extern "C" MYSQL_PLUGIN_IMPORT char **orig_argv;
212 extern pthread_attr_t connection_attrib;
213 extern MYSQL_FILE *bootstrap_file;
214 extern my_bool old_mode;
215 extern LEX_STRING opt_init_connect, opt_init_slave;
216 extern int bootstrap_error;
217 extern I_List<THD> threads;
218 extern char err_shared_dir[];
219 extern TYPELIB thread_handling_typelib;
220 extern my_decimal decimal_zero;
221 void init_sql_statement_names();
222 
223 /*
224   THR_MALLOC is a key which will be used to set/get MEM_ROOT** for a thread,
225   using my_pthread_setspecific_ptr()/my_thread_getspecific_ptr().
226 */
227 extern pthread_key(MEM_ROOT**,THR_MALLOC);
228 
229 #ifdef HAVE_PSI_INTERFACE
230 #ifdef HAVE_MMAP
231 extern PSI_mutex_key key_PAGE_lock, key_LOCK_sync, key_LOCK_active,
232        key_LOCK_pool;
233 #endif /* HAVE_MMAP */
234 
235 #ifdef HAVE_OPENSSL
236 extern PSI_mutex_key key_LOCK_des_key_file;
237 #endif
238 
239 extern PSI_mutex_key key_BINLOG_LOCK_index, key_BINLOG_LOCK_prep_xids,
240   key_delayed_insert_mutex, key_hash_filo_lock, key_LOCK_active_mi,
241   key_LOCK_connection_count, key_LOCK_crypt, key_LOCK_delayed_create,
242   key_LOCK_delayed_insert, key_LOCK_delayed_status, key_LOCK_error_log,
243   key_LOCK_gdl, key_LOCK_global_system_variables,
244   key_LOCK_logger, key_LOCK_manager,
245   key_LOCK_prepared_stmt_count,
246   key_LOCK_rpl_status, key_LOCK_server_started, key_LOCK_status,
247   key_LOCK_table_share, key_LOCK_thd_data,
248   key_LOCK_user_conn, key_LOCK_uuid_generator, key_LOG_LOCK_log,
249   key_master_info_data_lock, key_master_info_run_lock,
250   key_master_info_sleep_lock,
251   key_mutex_slave_reporting_capability_err_lock, key_relay_log_info_data_lock,
252   key_relay_log_info_log_space_lock, key_relay_log_info_run_lock,
253   key_relay_log_info_sleep_lock,
254   key_structure_guard_mutex, key_TABLE_SHARE_LOCK_ha_data,
255   key_LOCK_error_messages, key_LOCK_thread_count, key_PARTITION_LOCK_auto_inc,
256   key_LOCK_thd_remove;
257 extern PSI_mutex_key key_RELAYLOG_LOCK_index;
258 
259 extern PSI_rwlock_key key_rwlock_LOCK_grant, key_rwlock_LOCK_logger,
260   key_rwlock_LOCK_sys_init_connect, key_rwlock_LOCK_sys_init_slave,
261   key_rwlock_LOCK_system_variables_hash, key_rwlock_query_cache_query_lock;
262 extern PSI_mutex_key key_LOCK_thread_created;
263 
264 #ifdef HAVE_MMAP
265 extern PSI_cond_key key_PAGE_cond, key_COND_active, key_COND_pool;
266 #endif /* HAVE_MMAP */
267 
268 extern PSI_cond_key key_BINLOG_COND_prep_xids, key_BINLOG_update_cond,
269   key_COND_cache_status_changed, key_COND_manager,
270   key_COND_rpl_status, key_COND_server_started,
271   key_delayed_insert_cond, key_delayed_insert_cond_client,
272   key_item_func_sleep_cond, key_master_info_data_cond,
273   key_master_info_start_cond, key_master_info_stop_cond,
274   key_master_info_sleep_cond,
275   key_relay_log_info_data_cond, key_relay_log_info_log_space_cond,
276   key_relay_log_info_start_cond, key_relay_log_info_stop_cond,
277   key_relay_log_info_sleep_cond,
278   key_TABLE_SHARE_cond, key_user_level_lock_cond,
279   key_COND_thread_count, key_COND_thread_cache, key_COND_flush_thread_cache;
280 extern PSI_cond_key key_RELAYLOG_update_cond;
281 
282 extern PSI_thread_key key_thread_bootstrap, key_thread_delayed_insert,
283   key_thread_handle_manager, key_thread_kill_server, key_thread_main,
284   key_thread_one_connection, key_thread_signal_hand;
285 
286 #ifdef HAVE_MMAP
287 extern PSI_file_key key_file_map;
288 #endif /* HAVE_MMAP */
289 
290 extern PSI_file_key key_file_binlog, key_file_binlog_index, key_file_casetest,
291   key_file_dbopt, key_file_des_key_file, key_file_ERRMSG, key_select_to_file,
292   key_file_fileparser, key_file_frm, key_file_global_ddl_log, key_file_load,
293   key_file_loadfile, key_file_log_event_data, key_file_log_event_info,
294   key_file_master_info, key_file_misc, key_file_partition,
295   key_file_pid, key_file_relay_log_info, key_file_send_file, key_file_tclog,
296   key_file_trg, key_file_trn, key_file_init;
297 extern PSI_file_key key_file_query_log, key_file_slow_log;
298 extern PSI_file_key key_file_relaylog, key_file_relaylog_index;
299 
300 void init_server_psi_keys();
301 #endif /* HAVE_PSI_INTERFACE */
302 
303 #ifndef __WIN__
304 extern pthread_t signal_thread;
305 #endif
306 
307 #ifdef HAVE_OPENSSL
308 extern struct st_VioSSLFd * ssl_acceptor_fd;
309 #endif /* HAVE_OPENSSL */
310 
311 /*
312   The following variables were under INNODB_COMPABILITY_HOOKS
313  */
314 extern my_bool opt_large_pages;
315 extern uint opt_large_page_size;
316 extern char lc_messages_dir[FN_REFLEN];
317 extern char *lc_messages_dir_ptr, *log_error_file_ptr;
318 extern MYSQL_PLUGIN_IMPORT char reg_ext[FN_EXTLEN];
319 extern MYSQL_PLUGIN_IMPORT uint reg_ext_length;
320 extern MYSQL_PLUGIN_IMPORT uint lower_case_table_names;
321 extern MYSQL_PLUGIN_IMPORT bool mysqld_embedded;
322 extern ulong specialflag;
323 extern uint mysql_data_home_len;
324 extern uint mysql_real_data_home_len;
325 extern const char *mysql_real_data_home_ptr;
326 extern ulong thread_handling;
327 extern MYSQL_PLUGIN_IMPORT char  *mysql_data_home;
328 extern "C" MYSQL_PLUGIN_IMPORT char server_version[SERVER_VERSION_LENGTH];
329 extern MYSQL_PLUGIN_IMPORT char mysql_real_data_home[];
330 extern char mysql_unpacked_real_data_home[];
331 extern MYSQL_PLUGIN_IMPORT struct system_variables global_system_variables;
332 extern char default_logfile_name[FN_REFLEN];
333 
334 #define mysql_tmpdir (my_tmpdir(&mysql_tmpdir_list))
335 
336 extern MYSQL_PLUGIN_IMPORT const key_map key_map_empty;
337 extern MYSQL_PLUGIN_IMPORT key_map key_map_full;          /* Should be threaded as const */
338 
339 /*
340   Server mutex locks and condition variables.
341  */
342 extern mysql_mutex_t
343        LOCK_user_locks, LOCK_status,
344        LOCK_error_log, LOCK_delayed_insert, LOCK_uuid_generator,
345        LOCK_delayed_status, LOCK_delayed_create, LOCK_crypt, LOCK_timezone,
346        LOCK_slave_list, LOCK_active_mi, LOCK_manager,
347        LOCK_global_system_variables, LOCK_user_conn,
348        LOCK_prepared_stmt_count, LOCK_error_messages, LOCK_connection_count;
349 extern MYSQL_PLUGIN_IMPORT mysql_mutex_t LOCK_thread_count;
350 extern MYSQL_PLUGIN_IMPORT mysql_mutex_t LOCK_thd_remove;
351 #ifdef HAVE_OPENSSL
352 extern char* des_key_file;
353 extern mysql_mutex_t LOCK_des_key_file;
354 #endif
355 extern mysql_mutex_t LOCK_server_started;
356 extern mysql_cond_t COND_server_started;
357 extern mysql_rwlock_t LOCK_grant, LOCK_sys_init_connect, LOCK_sys_init_slave;
358 extern mysql_rwlock_t LOCK_system_variables_hash;
359 extern mysql_cond_t COND_thread_count;
360 extern mysql_cond_t COND_manager;
361 extern int32 thread_running;
362 extern my_atomic_rwlock_t thread_running_lock;
363 
364 extern char *opt_ssl_ca, *opt_ssl_capath, *opt_ssl_cert, *opt_ssl_cipher,
365             *opt_ssl_key;
366 
367 extern MYSQL_PLUGIN_IMPORT pthread_key(THD*, THR_THD);
368 
369 /**
370   only options that need special treatment in get_one_option() deserve
371   to be listed below
372 */
373 enum options_mysqld
374 {
375   OPT_to_set_the_start_number=256,
376   OPT_BIND_ADDRESS,
377   OPT_BINLOG_DO_DB,
378   OPT_BINLOG_FORMAT,
379   OPT_BINLOG_IGNORE_DB,
380   OPT_BIN_LOG,
381   OPT_BOOTSTRAP,
382   OPT_CONSOLE,
383   OPT_DEBUG_SYNC_TIMEOUT,
384   OPT_DELAY_KEY_WRITE_ALL,
385   OPT_ISAM_LOG,
386   OPT_KEY_BUFFER_SIZE,
387   OPT_KEY_CACHE_AGE_THRESHOLD,
388   OPT_KEY_CACHE_BLOCK_SIZE,
389   OPT_KEY_CACHE_DIVISION_LIMIT,
390   OPT_LOWER_CASE_TABLE_NAMES,
391   OPT_ONE_THREAD,
392   OPT_POOL_OF_THREADS,
393   OPT_REPLICATE_DO_DB,
394   OPT_REPLICATE_DO_TABLE,
395   OPT_REPLICATE_IGNORE_DB,
396   OPT_REPLICATE_IGNORE_TABLE,
397   OPT_REPLICATE_REWRITE_DB,
398   OPT_REPLICATE_WILD_DO_TABLE,
399   OPT_REPLICATE_WILD_IGNORE_TABLE,
400   OPT_SAFE,
401   OPT_SERVER_ID,
402   OPT_SKIP_HOST_CACHE,
403   OPT_SKIP_LOCK,
404   OPT_SKIP_NEW,
405   OPT_SKIP_PRIOR,
406   OPT_SKIP_RESOLVE,
407   OPT_SKIP_STACK_TRACE,
408   OPT_SKIP_SYMLINKS,
409   OPT_SLOW_QUERY_LOG,
410   OPT_SSL_CA,
411   OPT_SSL_CAPATH,
412   OPT_SSL_CERT,
413   OPT_SSL_CIPHER,
414   OPT_SSL_KEY,
415   OPT_THREAD_CONCURRENCY,
416   OPT_UPDATE_LOG,
417   OPT_WANT_CORE,
418   OPT_ENGINE_CONDITION_PUSHDOWN,
419   OPT_LOG_ERROR,
420   OPT_MAX_LONG_DATA_SIZE
421 };
422 
423 
424 /**
425    Query type constants (usable as bitmap flags).
426 */
427 enum enum_query_type
428 {
429   /// Nothing specific, ordinary SQL query.
430   QT_ORDINARY= 0,
431   /// In utf8.
432   QT_TO_SYSTEM_CHARSET= (1 << 0),
433   /// Without character set introducers.
434   QT_WITHOUT_INTRODUCERS= (1 << 1),
435   /**
436     If an expression is constant, print the expression, not the value
437     it evaluates to. Should be used for error messages, so that they
438     don't reveal values.
439   */
440   QT_NO_DATA_EXPANSION= (1 << 9),
441 };
442 
443 /* query_id */
444 typedef int64 query_id_t;
445 extern query_id_t global_query_id;
446 extern my_atomic_rwlock_t global_query_id_lock;
447 
448 void unireg_end(void) __attribute__((noreturn));
449 
450 /* increment query_id and return it.  */
next_query_id()451 inline __attribute__((warn_unused_result)) query_id_t next_query_id()
452 {
453   query_id_t id;
454   my_atomic_rwlock_wrlock(&global_query_id_lock);
455   id= my_atomic_add64(&global_query_id, 1);
456   my_atomic_rwlock_wrunlock(&global_query_id_lock);
457   return (id+1);
458 }
459 
460 /*
461   TODO: Replace this with an inline function.
462  */
463 #ifndef EMBEDDED_LIBRARY
464 extern "C" void unireg_abort(int exit_code) __attribute__((noreturn));
465 #else
466 extern "C" void unireg_clear(int exit_code);
467 #define unireg_abort(exit_code) do { unireg_clear(exit_code); DBUG_RETURN(exit_code); } while(0)
468 #endif
469 
table_case_convert(char * name,uint length)470 inline void table_case_convert(char * name, uint length)
471 {
472   if (lower_case_table_names)
473     files_charset_info->cset->casedn(files_charset_info,
474                                      name, length, name, length);
475 }
476 
sql_rnd_with_mutex()477 inline ulong sql_rnd_with_mutex()
478 {
479   mysql_mutex_lock(&LOCK_thread_count);
480   ulong tmp=(ulong) (my_rnd(&sql_rand) * 0xffffffff); /* make all bits random */
481   mysql_mutex_unlock(&LOCK_thread_count);
482   return tmp;
483 }
484 
485 inline int32
inc_thread_running()486 inc_thread_running()
487 {
488   int32 num_thread_running;
489   my_atomic_rwlock_wrlock(&thread_running_lock);
490   num_thread_running= my_atomic_add32(&thread_running, 1);
491   my_atomic_rwlock_wrunlock(&thread_running_lock);
492   return (num_thread_running+1);
493 }
494 
495 inline int32
dec_thread_running()496 dec_thread_running()
497 {
498   int32 num_thread_running;
499   my_atomic_rwlock_wrlock(&thread_running_lock);
500   num_thread_running= my_atomic_add32(&thread_running, -1);
501   my_atomic_rwlock_wrunlock(&thread_running_lock);
502   return (num_thread_running-1);
503 }
504 
505 inline int32
get_thread_running()506 get_thread_running()
507 {
508   int32 num_thread_running;
509   my_atomic_rwlock_wrlock(&thread_running_lock);
510   num_thread_running= my_atomic_load32(&thread_running);
511   my_atomic_rwlock_wrunlock(&thread_running_lock);
512   return num_thread_running;
513 }
514 
515 #if defined(MYSQL_DYNAMIC_PLUGIN) && defined(_WIN32)
516 extern "C" THD *_current_thd_noinline();
517 #define _current_thd() _current_thd_noinline()
518 #else
519 /*
520   THR_THD is a key which will be used to set/get THD* for a thread,
521   using my_pthread_setspecific_ptr()/my_thread_getspecific_ptr().
522 */
523 extern pthread_key(THD*, THR_THD);
_current_thd(void)524 inline THD *_current_thd(void)
525 {
526   return my_pthread_getspecific_ptr(THD*,THR_THD);
527 }
528 #endif
529 #define current_thd _current_thd()
530 
531 #endif /* MYSQLD_INCLUDED */
532