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/KQueue.h"
|
|
|
|
|
|
|
|
char disable_linker_warning_about_empty_file_kqueue_cpp TD_UNUSED;
|
|
|
|
|
|
|
|
#ifdef TD_POLL_KQUEUE
|
|
|
|
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
2018-11-02 20:57:22 +01:00
|
|
|
#include <sys/time.h>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
KQueue::~KQueue() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
void KQueue::init() {
|
2019-07-31 11:18:48 +02:00
|
|
|
kq_ = NativeFd(kqueue());
|
2018-12-31 20:04:05 +01:00
|
|
|
auto kqueue_errno = errno;
|
2019-07-31 11:18:48 +02:00
|
|
|
LOG_IF(FATAL, !kq_) << Status::PosixError(kqueue_errno, "kqueue creation failed");
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// TODO: const
|
2019-07-31 11:18:48 +02:00
|
|
|
events_.resize(1000);
|
|
|
|
changes_n_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void KQueue::clear() {
|
2019-07-31 11:18:48 +02:00
|
|
|
if (!kq_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-07-31 11:18:48 +02:00
|
|
|
events_.clear();
|
|
|
|
kq_.close();
|
|
|
|
for (auto *list_node = list_root_.next; list_node != &list_root_;) {
|
2018-08-13 19:15:09 +02:00
|
|
|
auto pollable_fd = PollableFd::from_list_node(list_node);
|
|
|
|
list_node = list_node->next;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int KQueue::update(int nevents, const timespec *timeout, bool may_fail) {
|
2019-07-31 11:18:48 +02:00
|
|
|
int err = kevent(kq_.fd(), &events_[0], changes_n_, &events_[0], nevents, timeout);
|
2018-12-31 20:04:05 +01:00
|
|
|
auto kevent_errno = errno;
|
|
|
|
|
|
|
|
bool is_fatal_error = [&] {
|
|
|
|
if (err != -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (may_fail) {
|
|
|
|
return kevent_errno != ENOENT;
|
|
|
|
}
|
|
|
|
return kevent_errno != EINTR;
|
|
|
|
}();
|
|
|
|
LOG_IF(FATAL, is_fatal_error) << Status::PosixError(kevent_errno, "kevent failed");
|
|
|
|
|
2019-07-31 11:18:48 +02:00
|
|
|
changes_n_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (err < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KQueue::flush_changes(bool may_fail) {
|
2019-07-31 11:18:48 +02:00
|
|
|
if (!changes_n_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
int n = update(0, nullptr, may_fail);
|
|
|
|
CHECK(n == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KQueue::add_change(std::uintptr_t ident, int16 filter, uint16 flags, uint32 fflags, std::intptr_t data,
|
|
|
|
void *udata) {
|
2019-07-31 11:18:48 +02:00
|
|
|
if (changes_n_ == static_cast<int>(events_.size())) {
|
2018-12-31 20:04:05 +01:00
|
|
|
flush_changes();
|
|
|
|
}
|
2019-08-02 18:06:53 +02:00
|
|
|
#if TD_NETBSD
|
|
|
|
auto set_udata = reinterpret_cast<std::intptr_t>(udata);
|
|
|
|
#else
|
|
|
|
auto set_udata = udata;
|
|
|
|
#endif
|
|
|
|
EV_SET(&events_[changes_n_], ident, filter, flags, fflags, data, set_udata);
|
2018-12-31 20:04:05 +01:00
|
|
|
VLOG(fd) << "Subscribe [fd:" << ident << "] [filter:" << filter << "] [udata: " << udata << "]";
|
2019-07-31 11:18:48 +02:00
|
|
|
changes_n_++;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
void KQueue::subscribe(PollableFd fd, PollFlags flags) {
|
|
|
|
auto native_fd = fd.native_fd().fd();
|
|
|
|
auto list_node = fd.release_as_list_node();
|
2019-07-31 11:18:48 +02:00
|
|
|
list_root_.put(list_node);
|
2018-08-13 19:15:09 +02:00
|
|
|
if (flags.can_read()) {
|
|
|
|
add_change(native_fd, EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, list_node);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
if (flags.can_write()) {
|
|
|
|
add_change(native_fd, EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, list_node);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
void KQueue::invalidate(int native_fd) {
|
2019-07-31 11:18:48 +02:00
|
|
|
for (int i = 0; i < changes_n_; i++) {
|
|
|
|
if (events_[i].ident == static_cast<std::uintptr_t>(native_fd)) {
|
|
|
|
changes_n_--;
|
|
|
|
std::swap(events_[i], events_[changes_n_]);
|
2018-12-31 20:04:05 +01:00
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
void KQueue::unsubscribe(PollableFdRef fd_ref) {
|
|
|
|
auto pollable_fd = fd_ref.lock();
|
|
|
|
auto native_fd = pollable_fd.native_fd().fd();
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
// invalidate(fd);
|
|
|
|
flush_changes();
|
2018-08-13 19:15:09 +02:00
|
|
|
add_change(native_fd, EVFILT_READ, EV_DELETE, 0, 0, nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
flush_changes(true);
|
2018-08-13 19:15:09 +02:00
|
|
|
add_change(native_fd, EVFILT_WRITE, EV_DELETE, 0, 0, nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
flush_changes(true);
|
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
void KQueue::unsubscribe_before_close(PollableFdRef fd_ref) {
|
|
|
|
auto pollable_fd = fd_ref.lock();
|
|
|
|
invalidate(pollable_fd.native_fd().fd());
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// just to avoid O(changes_n ^ 2)
|
2019-07-31 11:18:48 +02:00
|
|
|
if (changes_n_ != 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
flush_changes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KQueue::run(int timeout_ms) {
|
|
|
|
timespec timeout_data;
|
|
|
|
timespec *timeout_ptr;
|
|
|
|
if (timeout_ms == -1) {
|
|
|
|
timeout_ptr = nullptr;
|
|
|
|
} else {
|
|
|
|
timeout_data.tv_sec = timeout_ms / 1000;
|
|
|
|
timeout_data.tv_nsec = timeout_ms % 1000 * 1000000;
|
|
|
|
timeout_ptr = &timeout_data;
|
|
|
|
}
|
|
|
|
|
2019-07-31 11:18:48 +02:00
|
|
|
int n = update(static_cast<int>(events_.size()), timeout_ptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
for (int i = 0; i < n; i++) {
|
2019-07-31 11:18:48 +02:00
|
|
|
struct kevent *event = &events_[i];
|
2018-08-13 19:15:09 +02:00
|
|
|
PollFlags flags;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (event->filter == EVFILT_WRITE) {
|
2018-08-13 19:15:09 +02:00
|
|
|
flags.add_flags(PollFlags::Write());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (event->filter == EVFILT_READ) {
|
2018-08-13 19:15:09 +02:00
|
|
|
flags.add_flags(PollFlags::Read());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (event->flags & EV_EOF) {
|
2018-08-13 19:15:09 +02:00
|
|
|
flags.add_flags(PollFlags::Close());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (event->fflags & EV_ERROR) {
|
2018-02-11 15:07:16 +01:00
|
|
|
LOG(FATAL) << "EV_ERROR in kqueue is not supported";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-08-02 18:06:53 +02:00
|
|
|
#if TD_NETBSD
|
|
|
|
auto udata = reinterpret_cast<void *>(event->udata);
|
|
|
|
#else
|
|
|
|
auto udata = event->udata;
|
|
|
|
#endif
|
|
|
|
VLOG(fd) << "Event [fd:" << event->ident << "] [filter:" << event->filter << "] [udata: " << udata << "]";
|
2019-02-21 16:58:20 +01:00
|
|
|
// LOG(WARNING) << "Have event->ident = " << event->ident << "event->filter = " << event->filter;
|
2019-08-02 18:06:53 +02:00
|
|
|
auto pollable_fd = PollableFd::from_list_node(static_cast<ListNode *>(udata));
|
2018-08-13 19:15:09 +02:00
|
|
|
pollable_fd.add_flags(flags);
|
|
|
|
pollable_fd.release_as_list_node();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace td
|
|
|
|
|
|
|
|
#endif
|