MySQL 26.10.0
Source Code Documentation
io_thread.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2020, 2026, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_IO_THREAD_INCLUDED
27#define ROUTER_IO_THREAD_INCLUDED
28
29#include <cstddef> // size_t
30#include <functional>
31#include <future>
32#include <memory> // make_unique
33#include <string>
34#include <thread>
35#include <utility>
36
41
43 std::function<void(size_t)> on_start;
44 std::function<void(size_t)> on_stop;
45};
46
47class IoThread {
48 public:
49 IoThread(size_t ndx, std::bitset<ThreadAffinity::max_cpus> cpu_affinity,
50 const std::string &backend_name = "poll",
51 IoThreadLifecycle lifecycle = {}, std::string name = {})
52 : ndx_{ndx},
53 name_{name.empty() ? "io-" + std::to_string(ndx) : std::move(name)},
55 io_ctx_{std::make_unique<net::impl::socket::SocketService>(),
56 IoBackend::backend(backend_name)},
57 lifecycle_{std::move(lifecycle)},
59 thr_{&IoThread::operator(), this} {}
60
62 stop();
63 join();
64 }
65
66 net::io_context &context() noexcept { return io_ctx_; }
67
68 void operator()();
69
70 void wait_until_started() { startup_future_.get(); }
71
72 void stop() { io_ctx_.stop(); }
73
74 void join() {
75 if (thr_.joinable()) thr_.join();
76 }
77
78 std::bitset<ThreadAffinity::max_cpus> cpu_affinity() const {
79 return cpu_affinity_;
80 }
81
82 private:
83 size_t ndx_;
84 std::string name_;
85 std::bitset<ThreadAffinity::max_cpus> cpu_affinity_;
86
88
89 // Keep the callbacks alive before the worker starts and until it exits.
91 std::promise<void> startup_promise_;
92 std::future<void> startup_future_;
93 std::thread thr_;
94};
95
96#endif
Definition: io_backend.h:36
Definition: io_thread.h:47
IoThread(size_t ndx, std::bitset< ThreadAffinity::max_cpus > cpu_affinity, const std::string &backend_name="poll", IoThreadLifecycle lifecycle={}, std::string name={})
Definition: io_thread.h:49
size_t ndx_
Definition: io_thread.h:83
net::io_context io_ctx_
Definition: io_thread.h:87
void join()
Definition: io_thread.h:74
std::bitset< ThreadAffinity::max_cpus > cpu_affinity_
Definition: io_thread.h:85
std::promise< void > startup_promise_
Definition: io_thread.h:91
std::bitset< ThreadAffinity::max_cpus > cpu_affinity() const
Definition: io_thread.h:78
IoThreadLifecycle lifecycle_
Definition: io_thread.h:90
void wait_until_started()
Definition: io_thread.h:70
std::future< void > startup_future_
Definition: io_thread.h:92
std::string name_
Definition: io_thread.h:84
~IoThread()
Definition: io_thread.h:61
net::io_context & context() noexcept
Definition: io_thread.h:66
void stop()
Definition: io_thread.h:72
std::thread thr_
Definition: io_thread.h:93
Definition: io_context.h:61
static void stop(mysql_harness::PluginFuncEnv *)
Definition: mysql_rest_service_plugin.cc:413
Definition: http_server_component.cc:36
Unique_ptr< T, std::nullptr_t > make_unique(size_t size)
In-place constructs a new unique pointer with no specific allocator and with array type T.
std::string to_string(LogLevel level)
Definition: mrs_component_backend.h:47
noexcept
The return type for any call_and_catch(f, args...) call where f(args...) returns Type.
Definition: call_and_catch.h:76
std::string join(const detail::range auto &rng, std::string_view delim)
join elements of a range into a string separated by a delimiter.
Definition: string.h:74
stdx::expected< native_handle_type, error_type > socket(int family, int sock_type, int protocol)
Definition: socket.h:63
Definition: buffer.h:45
Define std::hash<Gtid>.
Definition: gtid.h:355
case opt name
Definition: sslopt-case.h:29
Definition: io_thread.h:42
std::function< void(size_t)> on_start
Definition: io_thread.h:43
std::function< void(size_t)> on_stop
Definition: io_thread.h:44