2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-01-02 14:42:31 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
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"
|
2018-09-10 03:08:15 +02:00
|
|
|
#include "td/utils/port/PollFlags.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
|
|
|
|
#include <sys/eventfd.h>
|
2018-08-13 19:15:09 +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");
|
|
|
|
impl_ = std::make_unique<EventFdLinuxImpl>();
|
|
|
|
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> {
|
2018-09-10 01:16:42 +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() {
|
|
|
|
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
|
|
|
|
) {
|
|
|
|
get_poll_info().clear_flags(PollFlags::Read());
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace td
|
|
|
|
|
|
|
|
#endif
|