2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
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/EventFdBsd.h"
|
|
|
|
|
|
|
|
char disable_linker_warning_about_empty_file_event_fd_bsd_cpp TD_UNUSED;
|
|
|
|
|
|
|
|
#ifdef TD_EVENTFD_BSD
|
|
|
|
|
|
|
|
#include "td/utils/logging.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/port/detail/NativeFd.h"
|
2020-08-07 17:50:33 +02:00
|
|
|
#include "td/utils/port/detail/skip_eintr.h"
|
2018-09-10 03:08:15 +02:00
|
|
|
#include "td/utils/port/PollFlags.h"
|
2018-08-13 19:15:09 +02:00
|
|
|
#include "td/utils/port/SocketFd.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/Slice.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-06-07 17:14:52 +02:00
|
|
|
#include <cerrno>
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <fcntl.h>
|
2018-09-13 20:22:25 +02:00
|
|
|
#include <poll.h>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
// TODO: it is extremely non optimal on Darwin. kqueue events should be used instead
|
|
|
|
void EventFdBsd::init() {
|
|
|
|
int fds[2];
|
|
|
|
int err = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
|
|
|
|
auto socketpair_errno = errno;
|
|
|
|
#if TD_CYGWIN
|
|
|
|
// it looks like CYGWIN bug
|
|
|
|
int max_retries = 1000000;
|
|
|
|
while (err == -1 && socketpair_errno == EADDRINUSE && max_retries-- > 0) {
|
|
|
|
err = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
|
|
|
|
socketpair_errno = errno;
|
|
|
|
}
|
|
|
|
// LOG_IF(ERROR, max_retries < 1000000) << max_retries;
|
|
|
|
#endif
|
|
|
|
LOG_IF(FATAL, err == -1) << Status::PosixError(socketpair_errno, "socketpair failed");
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
auto fd_a = NativeFd(fds[0]);
|
|
|
|
auto fd_b = NativeFd(fds[1]);
|
2018-09-10 16:05:12 +02:00
|
|
|
fd_a.set_is_blocking_unsafe(false).ensure();
|
|
|
|
fd_b.set_is_blocking_unsafe(false).ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
in_ = SocketFd::from_native_fd(std::move(fd_a)).move_as_ok();
|
|
|
|
out_ = SocketFd::from_native_fd(std::move(fd_b)).move_as_ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EventFdBsd::empty() {
|
|
|
|
return in_.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventFdBsd::close() {
|
|
|
|
in_.close();
|
|
|
|
out_.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status EventFdBsd::get_pending_error() {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
PollableFdInfo &EventFdBsd::get_poll_info() {
|
|
|
|
return out_.get_poll_info();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EventFdBsd::release() {
|
|
|
|
int value = 1;
|
|
|
|
auto result = in_.write(Slice(reinterpret_cast<const char *>(&value), sizeof(value)));
|
|
|
|
if (result.is_error()) {
|
|
|
|
LOG(FATAL) << "EventFdBsd write failed: " << result.error();
|
|
|
|
}
|
|
|
|
size_t size = result.ok();
|
|
|
|
if (size != sizeof(value)) {
|
2020-09-02 16:22:30 +02:00
|
|
|
LOG(FATAL) << "EventFdBsd write returned " << size << " instead of " << sizeof(value);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventFdBsd::acquire() {
|
2020-07-21 17:29:39 +02:00
|
|
|
sync_with_poll(out_);
|
2018-08-13 19:15:09 +02:00
|
|
|
out_.get_poll_info().add_flags(PollFlags::Read());
|
2020-07-21 17:29:39 +02:00
|
|
|
while (can_read_local(out_)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
uint8 value[1024];
|
|
|
|
auto result = out_.read(MutableSlice(value, sizeof(value)));
|
|
|
|
if (result.is_error()) {
|
|
|
|
LOG(FATAL) << "EventFdBsd read failed:" << result.error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 15:27:09 +02:00
|
|
|
void EventFdBsd::wait(int timeout_ms) {
|
2020-08-07 17:50:33 +02:00
|
|
|
detail::skip_eintr_timeout(
|
|
|
|
[this](int timeout_ms) {
|
|
|
|
pollfd fd;
|
|
|
|
fd.fd = get_poll_info().native_fd().fd();
|
|
|
|
fd.events = POLLIN;
|
|
|
|
return poll(&fd, 1, timeout_ms);
|
|
|
|
},
|
|
|
|
timeout_ms);
|
2018-09-13 15:27:09 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace td
|
|
|
|
|
|
|
|
#endif
|