From bc1e26779fd75b7e6d444f2eca086ea8765f5ff4 Mon Sep 17 00:00:00 2001 From: Arseny Smirnov Date: Wed, 31 Jul 2019 13:45:15 +0300 Subject: [PATCH] Epoll: use NativeFd GitOrigin-RevId: a4ff097abcf03cc7ac3a918969762c1539a19644 --- tdutils/td/utils/port/detail/Epoll.cpp | 35 +++++++++++++------------- tdutils/td/utils/port/detail/Epoll.h | 6 ++--- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/tdutils/td/utils/port/detail/Epoll.cpp b/tdutils/td/utils/port/detail/Epoll.cpp index 5c371c52..50e7c193 100644 --- a/tdutils/td/utils/port/detail/Epoll.cpp +++ b/tdutils/td/utils/port/detail/Epoll.cpp @@ -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(events.size()), timeout_ms); + int ready_n = epoll_wait(epoll_fd_.fd(), &events_[0], static_cast(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(); diff --git a/tdutils/td/utils/port/detail/Epoll.h b/tdutils/td/utils/port/detail/Epoll.h index 678093b9..1266f490 100644 --- a/tdutils/td/utils/port/detail/Epoll.h +++ b/tdutils/td/utils/port/detail/Epoll.h @@ -47,9 +47,9 @@ class Epoll final : public PollBase { } private: - int epoll_fd = -1; - vector events; - ListNode list_root; + NativeFd epoll_fd_; + vector events_; + ListNode list_root_; }; } // namespace detail