MySQL 8.0.39
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) {
66 std::error_code{errno, std::generic_category()});
67 }
68
69 return epfd;
70 });
71}
72inline stdx::expected<void, std::error_code> ctl(int epfd, Cmd cmd, int fd,
73 epoll_event *ev) {
75 if (-1 == ::epoll_ctl(epfd, static_cast<int>(cmd), fd, ev)) {
77 std::error_code{errno, std::generic_category()});
78 }
79
80 return {};
81 });
82}
84 int epfd, epoll_event *fd_events, size_t num_fd_events,
85 std::chrono::milliseconds timeout) {
86 // all are processed. fetch the next batch
87 int res = ::epoll_wait(epfd, fd_events, num_fd_events, timeout.count());
88
89 if (res < 0) {
91 } else if (res == 0) {
92 // timed out
93 return stdx::make_unexpected(make_error_code(std::errc::timed_out));
94 }
95
96 return res;
97}
98
99} // namespace epoll
100
101} // namespace impl
102} // namespace net
103#endif
104
105#endif
Definition: expected.h:944
static bool interrupted
Definition: mysqladmin.cc:66
Definition: authentication.cc:36
static bool timeout(bool(*wait_condition)())
Timeout function.
Definition: log0meb.cc:496
stdx::expected< void, std::error_code > ctl(int epfd, Cmd cmd, int fd, epoll_event *ev)
Definition: linux_epoll.h:72
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:83
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
constexpr auto make_unexpected(E &&e) -> unexpected< std::decay_t< E > >
Definition: expected.h:125