MySQL 9.1.0
Source Code Documentation
mysqld_thd_manager.h
Go to the documentation of this file.
1/* Copyright (c) 2013, 2024, Oracle and/or its affiliates.
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, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef MYSQLD_THD_MANAGER_INCLUDED
25#define MYSQLD_THD_MANAGER_INCLUDED
26
27#include <assert.h>
28#include <stddef.h>
29#include <sys/types.h>
30#include <atomic>
31
32#include "my_inttypes.h"
33#include "my_thread_local.h" // my_thread_id
36#include "prealloced_array.h"
37
38class THD;
39
42
43/**
44 Base class to perform actions on all thds in the thd list.
45 Users of do_for_all_thd() need to subclass this and override operator().
46
47 @note THD can be in the disposal state. Accessing resources freed in disposal
48 state is not safe. Before accessing any such resources in operator()
49 please check THD state.
50*/
51
53 public:
54 virtual ~Do_THD_Impl() = default;
55 virtual void operator()(THD *) = 0;
56};
57
58/**
59 Base class to find specific thd from the thd list.
60 Users of find_thd() need to subclass this and override operator()
61 to provide implementation to find thd from thd list.
62*/
63
65 public:
66 virtual ~Find_THD_Impl() = default;
67 /**
68 Override this operator to provide implementation to find specific thd.
69
70 @param thd THD of one element in global thread list
71
72 @returns bool
73 @retval true for matching thd
74 false otherwise
75 */
76 virtual bool operator()(THD *thd) = 0;
77};
78
79/**
80 Callback function used by kill_one_thread and timer_notify functions
81 to find "thd" based on the thread id.
82*/
84 public:
85 Find_thd_with_id(my_thread_id value, bool daemon_allowed = false)
86 : m_thread_id(value), m_daemon_allowed(daemon_allowed) {}
87 bool operator()(THD *thd) override;
88
90 const bool m_daemon_allowed;
91};
92
93/**
94 This class encapsulates THD instance, controls access to the actual THD.
95 It also ensures that THD::LOCK_thd_data mutex is acquired at instantiation and
96 released at destruction.
97*/
98class THD_ptr {
99 public:
100 /**
101 Default class constructor.
102 */
103 THD_ptr() = default;
104 /**
105 Constructor assigns THD instance to manage and acquires THD::LOCK_thd_data
106 mutex.
107 @param thd THD instance.
108 */
109 explicit THD_ptr(THD *thd);
110 /**
111 Delete copy constructor, THD_ptr copy is not allowed.
112 */
113 THD_ptr(THD_ptr const &) = delete;
114 /**
115 Move constructor.
116 @param thd_ptr THD_ptr instance to collect underlying THD instance.
117 */
118 THD_ptr(THD_ptr &&thd_ptr);
119
120 /**
121 Destructor to release underlying THD instance's control and release mutex
122 THD::LOCK_thd_data.
123 */
125 /**
126 Release underlying THD instance's control and release THD::LOCK_thd_data.
127 @returns underlying THD instance.
128 */
129 THD *release();
130
131 /**
132 Delete copy operator, THD_ptr copy is not allowed.
133 */
134 THD_ptr &operator=(THD_ptr const &) = delete;
135 /**
136 Move semantics assignment operator.
137 @param thd_ptr THD_ptr instance to collect underlying THD instance.
138 */
139 THD_ptr &operator=(THD_ptr &&thd_ptr);
140
141 /**
142 Access underlying THD instance.
143 returns pointer to underlying THD instance.
144 */
145 THD *get() { return m_underlying; }
146 /**
147 Access underlying THD instance.
148 returns pointer to underlying THD instance.
149 */
151 /**
152 Access underlying THD instance.
153 returns reference to underlying THD instance.
154 */
155 THD &operator*() { return *m_underlying; }
156
157 /**
158 Check if there is an underlying THD instance.
159 */
160 operator bool() const { return m_underlying != nullptr; }
161
162 /**
163 Compare underlying THD pointer value with the "nullptr".
164 @returns true if underlying THD pointer value equals "nullptr".
165 */
166 bool operator==(std::nullptr_t) const { return m_underlying == nullptr; }
167 /**
168 Compare this instance with other THD_ptr instance.
169 @param thd_ptr Other THD_ptr instance for comparison.
170 @returns true if this instance equals other THD_ptr instance.
171 */
172 bool operator==(THD const *thd_ptr) const { return m_underlying == thd_ptr; }
173 /**
174 Compare underlying THD pointer value with the "nullptr".
175 @returns true if underlying THD pointer value *not* equals nullptr.
176 */
177 bool operator!=(std::nullptr_t) const { return m_underlying != nullptr; }
178 /**
179 Compare this instance with other THD_ptr instance.
180 @param thd_ptr Other THD_ptr instance for comparison.
181 @returns true if this instance differs from other THD_ptr instance.
182 */
183 bool operator!=(THD const *thd_ptr) const { return m_underlying != thd_ptr; }
184
185 private:
186 // Underlying THD instance to manage.
187 THD *m_underlying{nullptr};
188};
189
190/**
191 This class maintains THD object of all registered threads.
192 It provides interface to perform functions such as find, count,
193 perform some action for each THD object in the list.
194
195 It also provide mutators for inserting, and removing an element:
196 add_thd() inserts a THD into the set, and increments the counter.
197 remove_thd() removes a THD from the set, and decrements the counter.
198 Method remove_thd() also broadcasts COND_thd_list.
199*/
200
202 public:
203 /**
204 Value for thread_id reserved for THDs which does not have an
205 assigned value yet. get_new_thread_id() will never return this
206 value.
207 */
209
210 /**
211 Retrieves singleton instance
212 */
214 assert(thd_manager != nullptr);
215 return thd_manager;
216 }
217
218 /**
219 Checks if the singleton is not already deinitialized
220 */
221 static bool is_initialized() { return thd_manager != nullptr; }
222
223 /**
224 Initializes the thd manager.
225 Must be called before get_instance() can be used.
226
227 @return true if initialization failed, false otherwise.
228 */
229 static bool create_instance();
230
231 /**
232 Destroys the singleton instance.
233 */
234 static void destroy_instance();
235
236 /**
237 Internally used to bypass code.
238 It enables unit test scripts to create dummy THD object for testing.
239 */
240 void set_unit_test() { unit_test = true; }
241
242 /**
243 Adds THD to global THD list.
244
245 @param thd THD object
246 */
247 void add_thd(THD *thd);
248
249 /**
250 Removes THD from global THD list.
251
252 @param thd THD object
253 */
254 void remove_thd(THD *thd);
255
256 /**
257 Retrieves thread running statistic variable.
258 @return int Returns the total number of threads currently running
259 */
261
262 /**
263 Increments thread running statistic variable.
264 */
266
267 /**
268 Decrements thread running statistic variable.
269 */
271
272 /**
273 Retrieves thread created statistic variable.
274 @return ulonglong Returns the total number of threads created
275 after server start
276 */
278
279 /**
280 Increments thread created statistic variable.
281 */
283
284 /**
285 Returns an unused thread id.
286 */
288
289 /**
290 Releases a thread id so that it can be reused.
291 Note that this is done automatically by remove_thd().
292 */
294
295 /**
296 Retrieves thread id counter value.
297 @return my_thread_id Returns the thread id counter value
298 @note This is a dirty read.
299 */
301
302 /**
303 Sets thread id counter value. Only used in testing for now.
304 @param new_id The next ID to hand out (if it's unused).
305 */
307
308 /**
309 Retrieves total number of items in global THD lists (all partitions).
310 @return uint Returns the count of items in global THD lists.
311 */
312 static uint get_thd_count() { return atomic_global_thd_count; }
313
314 /**
315 Waits until all THDs are removed from global THD lists (all partitions).
316 In other words, get_thd_count() to become zero.
317 */
318 void wait_till_no_thd();
319
320 /**
321 This function calls func() for all THDs in every thd list partition
322 after taking local copy of the THD list partition. It acquires
323 LOCK_thd_remove to prevent removal of the THD.
324 @param func Object of class which overrides operator()
325 */
327
328 /**
329 This function calls func() for all THDs in all THD list partitions.
330 @param func Object of class which overrides operator()
331 @note One list partition is unlocked before the next partition is locked.
332 */
333 void do_for_all_thd(Do_THD_Impl *func);
334
335 /**
336 * This function calls func() for all first "n" THDs across all THD list
337 * partitions.
338 * @param func Object of class which overrides operator()
339 * @param n number of elements we want to call func for
340 */
341 void do_for_first_n_thd(Do_THD_Impl *func, uint n);
342
343 /**
344 Returns a THD_ptr containing first THD for which operator() returns true.
345
346 @param func Object of class which overrides operator()
347 @return THD_ptr
348 @retval THD_ptr{THD*} When matching THD is found.
349 @retval THD_ptr{nullptr} When THD is *not* found.
350 */
352
354
355 // Declared static as it is referenced in handle_fatal_signal()
356 static std::atomic<uint> atomic_global_thd_count;
357
358 // Number of THD list partitions.
359 static const int NUM_PARTITIONS = 8;
360
361 private:
364
365 // Singleton instance.
367
368 // Array of current THDs. Protected by LOCK_thd_list.
371
372 // Array of thread ID in current use. Protected by LOCK_thread_ids.
375
377
378 // Mutexes that guard thd_list partitions
380 // Mutexes used to guard removal of elements from thd_list partitions.
382 // Mutex protecting thread_ids
384
385 // Count of active threads which are running queries in the system.
387
388 // Cumulative number of threads created by mysqld daemon.
389 std::atomic<ulonglong> atomic_thread_created;
390
391 // Counter to assign thread id.
393
394 // Used during unit test to bypass creating real THD object.
396
397 friend void thd_lock_thread_count();
398 friend void thd_unlock_thread_count();
399};
400
401#endif /* MYSQLD_INCLUDED */
Base class to perform actions on all thds in the thd list.
Definition: mysqld_thd_manager.h:52
virtual ~Do_THD_Impl()=default
virtual void operator()(THD *)=0
Base class to find specific thd from the thd list.
Definition: mysqld_thd_manager.h:64
virtual ~Find_THD_Impl()=default
virtual bool operator()(THD *thd)=0
Override this operator to provide implementation to find specific thd.
Callback function used by kill_one_thread and timer_notify functions to find "thd" based on the threa...
Definition: mysqld_thd_manager.h:83
bool operator()(THD *thd) override
Override this operator to provide implementation to find specific thd.
Definition: mysqld_thd_manager.cc:61
const bool m_daemon_allowed
Definition: mysqld_thd_manager.h:90
Find_thd_with_id(my_thread_id value, bool daemon_allowed=false)
Definition: mysqld_thd_manager.h:85
const my_thread_id m_thread_id
Definition: mysqld_thd_manager.h:89
This class maintains THD object of all registered threads.
Definition: mysqld_thd_manager.h:201
std::atomic< int > atomic_num_thread_running
Definition: mysqld_thd_manager.h:386
~Global_THD_manager()
Definition: mysqld_thd_manager.cc:191
void add_thd(THD *thd)
Adds THD to global THD list.
Definition: mysqld_thd_manager.cc:218
mysql_mutex_t LOCK_thd_remove[NUM_PARTITIONS]
Definition: mysqld_thd_manager.h:381
static std::atomic< uint > atomic_global_thd_count
Definition: mysqld_thd_manager.h:356
static Global_THD_manager * thd_manager
Definition: mysqld_thd_manager.h:366
void inc_thread_running()
Increments thread running statistic variable.
Definition: mysqld_thd_manager.h:265
void do_for_all_thd_copy(Do_THD_Impl *func)
This function calls func() for all THDs in every thd list partition after taking local copy of the TH...
Definition: mysqld_thd_manager.cc:291
my_thread_id thread_id_counter
Definition: mysqld_thd_manager.h:392
Prealloced_array< THD *, 60 > THD_array
Definition: mysqld_thd_manager.h:369
friend void thd_lock_thread_count()
Definition: mysqld_thd_manager.cc:371
mysql_mutex_t LOCK_thd_list[NUM_PARTITIONS]
Definition: mysqld_thd_manager.h:379
my_thread_id get_thread_id() const
Retrieves thread id counter value.
Definition: mysqld_thd_manager.h:300
void do_for_all_thd(Do_THD_Impl *func)
This function calls func() for all THDs in all THD list partitions.
Definition: mysqld_thd_manager.cc:314
void set_unit_test()
Internally used to bypass code.
Definition: mysqld_thd_manager.h:240
bool unit_test
Definition: mysqld_thd_manager.h:395
void release_thread_id(my_thread_id thread_id)
Releases a thread id so that it can be reused.
Definition: mysqld_thd_manager.cc:264
static const my_thread_id reserved_thread_id
Value for thread_id reserved for THDs which does not have an assigned value yet.
Definition: mysqld_thd_manager.h:208
int get_num_thread_running() const
Retrieves thread running statistic variable.
Definition: mysqld_thd_manager.h:260
Thread_id_array thread_ids
Definition: mysqld_thd_manager.h:374
void dec_thread_running()
Decrements thread running statistic variable.
Definition: mysqld_thd_manager.h:270
my_thread_id get_new_thread_id()
Returns an unused thread id.
Definition: mysqld_thd_manager.cc:255
static bool is_initialized()
Checks if the singleton is not already deinitialized.
Definition: mysqld_thd_manager.h:221
static void destroy_instance()
Destroys the singleton instance.
Definition: mysqld_thd_manager.cc:213
THD_array thd_list[NUM_PARTITIONS]
Definition: mysqld_thd_manager.h:370
void do_for_first_n_thd(Do_THD_Impl *func, uint n)
This function calls func() for all first "n" THDs across all THD list partitions.
Definition: mysqld_thd_manager.cc:322
void set_thread_id_counter(my_thread_id new_id)
Sets thread id counter value.
Definition: mysqld_thd_manager.cc:273
std::atomic< ulonglong > atomic_thread_created
Definition: mysqld_thd_manager.h:389
ulonglong get_num_thread_created() const
Retrieves thread created statistic variable.
Definition: mysqld_thd_manager.h:277
void wait_till_no_thd()
Waits until all THDs are removed from global THD lists (all partitions).
Definition: mysqld_thd_manager.cc:279
friend void thd_unlock_thread_count()
Definition: mysqld_thd_manager.cc:376
void remove_thd(THD *thd)
Removes THD from global THD list.
Definition: mysqld_thd_manager.cc:232
Global_THD_manager()
Definition: mysqld_thd_manager.cc:151
mysql_mutex_t LOCK_thread_ids
Definition: mysqld_thd_manager.h:383
static uint get_thd_count()
Retrieves total number of items in global THD lists (all partitions).
Definition: mysqld_thd_manager.h:312
void inc_thread_created()
Increments thread created statistic variable.
Definition: mysqld_thd_manager.h:282
static Global_THD_manager * get_instance()
Retrieves singleton instance.
Definition: mysqld_thd_manager.h:213
mysql_cond_t COND_thd_list[NUM_PARTITIONS]
Definition: mysqld_thd_manager.h:376
Prealloced_array< my_thread_id, 1000 > Thread_id_array
Definition: mysqld_thd_manager.h:373
static bool create_instance()
Initializes the thd manager.
Definition: mysqld_thd_manager.cc:207
static const int NUM_PARTITIONS
Definition: mysqld_thd_manager.h:359
THD_ptr find_thd(Find_THD_Impl *func)
Returns a THD_ptr containing first THD for which operator() returns true.
Definition: mysqld_thd_manager.cc:335
This class encapsulates THD instance, controls access to the actual THD.
Definition: mysqld_thd_manager.h:98
THD * get()
Access underlying THD instance.
Definition: mysqld_thd_manager.h:145
bool operator==(std::nullptr_t) const
Compare underlying THD pointer value with the "nullptr".
Definition: mysqld_thd_manager.h:166
~THD_ptr()
Destructor to release underlying THD instance's control and release mutex THD::LOCK_thd_data.
Definition: mysqld_thd_manager.h:124
THD_ptr(THD_ptr const &)=delete
Delete copy constructor, THD_ptr copy is not allowed.
bool operator!=(std::nullptr_t) const
Compare underlying THD pointer value with the "nullptr".
Definition: mysqld_thd_manager.h:177
THD * m_underlying
Definition: mysqld_thd_manager.h:187
THD * operator->()
Access underlying THD instance.
Definition: mysqld_thd_manager.h:150
THD_ptr()=default
Default class constructor.
THD & operator*()
Access underlying THD instance.
Definition: mysqld_thd_manager.h:155
bool operator!=(THD const *thd_ptr) const
Compare this instance with other THD_ptr instance.
Definition: mysqld_thd_manager.h:183
THD * release()
Release underlying THD instance's control and release THD::LOCK_thd_data.
Definition: mysqld_thd_manager.cc:120
bool operator==(THD const *thd_ptr) const
Compare this instance with other THD_ptr instance.
Definition: mysqld_thd_manager.h:172
THD_ptr & operator=(THD_ptr const &)=delete
Delete copy operator, THD_ptr copy is not allowed.
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
static my_thread_id thread_id
Definition: my_thr_init.cc:63
uint32 my_thread_id
Definition: my_thread_local.h:34
Instrumentation helpers for conditions.
ABI for instrumented mutexes.
void thd_lock_thread_count()
Definition: mysqld_thd_manager.cc:371
void thd_unlock_thread_count()
Definition: mysqld_thd_manager.cc:376
An instrumented cond structure.
Definition: mysql_cond_bits.h:50
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:50
uint32_t new_id()
Create a new (hopefully unique) ID.
Definition: xcom_base.cc:1786
int n
Definition: xcom_base.cc:509