MySQL 8.4.0
Source Code Documentation
mysql_thread.h
Go to the documentation of this file.
1/* Copyright (c) 2021, 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 MYSQL_THREAD_INCLUDE
25#define MYSQL_THREAD_INCLUDE
26
27#include <atomic>
31
32class THD;
33
34/**
35 @class Mysql_thread_body_parameters
36
37 Interface for Mysql_thread_body parameters.
38*/
40 public:
41 /*
42 Allocate memory on the heap with instrumented memory allocation, so
43 that memory consumption can be tracked.
44
45 @param[in] size memory size to be allocated
46 @param[in] nval When the nothrow constant is passed as second parameter
47 to operator new, operator new returns a null-pointer on
48 failure instead of throwing a bad_alloc exception.
49
50 @return pointer to the allocated memory, or NULL if memory could not
51 be allocated.
52 */
53 void *operator new(size_t size, const std::nothrow_t &) noexcept {
54 /*
55 Call my_malloc() with the MY_WME flag to make sure that it will
56 write an error message if the memory could not be allocated.
57 */
59 }
60
61 /*
62 Deallocate memory on the heap with instrumented memory allocation, so
63 that memory consumption can be tracked.
64
65 @param[in] ptr pointer to the allocated memory
66 @param[in] nval When the nothrow constant is passed as second parameter
67 to operator new, operator new returns a null-pointer on
68 failure instead of throwing a bad_alloc exception.
69 */
70 void operator delete(void *ptr, const std::nothrow_t &) noexcept {
71 my_free(ptr);
72 }
73
74 /**
75 Allocate memory on the heap with instrumented memory allocation, so
76 that memory consumption can be tracked.
77
78 @param[in] size memory size to be allocated
79
80 @return pointer to the allocated memory, or NULL if memory could not
81 be allocated.
82 */
83 void *operator new(size_t size) noexcept {
84 /*
85 Call my_malloc() with the MY_WME flag to make sure that it will
86 write an error message if the memory could not be allocated.
87 */
89 }
90
91 /**
92 Deallocate memory on the heap with instrumented memory allocation, so
93 that memory consumption can be tracked.
94
95 @param[in] ptr pointer to the allocated memory
96 */
97 void operator delete(void *ptr) noexcept { my_free(ptr); }
98
101};
102
103/**
104 @class Mysql_thread_body
105
106 Interface for Mysql_thread_body, the task of a Mysql_thread.
107*/
109 public:
111 virtual void run(Mysql_thread_body_parameters *parameters) = 0;
112};
113
115 public:
116 /*
117 Allocate memory on the heap with instrumented memory allocation, so
118 that memory consumption can be tracked.
119
120 @param[in] size memory size to be allocated
121 @param[in] nothrow When the nothrow constant is passed as second parameter
122 to operator new, operator new returns a null-pointer on
123 failure instead of throwing a bad_alloc exception.
124
125 @return pointer to the allocated memory, or NULL if memory could not
126 be allocated.
127 */
128 void *operator new(size_t size, const std::nothrow_t &) noexcept {
129 /*
130 Call my_malloc() with the MY_WME flag to make sure that it will
131 write an error message if the memory could not be allocated.
132 */
134 }
135
136 /*
137 Deallocate memory on the heap with instrumented memory allocation, so
138 that memory consumption can be tracked.
139
140 @param[in] ptr pointer to the allocated memory
141 @param[in] nothrow When the nothrow constant is passed as second parameter
142 to operator new, operator new returns a null-pointer on
143 failure instead of throwing a bad_alloc exception.
144 */
145 void operator delete(void *ptr, const std::nothrow_t &) noexcept {
146 my_free(ptr);
147 }
148
149 /**
150 Allocate memory on the heap with instrumented memory allocation, so
151 that memory consumption can be tracked.
152
153 @param[in] size memory size to be allocated
154
155 @return pointer to the allocated memory, or NULL if memory could not
156 be allocated.
157 */
158 void *operator new(size_t size) noexcept {
159 /*
160 Call my_malloc() with the MY_WME flag to make sure that it will
161 write an error message if the memory could not be allocated.
162 */
164 }
165
166 /**
167 Deallocate memory on the heap with instrumented memory allocation, so
168 that memory consumption can be tracked.
169
170 @param[in] ptr pointer to the allocated memory
171 */
172 void operator delete(void *ptr) noexcept { my_free(ptr); }
173
176 : m_body(body), m_parameters(parameters){};
178 delete m_parameters;
179 m_parameters = nullptr;
180 };
181
182 /**
183 Execute task, calling body function with parameters
184 */
185 void execute();
186
187 /**
188 Check if the task did finish.
189
190 @return did the task finish?
191 @retval false No
192 @retval true Yes
193 */
194 bool is_finished();
195
196 private:
197 // cannot be deleted, represent class where method will run
200 std::atomic<bool> m_finished{false};
201};
202
203/**
204 @class Mysql_thread
205
206 A generic single thread executor.
207*/
209 public:
210 /**
211 Mysql_thread constructor
212 */
213 Mysql_thread(PSI_thread_key thread_key, PSI_mutex_key run_mutex_key,
214 PSI_cond_key run_cond_key, PSI_mutex_key dispatcher_mutex_key,
215 PSI_cond_key dispatcher_cond_key);
216 virtual ~Mysql_thread();
217
218 /**
219 Initialize the thread.
220
221 @return the operation status
222 @retval false Successful
223 @retval true Error
224 */
225 bool initialize();
226
227 /**
228 Terminate the thread.
229
230 @return the operation status
231 @retval false Successful
232 @retval true Error
233 */
234 bool terminate();
235
236 /**
237 Thread worker method.
238 */
239 void dispatcher();
240
241 /**
242 Trigger a task to run synchronously.
243
244 @param[in] task task to run
245
246 @return the operation status
247 @retval false Successful
248 @retval true Error
249 */
250 bool trigger(Mysql_thread_task *task);
251
252 private:
258
259 THD *m_thd{nullptr};
264 std::atomic<bool> m_aborted{false};
265
268
270};
271
272#endif /* MYSQL_THREAD_INCLUDE */
Abortable synchronized queue extends synchronized queue allowing to abort methods waiting for element...
Definition: plugin_utils.h:262
Interface for Mysql_thread_body parameters.
Definition: mysql_thread.h:39
Mysql_thread_body_parameters()
Definition: mysql_thread.h:99
virtual ~Mysql_thread_body_parameters()
Definition: mysql_thread.h:100
Interface for Mysql_thread_body, the task of a Mysql_thread.
Definition: mysql_thread.h:108
virtual ~Mysql_thread_body()
Definition: mysql_thread.h:110
virtual void run(Mysql_thread_body_parameters *parameters)=0
Definition: mysql_thread.h:114
Mysql_thread_body_parameters * m_parameters
Definition: mysql_thread.h:199
Mysql_thread_body * m_body
Definition: mysql_thread.h:198
void execute()
Execute task, calling body function with parameters.
Definition: mysql_thread.cc:37
virtual ~Mysql_thread_task()
Definition: mysql_thread.h:177
Mysql_thread_task(Mysql_thread_body *body, Mysql_thread_body_parameters *parameters)
Definition: mysql_thread.h:174
bool is_finished()
Check if the task did finish.
Definition: mysql_thread.cc:42
std::atomic< bool > m_finished
Definition: mysql_thread.h:200
A generic single thread executor.
Definition: mysql_thread.h:208
my_thread_handle m_pthd
Definition: mysql_thread.h:260
mysql_cond_t m_run_cond
Definition: mysql_thread.h:262
std::atomic< bool > m_aborted
Definition: mysql_thread.h:264
PSI_mutex_key m_mutex_key
Definition: mysql_thread.h:254
PSI_thread_key m_thread_key
Definition: mysql_thread.h:253
mysql_mutex_t m_run_lock
Definition: mysql_thread.h:261
bool terminate()
Terminate the thread.
Definition: mysql_thread.cc:129
PSI_cond_key m_cond_key
Definition: mysql_thread.h:255
thread_state m_state
Definition: mysql_thread.h:263
bool trigger(Mysql_thread_task *task)
Trigger a task to run synchronously.
Definition: mysql_thread.cc:250
virtual ~Mysql_thread()
Definition: mysql_thread.cc:65
mysql_cond_t m_dispatcher_cond
Definition: mysql_thread.h:267
mysql_mutex_t m_dispatcher_lock
Definition: mysql_thread.h:266
Mysql_thread(PSI_thread_key thread_key, PSI_mutex_key run_mutex_key, PSI_cond_key run_cond_key, PSI_mutex_key dispatcher_mutex_key, PSI_cond_key dispatcher_cond_key)
Mysql_thread constructor.
Definition: mysql_thread.cc:44
bool initialize()
Initialize the thread.
Definition: mysql_thread.cc:82
PSI_mutex_key m_dispatcher_mutex_key
Definition: mysql_thread.h:256
PSI_cond_key m_dispatcher_cond_key
Definition: mysql_thread.h:257
Abortable_synchronized_queue< Mysql_thread_task * > * m_trigger_queue
Definition: mysql_thread.h:269
void dispatcher()
Thread worker method.
Definition: mysql_thread.cc:161
THD * m_thd
Definition: mysql_thread.h:259
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
#define MY_WME
Definition: my_sys.h:128
unsigned int PSI_cond_key
Instrumented cond key.
Definition: psi_cond_bits.h:44
unsigned int PSI_mutex_key
Instrumented mutex key.
Definition: psi_mutex_bits.h:52
unsigned int PSI_thread_key
Instrumented thread key.
Definition: psi_thread_bits.h:50
#define MYF(v)
Definition: my_inttypes.h:97
void * my_malloc(PSI_memory_key key, size_t size, int flags)
Allocates size bytes of memory.
Definition: my_memory.cc:57
void my_free(void *ptr)
Frees the memory pointed by the ptr.
Definition: my_memory.cc:81
size_t size(const char *const c)
Definition: base64.h:46
PSI_memory_key key_mysql_thread_queued_task
Definition: plugin_psi.h:242
Definition: my_thread_bits.h:58
An instrumented cond structure.
Definition: mysql_cond_bits.h:50
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:50
Definition: plugin_utils.h:48