MySQL 8.0.39
Source Code Documentation
io_thread.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_THREAD_INCLUDED
27#define ROUTER_IO_THREAD_INCLUDED
28
29#include <cstddef> // size_t
30#include <memory> // make_unique
31#include <string>
32#include <thread>
33
38
39class IoThread {
40 public:
41 IoThread(size_t ndx, std::bitset<ThreadAffinity::max_cpus> cpu_affinity,
42 const std::string &backend_name = "poll")
43 : ndx_{ndx},
45 io_ctx_{std::make_unique<net::impl::socket::SocketService>(),
46 IoBackend::backend(backend_name)},
47 thr_{&IoThread::operator(), this} {}
48
50 stop();
51 join();
52 }
53
54 net::io_context &context() noexcept { return io_ctx_; }
55
56 void operator()();
57
58 void stop() { io_ctx_.stop(); }
59
60 void join() {
61 if (thr_.joinable()) thr_.join();
62 }
63
64 std::bitset<ThreadAffinity::max_cpus> cpu_affinity() const {
65 return cpu_affinity_;
66 }
67
68 private:
69 size_t ndx_;
70 std::bitset<ThreadAffinity::max_cpus> cpu_affinity_;
71
73
74 std::thread thr_;
75};
76
77#endif
Definition: io_backend.h:36
Definition: io_thread.h:39
size_t ndx_
Definition: io_thread.h:69
net::io_context io_ctx_
Definition: io_thread.h:72
IoThread(size_t ndx, std::bitset< ThreadAffinity::max_cpus > cpu_affinity, const std::string &backend_name="poll")
Definition: io_thread.h:41
void operator()()
Definition: io_thread.cc:38
void join()
Definition: io_thread.h:60
std::bitset< ThreadAffinity::max_cpus > cpu_affinity_
Definition: io_thread.h:70
std::bitset< ThreadAffinity::max_cpus > cpu_affinity() const
Definition: io_thread.h:64
~IoThread()
Definition: io_thread.h:49
net::io_context & context() noexcept
Definition: io_thread.h:54
void stop()
Definition: io_thread.h:58
std::thread thr_
Definition: io_thread.h:74
Definition: io_context.h:61
void stop()
Definition: io_context.h:116
Definition: authentication.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.
stdx::expected< native_handle_type, error_type > socket(int family, int sock_type, int protocol)
Definition: socket.h:63
Definition: buffer.h:45
Definition: gcs_xcom_synode.h:64