2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
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() {
|
|
|
|
kq = -1;
|
|
|
|
}
|
|
|
|
KQueue::~KQueue() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
void KQueue::init() {
|
|
|
|
kq = kqueue();
|
|
|
|
auto kqueue_errno = errno;
|
|
|
|
LOG_IF(FATAL, kq == -1) << Status::PosixError(kqueue_errno, "kqueue creation failed");
|
|
|
|
|
|
|
|
// TODO: const
|
|
|
|
events.resize(1000);
|
|
|
|
changes_n = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KQueue::clear() {
|
|
|
|
if (kq == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
events.clear();
|
|
|
|
close(kq);
|
|
|
|
kq = -1;
|
2018-08-13 19:15:09 +02:00
|
|
|
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;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int KQueue::update(int nevents, const timespec *timeout, bool may_fail) {
|
|
|
|
int err = kevent(kq, &events[0], changes_n, &events[0], nevents, timeout);
|
|
|
|
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");
|
|
|
|
|
|
|
|
changes_n = 0;
|
|
|
|
if (err < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KQueue::flush_changes(bool may_fail) {
|
|
|
|
if (!changes_n) {
|
|
|
|
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) {
|
|
|
|
if (changes_n == static_cast<int>(events.size())) {
|
|
|
|
flush_changes();
|
|
|
|
}
|
|
|
|
EV_SET(&events[changes_n], ident, filter, flags, fflags, data, udata);
|
|
|
|
VLOG(fd) << "Subscribe [fd:" << ident << "] [filter:" << filter << "] [udata: " << udata << "]";
|
|
|
|
changes_n++;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
list_root.put(list_node);
|
|
|
|
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) {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (int i = 0; i < changes_n; i++) {
|
2018-08-13 19:15:09 +02:00
|
|
|
if (events[i].ident == static_cast<std::uintptr_t>(native_fd)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
changes_n--;
|
|
|
|
std::swap(events[i], events[changes_n]);
|
|
|
|
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)
|
|
|
|
if (changes_n != 0) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int n = update(static_cast<int>(events.size()), timeout_ptr);
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
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
|
|
|
}
|
|
|
|
VLOG(fd) << "Event [fd:" << event->ident << "] [filter:" << event->filter << "] [udata: " << event->udata << "]";
|
2019-02-21 16:58:20 +01:00
|
|
|
// LOG(WARNING) << "Have event->ident = " << event->ident << "event->filter = " << event->filter;
|
2018-08-13 19:15:09 +02:00
|
|
|
auto pollable_fd = PollableFd::from_list_node(static_cast<ListNode *>(event->udata));
|
|
|
|
pollable_fd.add_flags(flags);
|
|
|
|
pollable_fd.release_as_list_node();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace td
|
|
|
|
|
|
|
|
#endif
|