MySQL 8.3.0
Source Code Documentation
io_component.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2020, 2023, 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 also distributed 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 included with MySQL.
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 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
25#ifndef ROUTER_IO_COMPONENT_INCLUDED
26#define ROUTER_IO_COMPONENT_INCLUDED
27
28#include <list>
29#include <memory> // make_unique
30#include <string>
31
35
37
38enum class IoComponentErrc {
41};
42
44
46 public:
47 std::list<IoThread> &io_threads() { return io_threads_; }
48
49 /**
50 * get ref to the io_context.
51 *
52 * behaviour is undefined when called before IoComponent::init() and after
53 * IoComponent::reset()
54 */
55 net::io_context &io_context();
56
57 static IoComponent &get_instance();
58
59 /**
60 * initialize io-component.
61 *
62 * use IoComponent::reset() to reset the io-component into its initial state
63 *
64 * @param num_worker_threads number of worker-threads to spawn
65 * @param backend_name name of the io-backend
66 *
67 * @see IoComponent::reset()
68 */
69 stdx::expected<void, std::error_code> init(size_t num_worker_threads,
70 const std::string &backend_name);
71
72 /**
73 * run the main loop of the io-component.
74 *
75 * runs until all no more work is assigned to the mainloop or stopped.
76 *
77 * @see IoComponent::stop()
78 */
79 void run();
80
81 void stop();
82
83 /**
84 * reset the io_component into its initial state.
85 *
86 * when calling reset() no io-thread SHALL run which can be achieved by
87 *
88 * - calling stop() after run() was called.
89 * - not calling run()
90 */
91 void reset();
92
93 std::string backend_name() const { return backend_name_; }
94
95 class Workguard {
96 public:
98 : io_comp_{io_comp},
99 io_ctx_work_guard_{net::make_work_guard(io_comp.io_context())} {
100 ++io_comp_.users_;
101 }
102
103 Workguard(const Workguard &) = delete;
104 Workguard(Workguard &&) = delete;
105 Workguard &operator=(const Workguard &) = delete;
107
109 if (--io_comp_.users_ == 0) {
110 io_comp_.stop();
111 }
112 }
113
114 private:
117 };
118
120
121 private:
122 IoComponent() = default;
123
124 std::list<IoThread> io_threads_;
125
126 std::unique_ptr<net::io_context> io_ctx_;
127
128 std::atomic<int> users_{};
129
130 std::string backend_name_;
131};
132
133#endif
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:570
Definition: io_component.h:95
IoComponent & io_comp_
Definition: io_component.h:115
Workguard(IoComponent &io_comp)
Definition: io_component.h:97
Workguard & operator=(Workguard &&)=delete
Workguard(const Workguard &)=delete
Workguard & operator=(const Workguard &)=delete
Workguard(Workguard &&)=delete
~Workguard()
Definition: io_component.h:108
net::executor_work_guard< net::io_context::executor_type > io_ctx_work_guard_
Definition: io_component.h:116
Definition: io_component.h:45
std::unique_ptr< net::io_context > io_ctx_
Definition: io_component.h:126
IoComponent()=default
std::list< IoThread > & io_threads()
Definition: io_component.h:47
std::string backend_name() const
Definition: io_component.h:93
std::string backend_name_
Definition: io_component.h:130
Workguard work_guard()
std::list< IoThread > io_threads_
Definition: io_component.h:124
Definition: executor.h:451
Definition: io_context.h:60
Definition: expected.h:943
IoComponentErrc
Definition: io_component.h:38
IO_COMPONENT_EXPORT std::error_code make_error_code(IoComponentErrc ec)
Definition: io_component.cc:53
#define IO_COMPONENT_EXPORT
Definition: io_component_export.h:15
static void run(mysql_harness::PluginFuncEnv *)
Definition: io_plugin.cc:194
std::string HARNESS_EXPORT reset()
get 'reset attributes' ESC sequence.
Definition: vt100.cc:36
Definition: buffer.h:44
std::enable_if_t< is_executor< Executor >::value, executor_work_guard< Executor > > make_work_guard(const Executor &ex)
Definition: executor.h:506