2018-08-13 19:15:09 +02:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-08-13 19:15:09 +02: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)
|
|
|
|
//
|
|
|
|
#pragma once
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/format.h"
|
2018-08-13 19:15:09 +02:00
|
|
|
#include "td/utils/List.h"
|
2018-09-10 03:08:15 +02:00
|
|
|
#include "td/utils/logging.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/Observer.h"
|
2018-08-13 19:15:09 +02:00
|
|
|
#include "td/utils/port/detail/NativeFd.h"
|
2018-09-10 01:37:05 +02:00
|
|
|
#include "td/utils/port/PollFlags.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/SpinLock.h"
|
2018-08-13 19:15:09 +02:00
|
|
|
|
|
|
|
#include <atomic>
|
2018-09-27 03:19:03 +02:00
|
|
|
#include <memory>
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class PollableFdInfo;
|
|
|
|
class PollableFdInfoUnlock {
|
|
|
|
public:
|
|
|
|
void operator()(PollableFdInfo *ptr);
|
|
|
|
};
|
|
|
|
|
|
|
|
class PollableFd;
|
|
|
|
class PollableFdRef {
|
|
|
|
public:
|
|
|
|
explicit PollableFdRef(ListNode *list_node) : list_node_(list_node) {
|
|
|
|
}
|
|
|
|
PollableFd lock();
|
|
|
|
|
|
|
|
private:
|
|
|
|
ListNode *list_node_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class PollableFd {
|
|
|
|
public:
|
|
|
|
// Interface for kqueue, epoll and e.t.c.
|
|
|
|
const NativeFd &native_fd() const;
|
|
|
|
|
|
|
|
ListNode *release_as_list_node();
|
|
|
|
PollableFdRef ref();
|
|
|
|
static PollableFd from_list_node(ListNode *node);
|
|
|
|
void add_flags(PollFlags flags);
|
|
|
|
PollFlags get_flags_unsafe() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<PollableFdInfo, PollableFdInfoUnlock> fd_info_;
|
|
|
|
friend class PollableFdInfo;
|
|
|
|
|
|
|
|
explicit PollableFd(std::unique_ptr<PollableFdInfo, PollableFdInfoUnlock> fd_info) : fd_info_(std::move(fd_info)) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline PollableFd PollableFdRef::lock() {
|
|
|
|
return PollableFd::from_list_node(list_node_);
|
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class PollableFdInfo final : private ListNode {
|
2018-08-13 19:15:09 +02:00
|
|
|
public:
|
|
|
|
PollableFdInfo() = default;
|
2018-09-10 14:42:46 +02:00
|
|
|
PollableFdInfo(const PollableFdInfo &) = delete;
|
|
|
|
PollableFdInfo &operator=(const PollableFdInfo &) = delete;
|
|
|
|
PollableFdInfo(PollableFdInfo &&) = delete;
|
|
|
|
PollableFdInfo &operator=(PollableFdInfo &&) = delete;
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
PollableFd extract_pollable_fd(ObserverBase *observer) {
|
|
|
|
VLOG(fd) << native_fd() << " extract pollable fd " << tag("observer", observer);
|
|
|
|
CHECK(!empty());
|
|
|
|
bool was_locked = lock_.test_and_set(std::memory_order_acquire);
|
|
|
|
CHECK(!was_locked);
|
|
|
|
set_observer(observer);
|
|
|
|
return PollableFd{std::unique_ptr<PollableFdInfo, PollableFdInfoUnlock>{this}};
|
|
|
|
}
|
|
|
|
PollableFdRef get_pollable_fd_ref() {
|
|
|
|
CHECK(!empty());
|
|
|
|
bool was_locked = lock_.test_and_set(std::memory_order_acquire);
|
|
|
|
CHECK(was_locked);
|
|
|
|
return PollableFdRef{as_list_node()};
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_flags(PollFlags flags) {
|
|
|
|
flags_.write_flags_local(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear_flags(PollFlags flags) {
|
|
|
|
flags_.clear_flags(flags);
|
|
|
|
}
|
2020-07-21 17:29:39 +02:00
|
|
|
PollFlags sync_with_poll() const {
|
2018-08-13 19:15:09 +02:00
|
|
|
return flags_.read_flags();
|
|
|
|
}
|
|
|
|
PollFlags get_flags_local() const {
|
|
|
|
return flags_.read_flags_local();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const {
|
|
|
|
return !fd_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_native_fd(NativeFd new_native_fd) {
|
|
|
|
if (fd_) {
|
|
|
|
CHECK(!new_native_fd);
|
|
|
|
bool was_locked = lock_.test_and_set(std::memory_order_acquire);
|
|
|
|
CHECK(!was_locked);
|
|
|
|
lock_.clear(std::memory_order_release);
|
|
|
|
}
|
|
|
|
|
|
|
|
fd_ = std::move(new_native_fd);
|
|
|
|
}
|
|
|
|
explicit PollableFdInfo(NativeFd native_fd) {
|
|
|
|
set_native_fd(std::move(native_fd));
|
|
|
|
}
|
|
|
|
const NativeFd &native_fd() const {
|
|
|
|
//CHECK(!empty());
|
|
|
|
return fd_;
|
|
|
|
}
|
|
|
|
NativeFd move_as_native_fd() {
|
|
|
|
return std::move(fd_);
|
|
|
|
}
|
|
|
|
|
|
|
|
~PollableFdInfo() {
|
|
|
|
VLOG(fd) << native_fd() << " destroy PollableFdInfo";
|
2020-12-24 16:43:26 +01:00
|
|
|
bool was_locked = lock_.test_and_set(std::memory_order_acquire);
|
|
|
|
CHECK(!was_locked);
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void add_flags_from_poll(PollFlags flags) {
|
|
|
|
VLOG(fd) << native_fd() << " add flags from poll " << flags;
|
|
|
|
if (flags_.write_flags(flags)) {
|
|
|
|
notify_observer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
NativeFd fd_{};
|
2019-04-22 02:18:33 +02:00
|
|
|
std::atomic_flag lock_ = ATOMIC_FLAG_INIT;
|
2018-08-13 19:15:09 +02:00
|
|
|
PollFlagsSet flags_;
|
|
|
|
#if TD_PORT_WINDOWS
|
|
|
|
SpinLock observer_lock_;
|
|
|
|
#endif
|
|
|
|
ObserverBase *observer_{nullptr};
|
|
|
|
|
|
|
|
friend class PollableFd;
|
|
|
|
friend class PollableFdInfoUnlock;
|
|
|
|
|
|
|
|
void set_observer(ObserverBase *observer) {
|
|
|
|
#if TD_PORT_WINDOWS
|
|
|
|
auto lock = observer_lock_.lock();
|
|
|
|
#endif
|
|
|
|
CHECK(!observer_);
|
|
|
|
observer_ = observer;
|
|
|
|
}
|
|
|
|
void clear_observer() {
|
|
|
|
#if TD_PORT_WINDOWS
|
|
|
|
auto lock = observer_lock_.lock();
|
|
|
|
#endif
|
|
|
|
observer_ = nullptr;
|
|
|
|
}
|
|
|
|
void notify_observer() {
|
|
|
|
#if TD_PORT_WINDOWS
|
|
|
|
auto lock = observer_lock_.lock();
|
|
|
|
#endif
|
|
|
|
VLOG(fd) << native_fd() << " notify " << tag("observer", observer_);
|
|
|
|
if (observer_) {
|
|
|
|
observer_->notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock() {
|
2018-08-17 17:48:32 +02:00
|
|
|
clear_observer();
|
2018-08-13 19:15:09 +02:00
|
|
|
lock_.clear(std::memory_order_release);
|
|
|
|
as_list_node()->remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
ListNode *as_list_node() {
|
|
|
|
return static_cast<ListNode *>(this);
|
|
|
|
}
|
|
|
|
static PollableFdInfo *from_list_node(ListNode *list_node) {
|
|
|
|
return static_cast<PollableFdInfo *>(list_node);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
inline void PollableFdInfoUnlock::operator()(PollableFdInfo *ptr) {
|
|
|
|
ptr->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ListNode *PollableFd::release_as_list_node() {
|
|
|
|
return fd_info_.release()->as_list_node();
|
|
|
|
}
|
|
|
|
inline PollableFdRef PollableFd::ref() {
|
|
|
|
return PollableFdRef{fd_info_->as_list_node()};
|
|
|
|
}
|
|
|
|
inline PollableFd PollableFd::from_list_node(ListNode *node) {
|
|
|
|
return PollableFd(std::unique_ptr<PollableFdInfo, PollableFdInfoUnlock>(PollableFdInfo::from_list_node(node)));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void PollableFd::add_flags(PollFlags flags) {
|
|
|
|
fd_info_->add_flags_from_poll(flags);
|
|
|
|
}
|
|
|
|
inline PollFlags PollableFd::get_flags_unsafe() const {
|
|
|
|
return fd_info_->get_flags_local();
|
|
|
|
}
|
|
|
|
inline const NativeFd &PollableFd::native_fd() const {
|
|
|
|
return fd_info_->native_fd();
|
|
|
|
}
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
template <class FdT>
|
2020-07-21 17:29:39 +02:00
|
|
|
void sync_with_poll(const FdT &fd) {
|
|
|
|
fd.get_poll_info().sync_with_poll();
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class FdT>
|
2020-07-21 17:29:39 +02:00
|
|
|
bool can_read_local(const FdT &fd) {
|
|
|
|
return fd.get_poll_info().get_flags_local().can_read() || fd.get_poll_info().get_flags_local().has_pending_error();
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class FdT>
|
2020-07-21 17:29:39 +02:00
|
|
|
bool can_write_local(const FdT &fd) {
|
|
|
|
return fd.get_poll_info().get_flags_local().can_write();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class FdT>
|
|
|
|
bool can_close_local(const FdT &fd) {
|
|
|
|
return fd.get_poll_info().get_flags_local().can_close();
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|