Epoll: use NativeFd

GitOrigin-RevId: a4ff097abcf03cc7ac3a918969762c1539a19644
This commit is contained in:
Arseny Smirnov 2019-07-31 13:45:15 +03:00
parent 115fba770f
commit bc1e26779f
2 changed files with 20 additions and 21 deletions

View File

@ -19,24 +19,23 @@ char disable_linker_warning_about_empty_file_epoll_cpp TD_UNUSED;
namespace td {
namespace detail {
void Epoll::init() {
CHECK(epoll_fd == -1);
epoll_fd = epoll_create(1);
CHECK(!epoll_fd_);
epoll_fd_ = NativeFd(epoll_create(1));
auto epoll_create_errno = errno;
LOG_IF(FATAL, epoll_fd == -1) << Status::PosixError(epoll_create_errno, "epoll_create failed");
LOG_IF(FATAL, !epoll_fd_) << Status::PosixError(epoll_create_errno, "epoll_create failed");
events.resize(1000);
events_.resize(1000);
}
void Epoll::clear() {
if (epoll_fd == -1) {
if (!epoll_fd_) {
return;
}
events.clear();
events_.clear();
close(epoll_fd);
epoll_fd = -1;
epoll_fd_.close();
for (auto *list_node = list_root.next; list_node != &list_root;) {
for (auto *list_node = list_root_.next; list_node != &list_root_;) {
auto pollable_fd = PollableFd::from_list_node(list_node);
list_node = list_node->next;
}
@ -56,22 +55,22 @@ void Epoll::subscribe(PollableFd fd, PollFlags flags) {
}
auto native_fd = fd.native_fd().fd();
auto *list_node = fd.release_as_list_node();
list_root.put(list_node);
list_root_.put(list_node);
event.data.ptr = list_node;
int err = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, native_fd, &event);
int err = epoll_ctl(epoll_fd_.fd(), EPOLL_CTL_ADD, native_fd, &event);
auto epoll_ctl_errno = errno;
LOG_IF(FATAL, err == -1) << Status::PosixError(epoll_ctl_errno, "epoll_ctl ADD failed") << ", epoll_fd = " << epoll_fd
<< ", fd = " << native_fd;
LOG_IF(FATAL, err == -1) << Status::PosixError(epoll_ctl_errno, "epoll_ctl ADD failed")
<< ", epoll_fd = " << epoll_fd_.fd() << ", fd = " << native_fd;
}
void Epoll::unsubscribe(PollableFdRef fd_ref) {
auto fd = fd_ref.lock();
auto native_fd = fd.native_fd().fd();
int err = epoll_ctl(epoll_fd, EPOLL_CTL_DEL, native_fd, nullptr);
int err = epoll_ctl(epoll_fd_.fd(), EPOLL_CTL_DEL, native_fd, nullptr);
auto epoll_ctl_errno = errno;
LOG_IF(FATAL, err == -1) << Status::PosixError(epoll_ctl_errno, "epoll_ctl DEL failed") << ", epoll_fd = " << epoll_fd
<< ", fd = " << native_fd << fd.native_fd().validate();
LOG_IF(FATAL, err == -1) << Status::PosixError(epoll_ctl_errno, "epoll_ctl DEL failed")
<< ", epoll_fd = " << epoll_fd_.fd() << ", fd = " << native_fd << fd.native_fd().validate();
}
void Epoll::unsubscribe_before_close(PollableFdRef fd) {
@ -79,14 +78,14 @@ void Epoll::unsubscribe_before_close(PollableFdRef fd) {
}
void Epoll::run(int timeout_ms) {
int ready_n = epoll_wait(epoll_fd, &events[0], static_cast<int>(events.size()), timeout_ms);
int ready_n = epoll_wait(epoll_fd_.fd(), &events_[0], static_cast<int>(events_.size()), timeout_ms);
auto epoll_wait_errno = errno;
LOG_IF(FATAL, ready_n == -1 && epoll_wait_errno != EINTR)
<< Status::PosixError(epoll_wait_errno, "epoll_wait failed");
for (int i = 0; i < ready_n; i++) {
PollFlags flags;
epoll_event *event = &events[i];
epoll_event *event = &events_[i];
if (event->events & EPOLLIN) {
event->events &= ~EPOLLIN;
flags = flags | PollFlags::Read();

View File

@ -47,9 +47,9 @@ class Epoll final : public PollBase {
}
private:
int epoll_fd = -1;
vector<struct epoll_event> events;
ListNode list_root;
NativeFd epoll_fd_;
vector<struct epoll_event> events_;
ListNode list_root_;
};
} // namespace detail