MySQL 26.7.0
Source Code Documentation
thread_pool.h
Go to the documentation of this file.
1// Copyright (c) 2026, 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_SCHEDULER_THREAD_POOL_H
25#define MYSQL_SCHEDULER_THREAD_POOL_H
26
27#include <atomic>
28#include <condition_variable>
29#include <future>
30#include <mutex>
31#include <queue>
32#include <system_error>
33#include <vector>
41
42namespace mysql::scheduler {
43
44/// @brief MySQL wrapper for a condition variable, template which may be
45/// specialized with a specific implementation of a condition variable, e.g.
46/// MySQL condition variable, and satisfying the following requirements:
47template <class T, std::size_t t_queue_size = 8192>
49 public:
50 /// @brief Thread type
52 /// @brief Runnable task type, future is obtained by the caller
53 using Task_type = std::packaged_task<T(unsigned int)>;
54 /// @brief Queue type used to synchronize a thread supplying task to this
55 /// thread pool and worker threads. Worker threads are consumers of this queue
56 /// type
61
62 /// @brief Constructs a thread pool
63 /// @param thread_num Numbers of threads in a thread pool, set as default to
64 /// the number of concurrent threads supported by the implementation
65 /// @param instance_id THP will gather statistics for this instance id
66 /// @param psi_params PSI parameters
67 Thread_pool(unsigned int thread_num = std::thread::hardware_concurrency(),
68 unsigned int instance_id = 0, Thread_pool_psi psi_params = {});
69
70 /// @brief Initializes the thread pool by starting worker threads.
71 /// @retval false Success
72 /// @retval true Failure
73 [[nodiscard]] bool init();
74
75 /// @brief Deinitializes the thread pool and joins all worker threads.
76 void deinit();
77
78 /// @brief Enqueues task for execution
79 /// @param task Functor to be executed by the thread pool
80 void enqueue(Task_type &&task);
81
82 /// @brief Destructor
83 virtual ~Thread_pool();
84
85 /// @brief Notifies all threads to end execution in a gracious way.
86 /// When finished, Thread_pool object is ready to be destroyed.
87 /// This function is called in destructor.
88 void end_execution();
89
90 // disable copy-move semantics
92 Thread_pool &operator=(Thread_pool &&) noexcept = delete;
93 Thread_pool(const Thread_pool &) = delete;
94 Thread_pool &operator=(const Thread_pool &) = delete;
95
96 /// @brief Prints worker queue state, debug function
97 /// @return String with internal queue state
98 std::string print_queue_state() const;
99
100 /// @brief Get an estimation of number of elements in the worker queue
101 /// @return The number of elements in the worker queue
102 std::size_t queue_size() const;
103
104 /// @brief Obtains worker pool size
105 /// @return The number of workers available in the pool
106 std::size_t size() const;
107
108 private:
109 /// @brief Function executed by each thread
110 void run_worker(unsigned int thread_id);
111
112 /// @brief Checks whether worker thread construction failed.
113 /// @param thread Worker thread object.
114 /// @return True on failure, false otherwise.
115 static bool has_creation_error(const Thread &thread);
116
117 /// Thread container
119 /// Variable indicating the end of execution, allows threads to stop in
120 /// a gracious way
121 std::unordered_map<unsigned int, std::atomic<bool>> m_end_execution;
122 /// Enqueued, ready to be executed tasks
124 /// The number of threads in the thread pool
125 unsigned int m_workers_num;
126 /// @brief Instance id
127 unsigned int m_instance_id{0};
128 /// PSI parameters
130 /// Whether init() completed successfully.
131 bool m_initialized{false};
132};
133
134} // namespace mysql::scheduler
135
137
138#endif // MYSQL_SCHEDULER_THREAD_POOL_H
Allocator using a Memory_resource to do the allocation.
Definition: allocator.h:52
Bounded concurrent queue supporting multiple producers and consumers.
Definition: sync_bounded_queue.h:84
Wrapper to mysql thread, which matches interface of std::thread.
Definition: thread_srv.h:46
MySQL wrapper for a condition variable, template which may be specialized with a specific implementat...
Definition: thread_pool.h:48
void end_execution()
Notifies all threads to end execution in a gracious way.
Definition: thread_pool_impl.hpp:136
unsigned int m_instance_id
Instance id.
Definition: thread_pool.h:127
bool init()
Initializes the thread pool by starting worker threads.
Definition: thread_pool_impl.hpp:47
void deinit()
Deinitializes the thread pool and joins all worker threads.
Definition: thread_pool_impl.hpp:72
unsigned int m_workers_num
The number of threads in the thread pool.
Definition: thread_pool.h:125
mysql::concurrency::Stage_key St_key
Definition: thread_pool.h:58
Queue_type m_tasks
Enqueued, ready to be executed tasks.
Definition: thread_pool.h:123
bool m_initialized
Whether init() completed successfully.
Definition: thread_pool.h:131
void run_worker(unsigned int thread_id)
Function executed by each thread.
Definition: thread_pool_impl.hpp:99
std::vector< Thread, Thread_allocator > m_workers
Thread container.
Definition: thread_pool.h:118
Thread_pool(unsigned int thread_num=std::thread::hardware_concurrency(), unsigned int instance_id=0, Thread_pool_psi psi_params={})
Constructs a thread pool.
Definition: thread_pool_impl.hpp:31
Thread_pool_psi m_psi
PSI parameters.
Definition: thread_pool.h:129
std::size_t size() const
Obtains worker pool size.
Definition: thread_pool_impl.hpp:144
mysql::concurrency::Thread_key Th_key
Definition: thread_pool.h:59
std::size_t queue_size() const
Get an estimation of number of elements in the worker queue.
Definition: thread_pool_impl.hpp:94
std::unordered_map< unsigned int, std::atomic< bool > > m_end_execution
Variable indicating the end of execution, allows threads to stop in a gracious way.
Definition: thread_pool.h:121
virtual ~Thread_pool()
Destructor.
Definition: thread_pool_impl.hpp:131
std::packaged_task< T(unsigned int)> Task_type
Runnable task type, future is obtained by the caller.
Definition: thread_pool.h:53
std::string print_queue_state() const
Prints worker queue state, debug function.
Definition: thread_pool_impl.hpp:89
void enqueue(Task_type &&task)
Enqueues task for execution.
Definition: thread_pool_impl.hpp:84
Thread_pool(Thread_pool &&) noexcept=delete
static bool has_creation_error(const Thread &thread)
Checks whether worker thread construction failed.
Definition: thread_pool_impl.hpp:149
#define T
Definition: jit_executor_value.cc:373
static my_thread_id thread_id
Definition: my_thr_init.cc:60
PSI_stage_key Stage_key
Definition: stage_srv.h:35
std::thread Thread
Definition: thread_stl.h:42
PSI_thread_key Thread_key
Definition: thread_srv.h:43
Definition: base_dependency_tracker.h:41
noexcept
The return type for any call_and_catch(f, args...) call where f(args...) returns Type.
Definition: call_and_catch.h:76
Define std::hash<Gtid>.
Definition: gtid.h:355
std::unordered_map< Key, Value, Hash, Key_equal, ut::allocator< std::pair< const Key, Value > > > unordered_map
Definition: ut0new.h:2748
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2724
Instrumentation helpers for conditions.
Thread_pool instrumentation parameters, packed into this structure to simplify construction of a Thre...
Definition: thread_pool_psi.h:38