MySQL 8.4.13
Source Code Documentation
io_component.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_COMPONENT_INCLUDED
27#define ROUTER_IO_COMPONENT_INCLUDED
28
29#include <list>
30#include <memory> // make_unique
31#include <string>
32
36
38
39enum class IoComponentErrc {
42};
43
45
47 public:
48 std::list<IoThread> &io_threads() { return io_threads_; }
49
50 /**
51 * get ref to the io_context.
52 *
53 * behaviour is undefined when called before IoComponent::init() and after
54 * IoComponent::reset()
55 */
56 net::io_context &io_context();
57
58 /**
59 * get ref to the io_context dedicated to socket acceptors.
60 *
61 * Accepted client sockets are still assigned to io_threads().
62 *
63 * behaviour is undefined when called before IoComponent::init() and after
64 * IoComponent::reset()
65 */
66 net::io_context &acceptor_io_context();
67
68 static IoComponent &get_instance();
69
70 /**
71 * initialize io-component.
72 *
73 * use IoComponent::reset() to reset the io-component into its initial state
74 *
75 * @param num_worker_threads number of worker-threads to spawn
76 * @param backend_name name of the io-backend
77 *
78 * @see IoComponent::reset()
79 */
80 stdx::expected<void, std::error_code> init(size_t num_worker_threads,
81 const std::string &backend_name);
82
83 /**
84 * run the main loop of the io-component.
85 *
86 * runs until all no more work is assigned to the mainloop or stopped.
87 *
88 * @see IoComponent::stop()
89 */
90 void run();
91
92 void stop();
93
94 /**
95 * reset the io_component into its initial state.
96 *
97 * when calling reset() no io-thread SHALL run which can be achieved by
98 *
99 * - calling stop() after run() was called.
100 * - not calling run()
101 */
102 void reset();
103
104 std::string backend_name() const { return backend_name_; }
105
106 class Workguard {
107 public:
109 : io_comp_{io_comp},
110 io_ctx_work_guard_{net::make_work_guard(io_comp.io_context())} {
111 ++io_comp_.users_;
112 }
113
114 Workguard(const Workguard &) = delete;
115 Workguard(Workguard &&) = delete;
116 Workguard &operator=(const Workguard &) = delete;
118
120 if (--io_comp_.users_ == 0) {
121 io_comp_.stop();
122 }
123 }
124
125 private:
128 };
129
131
132 private:
133 IoComponent() = default;
134
135 std::list<IoThread> io_threads_;
136
137 std::unique_ptr<IoThread> routing_acceptor_io_thread_;
138
139 std::unique_ptr<net::io_context> io_ctx_;
140
141 std::atomic<int> users_{};
142
143 std::string backend_name_;
144};
145
146#endif
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:580
Definition: io_component.h:106
IoComponent & io_comp_
Definition: io_component.h:126
Workguard(IoComponent &io_comp)
Definition: io_component.h:108
Workguard & operator=(Workguard &&)=delete
Workguard(const Workguard &)=delete
Workguard & operator=(const Workguard &)=delete
Workguard(Workguard &&)=delete
~Workguard()
Definition: io_component.h:119
net::executor_work_guard< net::io_context::executor_type > io_ctx_work_guard_
Definition: io_component.h:127
Definition: io_component.h:46
std::unique_ptr< net::io_context > io_ctx_
Definition: io_component.h:139
IoComponent()=default
std::unique_ptr< IoThread > routing_acceptor_io_thread_
Definition: io_component.h:137
std::list< IoThread > & io_threads()
Definition: io_component.h:48
std::string backend_name() const
Definition: io_component.h:104
std::string backend_name_
Definition: io_component.h:143
Workguard work_guard()
std::list< IoThread > io_threads_
Definition: io_component.h:135
Definition: executor.h:452
Definition: io_context.h:61
Definition: expected.h:286
IoComponentErrc
Definition: io_component.h:39
IO_COMPONENT_EXPORT std::error_code make_error_code(IoComponentErrc ec)
Definition: io_component.cc:54
#define IO_COMPONENT_EXPORT
Definition: io_component_export.h:15
static void run(mysql_harness::PluginFuncEnv *)
Definition: io_plugin.cc:197
std::string HARNESS_EXPORT reset()
get 'reset attributes' ESC sequence.
Definition: vt100.cc:37
Definition: buffer.h:45
executor_work_guard< Executor > make_work_guard(const Executor &ex)
Definition: executor.h:496