| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1995, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/os0thread-create.h |
| 29 |
The interface to the threading wrapper |
| 30 |
|
| 31 |
Created 2016-May-17 Sunny Bains |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#ifndef os0thread_create_h |
| 35 |
#define os0thread_create_h |
| 36 |
|
| 37 |
#include <my_thread.h> |
| 38 |
|
| 39 |
#include "univ.i" |
| 40 |
|
| 41 |
#include "os0thread.h" |
| 42 |
#include "sql_thd_internal_api.h" |
| 43 |
|
| 44 |
#include <atomic> |
| 45 |
#include <functional> |
| 46 |
|
| 47 |
/** Maximum number of threads inside InnoDB */ |
| 48 |
extern uint32_t srv_max_n_threads; |
| 49 |
|
| 50 |
/** Number of threads active. */ |
| 51 |
extern std::atomic_int os_thread_count; |
| 52 |
|
| 53 |
/** Initializes OS thread management data structures. */ |
| 54 |
inline void os_thread_open() { /* No op */ |
| 55 |
} |
| 56 |
|
| 57 |
/** Check if there are threads active. |
| 58 |
@return true if the thread count > 0. */ |
| 59 |
inline bool os_thread_any_active() { |
| 60 |
return os_thread_count.load(std::memory_order_relaxed) > 0; |
| 61 |
} |
| 62 |
|
| 63 |
/** Frees OS thread management data structures. */ |
| 64 |
inline void os_thread_close() { |
| 65 |
if (os_thread_any_active()) { |
| 66 |
ib::warn(ER_IB_MSG_1274, os_thread_count.load(std::memory_order_relaxed)); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** Register with MySQL infrastructure. */ |
| 71 |
class MySQL_thread { |
| 72 |
public: |
| 73 |
#ifdef UNIV_PFS_THREAD |
| 74 |
/** Constructor for the Runnable object. |
| 75 |
@param[in] pfs_key Performance schema key |
| 76 |
@param[in] pfs_seqnum Performance schema sequence number */ |
| 77 |
explicit MySQL_thread(mysql_pfs_key_t pfs_key, PSI_thread_seqnum pfs_seqnum) |
| 78 |
: m_pfs_key(pfs_key), m_pfs_seqnum(pfs_seqnum) {} |
| 79 |
#else |
| 80 |
/** Constructor for the Runnable object. |
| 81 |
@param[in] pfs_key Performance schema key (ignored) |
| 82 |
@param[in] pfs_seqnum Performance schema sequence number */ |
| 83 |
explicit MySQL_thread(mysql_pfs_key_t, PSI_thread_seqnum) {} |
| 84 |
#endif /* UNIV_PFS_THREAD */ |
| 85 |
|
| 86 |
protected: |
| 87 |
/** Register the thread with the server */ |
| 88 |
void preamble() { |
| 89 |
const bool ret = my_thread_init(); |
| 90 |
ut_a(!ret); |
| 91 |
|
| 92 |
#if defined(UNIV_PFS_THREAD) && !defined(UNIV_HOTBACKUP) |
| 93 |
if (m_pfs_key.m_value != PFS_NOT_INSTRUMENTED.m_value) { |
| 94 |
auto &value = m_pfs_key.m_value; |
| 95 |
auto psi = PSI_THREAD_CALL(new_thread)(value, m_pfs_seqnum, this, 0); |
| 96 |
|
| 97 |
PSI_THREAD_CALL(set_thread_os_id)(psi); |
| 98 |
PSI_THREAD_CALL(set_thread)(psi); |
| 99 |
} |
| 100 |
#endif /* UNIV_PFS_THREAD && !UNIV_HOTBACKUP */ |
| 101 |
} |
| 102 |
|
| 103 |
/** Deregister the thread */ |
| 104 |
void epilogue() { |
| 105 |
my_thread_end(); |
| 106 |
|
| 107 |
#if defined(UNIV_PFS_THREAD) && !defined(UNIV_HOTBACKUP) |
| 108 |
if (m_pfs_key.m_value != PFS_NOT_INSTRUMENTED.m_value) { |
| 109 |
PSI_THREAD_CALL(delete_current_thread)(); |
| 110 |
} |
| 111 |
#endif /* UNIV_PFS_THREAD && !UNIV_HOTBACKUP */ |
| 112 |
} |
| 113 |
|
| 114 |
/** @return a THD instance. */ |
| 115 |
THD *create_mysql_thd() noexcept { |
| 116 |
#ifdef UNIV_PFS_THREAD |
| 117 |
return create_thd(false, true, true, m_pfs_key.m_value, m_pfs_seqnum); |
| 118 |
#else |
| 119 |
return create_thd(false, true, true, 0, 0); |
| 120 |
#endif /* UNIV_PFS_THREAD */ |
| 121 |
} |
| 122 |
|
| 123 |
/** Destroy a THD instance. |
| 124 |
@param[in,out] thd Instance to destroy. */ |
| 125 |
void destroy_mysql_thd(THD *thd) noexcept { destroy_thd(thd); } |
| 126 |
|
| 127 |
protected: |
| 128 |
#ifdef UNIV_PFS_THREAD |
| 129 |
/** Performance schema key */ |
| 130 |
const mysql_pfs_key_t m_pfs_key; |
| 131 |
|
| 132 |
/** Performance schema sequence number */ |
| 133 |
PSI_thread_seqnum m_pfs_seqnum; |
| 134 |
#endif /* UNIV_PFS_THREAD */ |
| 135 |
}; |
| 136 |
|
| 137 |
/** Execute in the context of a non detached MySQL thread. */ |
| 138 |
class Runnable : public MySQL_thread { |
| 139 |
public: |
| 140 |
/** Constructor for the Runnable object. |
| 141 |
@param[in] pfs_key Performance schema key |
| 142 |
@param[in] pfs_seqnum Performance schema sequence number */ |
| 143 |
explicit Runnable(mysql_pfs_key_t pfs_key, PSI_thread_seqnum pfs_seqnum) |
| 144 |
: MySQL_thread(pfs_key, pfs_seqnum) {} |
| 145 |
|
| 146 |
/** Method to execute the callable |
| 147 |
@param[in] f Callable object |
| 148 |
@param[in] args Variable number of args to F |
| 149 |
@retval f return value. */ |
| 150 |
template <typename F, typename... Args> |
| 151 |
dberr_t operator()(F &&f, Args &&...args) { |
| 152 |
MySQL_thread::preamble(); |
| 153 |
|
| 154 |
auto r = std::invoke(std::forward<F>(f), std::forward<Args>(args)...); |
| 155 |
|
| 156 |
MySQL_thread::epilogue(); |
| 157 |
|
| 158 |
return r; |
| 159 |
} |
| 160 |
}; |
| 161 |
|
| 162 |
/** Wrapper for a callable, it will count the number of registered |
| 163 |
Runnable instances and will register the thread executing the callable |
| 164 |
with the PFS and the Server threading infrastructure. */ |
| 165 |
class Detached_thread : public MySQL_thread { |
| 166 |
public: |
| 167 |
/** Constructor for the detached thread. |
| 168 |
@param[in] pfs_key Performance schema key |
| 169 |
@param[in] pfs_seqnum Performance schema sequence number */ |
| 170 |
explicit Detached_thread(mysql_pfs_key_t pfs_key, |
| 171 |
PSI_thread_seqnum pfs_seqnum) |
| 172 |
: MySQL_thread(pfs_key, pfs_seqnum) { |
| 173 |
init(); |
| 174 |
} |
| 175 |
|
| 176 |
/** Method to execute the callable |
| 177 |
@param[in] f Callable object |
| 178 |
@param[in] args Variable number of args to F */ |
| 179 |
template <typename F, typename... Args> |
| 180 |
void operator()(F &&f, Args &&...args) { |
| 181 |
while (m_thread.state() == IB_thread::State::NOT_STARTED) { |
| 182 |
UT_RELAX_CPU(); |
| 183 |
} |
| 184 |
|
| 185 |
ut_a(m_thread.state() == IB_thread::State::ALLOWED_TO_START); |
| 186 |
|
| 187 |
preamble(); |
| 188 |
|
| 189 |
m_thread.set_state(IB_thread::State::STARTED); |
| 190 |
|
| 191 |
std::invoke(std::forward<F>(f), std::forward<Args>(args)...); |
| 192 |
|
| 193 |
epilogue(); |
| 194 |
|
| 195 |
m_thread.set_state(IB_thread::State::STOPPED); |
| 196 |
} |
| 197 |
|
| 198 |
/** @return thread handle. */ |
| 199 |
IB_thread thread() const { return (m_thread); } |
| 200 |
|
| 201 |
private: |
| 202 |
/** Initializes the m_shared_future, uses the m_promise's get_future, |
| 203 |
which cannot be used since then, according to its documentation. */ |
| 204 |
void init() { m_thread.init(m_promise); } |
| 205 |
|
| 206 |
/** Register the thread with the server */ |
| 207 |
void preamble() { |
| 208 |
MySQL_thread::preamble(); |
| 209 |
|
| 210 |
std::atomic_thread_fence(std::memory_order_release); |
| 211 |
|
| 212 |
auto old = os_thread_count.fetch_add(1, std::memory_order_relaxed); |
| 213 |
|
| 214 |
ut_a(old <= static_cast<int>(srv_max_n_threads) - 1); |
| 215 |
} |
| 216 |
|
| 217 |
/** Deregister the thread */ |
| 218 |
void epilogue() { |
| 219 |
m_promise.set_value(); |
| 220 |
|
| 221 |
std::atomic_thread_fence(std::memory_order_release); |
| 222 |
|
| 223 |
auto old = os_thread_count.fetch_sub(1, std::memory_order_relaxed); |
| 224 |
|
| 225 |
ut_a(old > 0); |
| 226 |
|
| 227 |
MySQL_thread::epilogue(); |
| 228 |
} |
| 229 |
|
| 230 |
private: |
| 231 |
/** Future object which keeps the ref counter >= 1 at least |
| 232 |
as long as the Detached_thread is not-destroyed. */ |
| 233 |
mutable IB_thread m_thread; |
| 234 |
|
| 235 |
/** Promise which is set when task is done. */ |
| 236 |
std::promise<void> m_promise; |
| 237 |
}; |
| 238 |
|
| 239 |
/** Check if thread is stopped |
| 240 |
@param[in] thread Thread handle. |
| 241 |
@return true if the thread has started, finished tasks and stopped. */ |
| 242 |
inline bool thread_is_stopped(const IB_thread &thread) { |
| 243 |
return thread.state() == IB_thread::State::STOPPED; |
| 244 |
} |
| 245 |
|
| 246 |
/** Check if thread is active |
| 247 |
@param[in] thread Thread handle. |
| 248 |
@return true if the thread is active. */ |
| 249 |
inline bool thread_is_active(const IB_thread &thread) { |
| 250 |
switch (thread.state()) { |
| 251 |
case IB_thread::State::NOT_STARTED: |
| 252 |
/* Not yet started. */ |
| 253 |
return false; |
| 254 |
|
| 255 |
case IB_thread::State::ALLOWED_TO_START: |
| 256 |
/* Thread "thread" is already active, but start() has not been called. |
| 257 |
Note that when start() is called, the thread's routine may decide to |
| 258 |
check if it is active or trigger other thread to do similar check |
| 259 |
regarding "thread". That could happen faster than thread's state |
| 260 |
is advanced from ALLOWED_TO_START to STARTED. Therefore we must |
| 261 |
already consider such thread as "active". */ |
| 262 |
return true; |
| 263 |
|
| 264 |
case IB_thread::State::STARTED: |
| 265 |
/* Note, that potentially the thread might be doing its cleanup after |
| 266 |
it has already ended its task. We still consider it active, until the |
| 267 |
cleanup is finished. */ |
| 268 |
return true; |
| 269 |
|
| 270 |
case IB_thread::State::STOPPED: |
| 271 |
/* Ended its task and became marked as STOPPED (cleanup finished) */ |
| 272 |
return false; |
| 273 |
|
| 274 |
case IB_thread::State::INVALID: |
| 275 |
default: |
| 276 |
/* The thread object has not been assigned yet. */ |
| 277 |
return false; |
| 278 |
} |
| 279 |
|
| 280 |
/* Note that similar goal was achieved by the usage of shared_future: |
| 281 |
return (shared_future.valid() && shared_future.wait_for(std::chrono::seconds( |
| 282 |
0)) != std::future_status::ready); |
| 283 |
However this resulted in longer execution of mtr tests (63minutes -> |
| 284 |
75minutes). You could try `mtr --mem collations.esperanto` (cmake |
| 285 |
WITH_DEBUG=1) */ |
| 286 |
} |
| 287 |
|
| 288 |
/** Create a detached non-started thread. After thread is created, you should |
| 289 |
assign the received object to any of variables/fields which you later could |
| 290 |
access to check thread's state. You are allowed to either move or copy that |
| 291 |
object (any number of copies is allowed). After assigning you are allowed to |
| 292 |
start the thread by calling start() on any of those objects. |
| 293 |
@param[in] pfs_key Performance schema thread key |
| 294 |
@param[in] pfs_seqnum Performance schema thread sequence number |
| 295 |
@param[in] f Callable instance |
| 296 |
@param[in] args Zero or more args |
| 297 |
@return Object which allows to start the created thread, monitor its state and |
| 298 |
wait until the thread is finished. */ |
| 299 |
template <typename F, typename... Args> |
| 300 |
IB_thread create_detached_thread(mysql_pfs_key_t pfs_key, |
| 301 |
PSI_thread_seqnum pfs_seqnum, F &&f, |
| 302 |
Args &&...args) { |
| 303 |
Detached_thread detached_thread{pfs_key, pfs_seqnum}; |
| 304 |
auto thread = detached_thread.thread(); |
| 305 |
|
| 306 |
std::thread t(std::move(detached_thread), f, args...); |
| 307 |
t.detach(); |
| 308 |
|
| 309 |
/* Thread t is doing busy waiting until the state is changed |
| 310 |
from NOT_STARTED to ALLOWED_TO_START. That will happen when |
| 311 |
thread.start() will be called. */ |
| 312 |
ut_a(thread.state() == IB_thread::State::NOT_STARTED); |
| 313 |
|
| 314 |
return thread; |
| 315 |
} |
| 316 |
|
| 317 |
#ifdef UNIV_PFS_THREAD |
| 318 |
#define os_thread_create(...) create_detached_thread(__VA_ARGS__) |
| 319 |
#else |
| 320 |
#define os_thread_create(k, s, ...) create_detached_thread(0, 0, __VA_ARGS__) |
| 321 |
#endif /* UNIV_PFS_THREAD */ |
| 322 |
|
| 323 |
/** Parallel for loop over a container. |
| 324 |
@param[in] pfs_key Performance schema thread key |
| 325 |
@param[in] c Container to iterate over in parallel |
| 326 |
@param[in] n Number of threads to create |
| 327 |
@param[in] f Callable instance |
| 328 |
@param[in] args Zero or more args */ |
| 329 |
template <typename Container, typename F, typename... Args> |
| 330 |
void par_for(mysql_pfs_key_t pfs_key, const Container &c, size_t n, F &&f, |
| 331 |
Args &&...args) { |
| 332 |
if (c.empty()) { |
| 333 |
return; |
| 334 |
} |
| 335 |
|
| 336 |
size_t slice = (n > 0) ? c.size() / n : 0; |
| 337 |
|
| 338 |
using Workers = std::vector<IB_thread>; |
| 339 |
|
| 340 |
Workers workers; |
| 341 |
|
| 342 |
workers.reserve(n); |
| 343 |
|
| 344 |
for (size_t i = 0; i < n; ++i) { |
| 345 |
auto b = c.begin() + (i * slice); |
| 346 |
auto e = b + slice; |
| 347 |
|
| 348 |
auto worker = os_thread_create(pfs_key, i, f, b, e, i, args...); |
| 349 |
worker.start(); |
| 350 |
|
| 351 |
workers.push_back(std::move(worker)); |
| 352 |
} |
| 353 |
|
| 354 |
f(c.begin() + (n * slice), c.end(), n, args...); |
| 355 |
|
| 356 |
for (auto &worker : workers) { |
| 357 |
worker.join(); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
#if defined(UNIV_PFS_THREAD) && !defined(UNIV_HOTBACKUP) |
| 362 |
#define par_for(...) par_for(__VA_ARGS__) |
| 363 |
#else |
| 364 |
#define par_for(k, ...) par_for(0, __VA_ARGS__) |
| 365 |
#endif /* UNIV_PFS_THREAD */ |
| 366 |
|
| 367 |
#endif /* !os0thread_create_h */ |