125 Changes_.emplace_back(
TEvent{fd, TEvent::READ|TEvent::WRITE|TEvent::RHUP, {}});
148 struct TAwaitableSleep {
159 TAwaitableSleep(TAwaitableSleep&& other)
160 : poller(other.poller)
163 other.poller =
nullptr;
166 TAwaitableSleep(
const TAwaitableSleep&) =
delete;
167 TAwaitableSleep& operator=(
const TAwaitableSleep&) =
delete;
173 void await_suspend(std::coroutine_handle<> h) {
177 void await_resume() { poller =
nullptr; }
181 unsigned timerId = 0;
184 return TAwaitableSleep{
this,until};
194 template<
typename Rep,
typename Period>
195 auto Sleep(std::chrono::duration<Rep,Period> duration) {
196 return Sleep(TClock::now() + duration);
206 return Sleep(TTime{});
236 change.Handle.resume();
237 if (change.Fd >= 0) {
238 bool matched =
false;
239 for (; index <
Changes_.size() && !matched; index++) {
240 matched =
Changes_[index].Match(change);
244 Changes_.emplace_back(std::move(change));
281 :
Timers_.top().Deadline == TTime{}
292 auto p1 = std::chrono::duration_cast<std::chrono::seconds>(duration);
293 auto p2 = std::chrono::duration_cast<std::chrono::nanoseconds>(duration - p1);
295 ts.tv_sec = p1.count();
296 ts.tv_nsec = p2.count();
312 auto now = TClock::now();
319 if ((first || prevId != timer.Id) && timer.Handle) {
321 timer.Handle.resume();
338 std::chrono::milliseconds
MaxDuration_ = std::chrono::milliseconds(100);
timespec GetTimeout() const
Computes the poll timeout based on scheduled timers.
Definition poller.hpp:278
void AddRemoteHup(int fd, THandle h)
Registers a remote hang-up (RHUP) event.
Definition poller.hpp:111
TPollerBase()=default
Default constructor.
std::chrono::milliseconds MaxDuration_
Maximum poll duration.
Definition poller.hpp:338
TPollerBase(const TPollerBase &)=delete
Copying is disabled.
void Wakeup(TEvent &&change)
Wakes up a coroutine waiting on an event.
Definition poller.hpp:217
void Reset()
Clears the lists of ready events and pending changes.
Definition poller.hpp:300
void SetMaxDuration(std::chrono::milliseconds maxDuration)
Sets the maximum polling duration.
Definition poller.hpp:263
void ProcessTimers()
Processes scheduled timers.
Definition poller.hpp:311
unsigned LastFiredTimer_
ID of the last fired timer.
Definition poller.hpp:337
unsigned AddTimer(TTime deadline, THandle h)
Schedules a timer.
Definition poller.hpp:62
std::vector< TEvent > ReadyEvents_
Events ready to wake up their coroutines.
Definition poller.hpp:333
void AddWrite(int fd, THandle h)
Registers a write event on a file descriptor.
Definition poller.hpp:101
int MaxFd_
Highest file descriptor in use.
Definition poller.hpp:331
auto Sleep(TTime until)
Suspends execution until the specified time.
Definition poller.hpp:147
std::priority_queue< TTimer > Timers_
Priority queue for scheduled timers.
Definition poller.hpp:335
timespec MaxDurationTs_
Max duration represented as timespec.
Definition poller.hpp:339
bool RemoveTimer(unsigned timerId, TTime deadline)
Removes or cancels a timer.
Definition poller.hpp:77
auto TimersSize() const
Returns the number of scheduled timers.
Definition poller.hpp:268
void RemoveEvent(int fd)
Removes registered events for a specific file descriptor.
Definition poller.hpp:122
void AddRead(int fd, THandle h)
Registers a read event on a file descriptor.
Definition poller.hpp:91
void RemoveEvent(THandle)
Removes events associated with a given coroutine handle.
Definition poller.hpp:135
TTime LastTimersProcessTime_
Last time timers were processed.
Definition poller.hpp:336
std::vector< TEvent > Changes_
Pending changes (registered events).
Definition poller.hpp:332
static constexpr timespec GetMaxDuration(std::chrono::milliseconds duration)
Computes a timespec from a duration.
Definition poller.hpp:291
unsigned TimerId_
Counter for generating unique timer IDs.
Definition poller.hpp:334
auto Yield()
Yields execution to the next event loop iteration.
Definition poller.hpp:205
void WakeupReadyHandles()
Wakes up all coroutines waiting on ready events.
Definition poller.hpp:253
auto Sleep(std::chrono::duration< Rep, Period > duration)
Overload of Sleep() accepting a duration.
Definition poller.hpp:195