COROIO: coroio/notifier.hpp Source File
COROIO
Loading...
Searching...
No Matches
notifier.hpp
1#pragma once
2
4#include <coroio/socket.hpp>
5
6#include <array>
7#include <atomic>
8#include <cassert>
9#include <cerrno>
10#include <cstdint>
11#include <stdexcept>
12#include <system_error>
13#include <type_traits>
14
15#ifndef _WIN32
16#include <unistd.h>
17#endif
18
19namespace NNet {
20
21#ifndef _WIN32
22
29class INotifier {
30public:
31 virtual ~INotifier() = default;
32
39 virtual void Notify() = 0;
40
50 virtual TFuture<void> Wait() = 0;
51};
52
53namespace NDetail {
54
56std::array<int, 2> MakeNotifierFds();
57
59void WriteNotifierSignal(int fd);
60
61} // namespace NDetail
62
89template<typename TPoller>
90class TNotifier final: public INotifier {
91 using TFileHandle = typename TPoller::TFileHandle;
92
93public:
95 explicit TNotifier(TPoller& poller)
96 : TNotifier(poller, NDetail::MakeNotifierFds())
97 { }
98
99 ~TNotifier() override {
100 assert(!Waiting_);
101#ifndef __linux__
102 ::close(WriteFd_);
103#endif
104 }
105
106 TNotifier(const TNotifier&) = delete;
107 TNotifier& operator=(const TNotifier&) = delete;
108
109 void Notify() override {
110 if (Pending_.exchange(true, std::memory_order_acq_rel)) {
111 return;
112 }
113 try {
114 NDetail::WriteNotifierSignal(WriteFd_);
115 } catch (...) {
116 Pending_.store(false, std::memory_order_release);
117 throw;
118 }
119 }
120
121 TFuture<void> Wait() override {
122 if (Waiting_) {
123 throw std::logic_error("notifier already has a listener");
124 }
125 Waiting_ = true;
126 struct TWaitGuard {
127 TNotifier& Owner;
128 bool Completed = false;
129 ~TWaitGuard() {
130 Owner.Waiting_ = false;
131 if constexpr (!std::is_same_v<TFileHandle, NNet::TFileHandle>) {
132 // A cancelled driven read may have consumed the signal already;
133 // signal again so the notification is retained. Worst case is a spurious wakeup.
134 if (!Completed && Owner.Pending_.load(std::memory_order_acquire)) {
135 try {
136 NDetail::WriteNotifierSignal(Owner.WriteFd_);
137 } catch (...) {
138 }
139 }
140 }
141 }
142 } guard{*this};
143
144#ifdef __linux__
145 uint64_t signal;
146#else
147 uint8_t signal;
148#endif
149 for (;;) {
150 const auto size = co_await Handle_.ReadSome(&signal, sizeof(signal));
151 if (size == static_cast<decltype(size)>(sizeof(signal))) {
152 break;
153 }
154 if (size >= 0) {
155 throw std::system_error(EIO, std::generic_category(), "notifier read");
156 }
157 }
158 // Re-arm before the caller drains its work, so concurrent work can signal again.
159 Pending_.exchange(false, std::memory_order_acq_rel);
160 guard.Completed = true;
161 }
162
163private:
164 TNotifier(TPoller& poller, std::array<int, 2> fds)
165 : Handle_(fds[0], poller)
166 , WriteFd_(fds[1])
167 { }
168
169 TFileHandle Handle_;
170 int WriteFd_ = -1;
171 std::atomic<bool> Pending_ = false;
172 bool Waiting_ = false;
173};
174
175#endif // _WIN32
176
177} // namespace NNet
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