2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
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/EventFdLinux.h"
|
|
|
|
|
|
|
|
char disable_linker_warning_about_empty_file_event_fd_linux_cpp TD_UNUSED;
|
|
|
|
|
|
|
|
#ifdef TD_EVENTFD_LINUX
|
|
|
|
|
|
|
|
#include "td/utils/logging.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/port/detail/NativeFd.h"
|
2020-06-15 02:50:38 +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-09-27 15:37:15 +02:00
|
|
|
#include "td/utils/ScopeGuard.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-06-07 17:14:52 +02:00
|
|
|
#include <cerrno>
|
|
|
|
|
2018-09-13 15:27:09 +02:00
|
|
|
#include <poll.h>
|
2018-09-14 00:00:21 +02:00
|
|
|
#include <sys/eventfd.h>
|
2018-09-13 20:22:25 +02:00
|
|
|
#include <unistd.h>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace detail {
|
2018-08-13 19:15:09 +02:00
|
|
|
class EventFdLinuxImpl {
|
|
|
|
public:
|
|
|
|
PollableFdInfo info;
|
|
|
|
};
|
|
|
|
|
|
|
|
EventFdLinux::EventFdLinux() = default;
|
|
|
|
EventFdLinux::EventFdLinux(EventFdLinux &&) = default;
|
|
|
|
EventFdLinux &EventFdLinux::operator=(EventFdLinux &&) = default;
|
|
|
|
EventFdLinux::~EventFdLinux() = default;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void EventFdLinux::init() {
|
2018-08-13 19:15:09 +02:00
|
|
|
auto fd = NativeFd(eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC));
|
2018-12-31 20:04:05 +01:00
|
|
|
auto eventfd_errno = errno;
|
2018-08-13 19:15:09 +02:00
|
|
|
LOG_IF(FATAL, !fd) << Status::PosixError(eventfd_errno, "eventfd call failed");
|
2018-09-27 03:19:03 +02:00
|
|
|
impl_ = make_unique<EventFdLinuxImpl>();
|
2018-08-13 19:15:09 +02:00
|
|
|
impl_->info.set_native_fd(std::move(fd));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EventFdLinux::empty() {
|
2018-08-13 19:15:09 +02:00
|
|
|
return !impl_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EventFdLinux::close() {
|
2018-08-13 19:15:09 +02:00
|
|
|
impl_.reset();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Status EventFdLinux::get_pending_error() {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
PollableFdInfo &EventFdLinux::get_poll_info() {
|
|
|
|
return impl_->info;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
// NB: will be called from multiple threads
|
2018-12-31 20:04:05 +01:00
|
|
|
void EventFdLinux::release() {
|
|
|
|
const uint64 value = 1;
|
2018-08-13 19:15:09 +02:00
|
|
|
auto slice = Slice(reinterpret_cast<const char *>(&value), sizeof(value));
|
|
|
|
auto native_fd = impl_->info.native_fd().fd();
|
|
|
|
|
|
|
|
auto result = [&]() -> Result<size_t> {
|
2020-10-18 17:12:32 +02:00
|
|
|
auto write_res = detail::skip_eintr([&] { return write(native_fd, slice.begin(), slice.size()); });
|
2018-08-13 19:15:09 +02:00
|
|
|
auto write_errno = errno;
|
|
|
|
if (write_res >= 0) {
|
|
|
|
return narrow_cast<size_t>(write_res);
|
|
|
|
}
|
|
|
|
return Status::PosixError(write_errno, PSLICE() << "Write to fd " << native_fd << " has failed");
|
|
|
|
}();
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
if (result.is_error()) {
|
|
|
|
LOG(FATAL) << "EventFdLinux write failed: " << result.error();
|
|
|
|
}
|
|
|
|
size_t size = result.ok();
|
|
|
|
if (size != sizeof(value)) {
|
|
|
|
LOG(FATAL) << "EventFdLinux write returned " << value << " instead of " << sizeof(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventFdLinux::acquire() {
|
2020-07-21 17:29:39 +02:00
|
|
|
impl_->info.sync_with_poll();
|
2018-09-22 16:13:49 +02:00
|
|
|
SCOPE_EXIT {
|
|
|
|
// Clear flags without EAGAIN and EWOULDBLOCK
|
|
|
|
// Looks like it is safe thing to do with eventfd
|
|
|
|
get_poll_info().clear_flags(PollFlags::Read());
|
|
|
|
};
|
2018-12-31 20:04:05 +01:00
|
|
|
uint64 res;
|
2018-08-13 19:15:09 +02:00
|
|
|
auto slice = MutableSlice(reinterpret_cast<char *>(&res), sizeof(res));
|
|
|
|
auto native_fd = impl_->info.native_fd().fd();
|
|
|
|
auto result = [&]() -> Result<size_t> {
|
|
|
|
CHECK(slice.size() > 0);
|
2018-09-10 01:16:42 +02:00
|
|
|
auto read_res = detail::skip_eintr([&] { return ::read(native_fd, slice.begin(), slice.size()); });
|
2018-08-13 19:15:09 +02:00
|
|
|
auto read_errno = errno;
|
|
|
|
if (read_res >= 0) {
|
|
|
|
CHECK(read_res != 0);
|
|
|
|
return narrow_cast<size_t>(read_res);
|
|
|
|
}
|
|
|
|
if (read_errno == EAGAIN
|
|
|
|
#if EAGAIN != EWOULDBLOCK
|
|
|
|
|| read_errno == EWOULDBLOCK
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return Status::PosixError(read_errno, PSLICE() << "Read from fd " << native_fd << " has failed");
|
|
|
|
}();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (result.is_error()) {
|
|
|
|
LOG(FATAL) << "EventFdLinux read failed: " << result.error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 15:27:09 +02:00
|
|
|
void EventFdLinux::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
|