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