MySQL 8.0.39
Source Code Documentation
io_component.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2020, 2024, 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 static IoComponent &get_instance();
59
60 /**
61 * initialize io-component.
62 *
63 * use IoComponent::reset() to reset the io-component into its initial state
64 *
65 * @param num_worker_threads number of worker-threads to spawn
66 * @param backend_name name of the io-backend
67 *
68 * @see IoComponent::reset()
69 */
70 stdx::expected<void, std::error_code> init(size_t num_worker_threads,
71 const std::string &backend_name);
72
73 /**
74 * run the main loop of the io-component.
75 *
76 * runs until all no more work is assigned to the mainloop or stopped.
77 *
78 * @see IoComponent::stop()
79 */
80 void run();
81
82 void stop();
83
84 /**
85 * reset the io_component into its initial state.
86 *
87 * when calling reset() no io-thread SHALL run which can be achieved by
88 *
89 * - calling stop() after run() was called.
90 * - not calling run()
91 */
92 void reset();
93
94 std::string backend_name() const { return backend_name_; }
95
96 class Workguard {
97 public:
99 : io_comp_{io_comp},
100 io_ctx_work_guard_{net::make_work_guard(io_comp.io_context())} {
101 ++io_comp_.users_;
102 }
103
104 Workguard(const Workguard &) = delete;
105 Workguard(Workguard &&) = delete;
106 Workguard &operator=(const Workguard &) = delete;
108
110 if (--io_comp_.users_ == 0) {
111 io_comp_.stop();
112 }
113 }
114
115 private:
118 };
119
121
122 private:
123 IoComponent() = default;
124
125 std::list<IoThread> io_threads_;
126
127 std::unique_ptr<net::io_context> io_ctx_;
128
129 std::atomic<int> users_{};
130
131 std::string backend_name_;
132};
133
134#endif
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:571
Definition: io_component.h:96
IoComponent & io_comp_
Definition: io_component.h:116
Workguard(IoComponent &io_comp)
Definition: io_component.h:98
Workguard & operator=(Workguard &&)=delete
Workguard(const Workguard &)=delete
Workguard & operator=(const Workguard &)=delete
Workguard(Workguard &&)=delete
~Workguard()
Definition: io_component.h:109
net::executor_work_guard< net::io_context::executor_type > io_ctx_work_guard_
Definition: io_component.h:117
Definition: io_component.h:46
std::unique_ptr< net::io_context > io_ctx_
Definition: io_component.h:127
IoComponent()=default
std::list< IoThread > & io_threads()
Definition: io_component.h:48
std::string backend_name() const
Definition: io_component.h:94
std::string backend_name_
Definition: io_component.h:131
Workguard work_guard()
std::list< IoThread > io_threads_
Definition: io_component.h:125
Definition: executor.h:469
Definition: io_context.h:61
Definition: expected.h:944
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:195
std::string HARNESS_EXPORT reset()
get 'reset attributes' ESC sequence.
Definition: vt100.cc:37
Definition: buffer.h:45
std::enable_if_t< is_executor< Executor >::value, executor_work_guard< Executor > > make_work_guard(const Executor &ex)
Definition: executor.h:524