4#include <coroio/socket.hpp>
12#include <system_error>
56std::array<int, 2> MakeNotifierFds();
59void WriteNotifierSignal(
int fd);
89template<
typename TPoller>
91 using TFileHandle =
typename TPoller::TFileHandle;
96 :
TNotifier(poller, NDetail::MakeNotifierFds())
110 if (Pending_.exchange(
true, std::memory_order_acq_rel)) {
114 NDetail::WriteNotifierSignal(WriteFd_);
116 Pending_.store(
false, std::memory_order_release);
123 throw std::logic_error(
"notifier already has a listener");
128 bool Completed =
false;
130 Owner.Waiting_ =
false;
131 if constexpr (!std::is_same_v<TFileHandle, NNet::TFileHandle>) {
134 if (!Completed && Owner.Pending_.load(std::memory_order_acquire)) {
136 NDetail::WriteNotifierSignal(Owner.WriteFd_);
150 const auto size =
co_await Handle_.ReadSome(&signal,
sizeof(signal));
151 if (size ==
static_cast<decltype(size)
>(
sizeof(signal))) {
155 throw std::system_error(EIO, std::generic_category(),
"notifier read");
159 Pending_.exchange(
false, std::memory_order_acq_rel);
160 guard.Completed =
true;
164 TNotifier(TPoller& poller, std::array<int, 2> fds)
165 : Handle_(fds[0], poller)
171 std::atomic<bool> Pending_ =
false;
172 bool Waiting_ =
false;
Interface of a thread-safe notification with a single asynchronous listener.
Definition notifier.hpp:29
virtual void Notify()=0
Signals the listener from any thread.
virtual TFuture< void > Wait()=0
Asynchronously waits for and consumes a notification.
Thread-safe notification with a single asynchronous listener.
Definition notifier.hpp:90
TNotifier(TPoller &poller)
Creates a notifier bound to the listener's event-loop poller.
Definition notifier.hpp:95
TFuture< void > Wait() override
Asynchronously waits for and consumes a notification.
Definition notifier.hpp:121
void Notify() override
Signals the listener from any thread.
Definition notifier.hpp:109
Implementation of a promise/future system for coroutines.
Owned coroutine handle that carries a result of type T.
Definition corochain.hpp:185