pragma once include poll include sys types include sys socket include

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#pragma once
#include <poll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/eventfd.h>
#include <sys/timerfd.h>
#include <condition_variable>
#include <mutex>
#include "common.h"
#include "stop_token.h"
namespace detail {
struct poll_stop_token
{
private:
int _eventfd = -1;
public:
poll_stop_token() {
_eventfd = CHECKED(eventfd(0, EFD_CLOEXEC));
}
~poll_stop_token() {
close(_eventfd);
}
poll_stop_token(const poll_stop_token&) = delete;
poll_stop_token& operator = (const poll_stop_token&) = delete;
void cancel() {
uint64_t val = 1;
CHECKED(::write(_eventfd, &val, sizeof(val)) == sizeof(val));
}
void reset() {
uint64_t val;
CHECKED(::read(_eventfd, &val, sizeof(val)) == sizeof(val));
}
int get_fd() const {
return _eventfd;
}
};
static inline ssize_t poll(stop_token &token, int fd, short event, int timeoutMs) {
poll_stop_token poll_token;
stop_callback call(token, [&poll_token]{
poll_token.cancel();
});
if (token.stop_requested()) {
errno = -ECANCELED;
return -1;
}
pollfd fds[2] = { };
fds[0].fd = fd;
fds[0].events = event;
fds[1].fd = poll_token.get_fd();
fds[1].events = POLLIN;
if (poll(fds, 2, timeoutMs) < 0) {
return -1;
}
if (fds[1].revents & POLLIN) {
errno = -ECANCELED;
return -1;
}
return fds[0].revents;
}
template<class Function, class ...Args>
ssize_t blocking_sysio(Function &&f, short event, stop_token &token, int fd, Args &&...args) {
auto res = poll(token, fd, event, -1);
if (res & event) {
if constexpr (std::is_invocable_v<std::decay_t<Function>>) {
return std::invoke(std::forward<Function>(f), fd, std::forward<Args>(args)...);
}
return 0;
}
return res;
}
inline const auto &read = ::read;
inline const auto &recv = ::recv;
inline const auto &recvfrom = ::recvfrom;
inline const auto &recvmsg = ::recvmsg;
inline const auto &write = ::write;
inline const auto &send = ::send;
inline const auto &sendto = ::sendto;
inline const auto &sendmsg = ::sendmsg;
}
static inline ssize_t read(stop_token &token, int fd, void *buf, size_t count) {
return detail::blocking_sysio(detail::read, POLLIN, token, fd, buf, count);
}
static inline int recv(stop_token &token, int s, void *buf, size_t len, int flags) {
return detail::blocking_sysio(detail::recv, POLLIN, token, s, buf, len, flags);
}
static inline int recvfrom(stop_token &token, int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen) {
return detail::blocking_sysio(detail::recvfrom, POLLIN, token, s, buf, len, flags, from, fromlen);
}
static inline int recvmsg(stop_token &token, int s, struct msghdr *msg, int flags) {
return detail::blocking_sysio(detail::recvmsg, POLLIN, token, s, msg, flags);
}
static inline ssize_t write(stop_token &token, int fd, void *buf, size_t count) {
return detail::blocking_sysio(detail::write, POLLOUT, token, fd, buf, count);
}
static inline ssize_t send(stop_token &token, int s, const void *msg, size_t len, int flags) {
return detail::blocking_sysio(detail::send, POLLOUT, token, s, msg, len, flags);
}
static inline ssize_t sendto(stop_token &token, int s, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen) {
return detail::blocking_sysio(detail::sendto, POLLOUT, token, s, msg, len, flags, to, tolen);
}
static inline ssize_t sendmsg(stop_token &token, int s, const struct msghdr *msg, int flags) {
return detail::blocking_sysio(detail::sendmsg, POLLOUT, token, s, msg, flags);
}
static inline int sleep(stop_token &token, unsigned int seconds) {
if (token.stop_requested()) {
errno = ECANCELED;
return -1;
}
int fd = CHECKED(timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK | TFD_CLOEXEC));
scope_guard {
close(fd);
};
itimerspec new_value {{},{seconds, 0}};
CHECKED(timerfd_settime(fd, 0, &new_value, nullptr));
return detail::blocking_sysio(nonstd::nullopt, POLLIN, token, fd);
}
static inline int cond_val_wait(stop_token &token, std::condition_variable &v, std::unique_lock<std::mutex>& lock) {
if (!lock.owns_lock()) {
lock.lock();
}
stop_callback guard(token, [&]() {
std::scoped_lock<std::mutex> m(*lock.mutex());
v.notify_all();
});
if (token.stop_requested()) {
errno = ECANCELED;
return -1;
}
v.wait(lock);
if (token.stop_requested()) {
errno = ECANCELED;
return -1;
}
return 0;
}
template<typename Predicate>
static inline int cond_val_wait(stop_token &token, std::condition_variable &v, std::unique_lock<std::mutex>& lock, Predicate p) {
while (!p) {
if (cond_val_wait(token, v, lock) < 0) {
return -1;
}
}
return 0;
}