MySQL 8.4.0
Source Code Documentation
linux_epoll.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 MYSQL_HARNESS_NET_TS_IMPL_LINUX_EPOLL_H_
27#define MYSQL_HARNESS_NET_TS_IMPL_LINUX_EPOLL_H_
28
29#include "my_config.h" // HAVE_EPOLL
30
31#ifdef HAVE_EPOLL
32#include <chrono>
33#include <system_error>
34
35#include <sys/epoll.h>
36
39
40namespace net {
41namespace impl {
42
43namespace epoll {
44
45enum class Cmd {
46 add = EPOLL_CTL_ADD,
47 del = EPOLL_CTL_DEL,
48 mod = EPOLL_CTL_MOD,
49};
50
51// restarted syscalls automatically after EINTR
52template <class Func>
53inline auto uninterruptable(Func &&f) {
54 do {
55 auto res = f();
56 if (res || (res.error() != std::errc::interrupted)) return res;
57 } while (true);
58}
59
62 int epfd = ::epoll_create1(EPOLL_CLOEXEC);
63
64 if (-1 == epfd) {
65 return stdx::unexpected(std::error_code{errno, std::generic_category()});
66 }
67
68 return epfd;
69 });
70}
71inline stdx::expected<void, std::error_code> ctl(int epfd, Cmd cmd, int fd,
72 epoll_event *ev) {
74 if (-1 == ::epoll_ctl(epfd, static_cast<int>(cmd), fd, ev)) {
75 return stdx::unexpected(std::error_code{errno, std::generic_category()});
76 }
77
78 return {};
79 });
80}
82 int epfd, epoll_event *fd_events, size_t num_fd_events,
83 std::chrono::milliseconds timeout) {
84 // all are processed. fetch the next batch
85 int res = ::epoll_wait(epfd, fd_events, num_fd_events, timeout.count());
86
87 if (res < 0) {
89 } else if (res == 0) {
90 // timed out
91 return stdx::unexpected(make_error_code(std::errc::timed_out));
92 }
93
94 return res;
95}
96
97} // namespace epoll
98
99} // namespace impl
100} // namespace net
101#endif
102
103#endif
Definition: expected.h:284
static bool interrupted
Definition: mysqladmin.cc:72
Definition: http_server_component.cc:34
static bool timeout(bool(*wait_condition)())
Timeout function.
Definition: log0meb.cc:498
stdx::expected< void, std::error_code > ctl(int epfd, Cmd cmd, int fd, epoll_event *ev)
Definition: linux_epoll.h:71
Cmd
Definition: linux_epoll.h:45
stdx::expected< int, std::error_code > create()
Definition: linux_epoll.h:60
auto uninterruptable(Func &&f)
Definition: linux_epoll.h:53
stdx::expected< size_t, std::error_code > wait(int epfd, epoll_event *fd_events, size_t num_fd_events, std::chrono::milliseconds timeout)
Definition: linux_epoll.h:81
std::error_code last_error_code()
get last std::error_code for socket-errors.
Definition: socket_error.h:107
Definition: buffer.h:45
std::error_code make_error_code(net::stream_errc e) noexcept
Definition: buffer.h:103
unexpected(E) -> unexpected< E >