2018-12-31 20:04:05 +01:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/utils/port/detail/Poll.h"
|
|
|
|
|
|
|
|
char disable_linker_warning_about_empty_file_poll_cpp TD_UNUSED;
|
|
|
|
|
|
|
|
#ifdef TD_POLL_POLL
|
|
|
|
|
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
2018-09-13 05:08:49 +02:00
|
|
|
#include "td/utils/ScopeGuard.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
2020-06-07 17:14:52 +02:00
|
|
|
#include <cerrno>
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
void Poll::init() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Poll::clear() {
|
|
|
|
pollfds_.clear();
|
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
void Poll::subscribe(PollableFd fd, PollFlags flags) {
|
|
|
|
unsubscribe(fd.ref());
|
2018-12-31 20:04:05 +01:00
|
|
|
struct pollfd pollfd;
|
2018-08-13 19:15:09 +02:00
|
|
|
pollfd.fd = fd.native_fd().fd();
|
2018-12-31 20:04:05 +01:00
|
|
|
pollfd.events = 0;
|
2018-08-13 19:15:09 +02:00
|
|
|
if (flags.can_read()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
pollfd.events |= POLLIN;
|
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
if (flags.can_write()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
pollfd.events |= POLLOUT;
|
|
|
|
}
|
|
|
|
pollfd.revents = 0;
|
|
|
|
pollfds_.push_back(pollfd);
|
2018-08-13 19:15:09 +02:00
|
|
|
fds_.push_back(std::move(fd));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
void Poll::unsubscribe(PollableFdRef fd_ref) {
|
|
|
|
auto fd = fd_ref.lock();
|
|
|
|
SCOPE_EXIT {
|
|
|
|
fd.release_as_list_node();
|
|
|
|
};
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto it = pollfds_.begin(); it != pollfds_.end(); ++it) {
|
2018-08-13 19:15:09 +02:00
|
|
|
if (it->fd == fd.native_fd().fd()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
pollfds_.erase(it);
|
2018-08-13 19:15:09 +02:00
|
|
|
fds_.erase(fds_.begin() + (it - pollfds_.begin()));
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
void Poll::unsubscribe_before_close(PollableFdRef fd) {
|
2018-12-31 20:04:05 +01:00
|
|
|
unsubscribe(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Poll::run(int timeout_ms) {
|
|
|
|
int err = poll(pollfds_.data(), narrow_cast<int>(pollfds_.size()), timeout_ms);
|
|
|
|
auto poll_errno = errno;
|
|
|
|
LOG_IF(FATAL, err == -1 && poll_errno != EINTR) << Status::PosixError(poll_errno, "poll failed");
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
for (size_t i = 0; i < pollfds_.size(); i++) {
|
|
|
|
auto &pollfd = pollfds_[i];
|
|
|
|
auto &fd = fds_[i];
|
|
|
|
|
|
|
|
PollFlags flags;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (pollfd.revents & POLLIN) {
|
|
|
|
pollfd.revents &= ~POLLIN;
|
2018-08-13 19:15:09 +02:00
|
|
|
flags = flags | PollFlags::Read();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (pollfd.revents & POLLOUT) {
|
|
|
|
pollfd.revents &= ~POLLOUT;
|
2018-08-13 19:15:09 +02:00
|
|
|
flags = flags | PollFlags::Write();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (pollfd.revents & POLLHUP) {
|
|
|
|
pollfd.revents &= ~POLLHUP;
|
2018-08-13 19:15:09 +02:00
|
|
|
flags = flags | PollFlags::Close();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (pollfd.revents & POLLERR) {
|
|
|
|
pollfd.revents &= ~POLLERR;
|
2018-08-13 19:15:09 +02:00
|
|
|
flags = flags | PollFlags::Error();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (pollfd.revents & POLLNVAL) {
|
|
|
|
LOG(FATAL) << "Unexpected POLLNVAL " << tag("fd", pollfd.fd);
|
|
|
|
}
|
|
|
|
if (pollfd.revents) {
|
|
|
|
LOG(FATAL) << "Unsupported poll events: " << pollfd.revents;
|
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
fd.add_flags(flags);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace td
|
|
|
|
|
|
|
|
#endif
|