|
| TPollerBase ()=default |
| Default constructor.
|
|
| TPollerBase (const TPollerBase &)=delete |
| Copying is disabled.
|
|
TPollerBase & | operator= (const TPollerBase &)=delete |
|
unsigned | AddTimer (TTime deadline, THandle h) |
| Schedules a timer.
|
|
bool | RemoveTimer (unsigned timerId, TTime deadline) |
| Removes or cancels a timer.
|
|
void | AddRead (int fd, THandle h) |
| Registers a read event on a file descriptor.
|
|
void | AddWrite (int fd, THandle h) |
| Registers a write event on a file descriptor.
|
|
void | AddRemoteHup (int fd, THandle h) |
| Registers a remote hang-up (RHUP) event.
|
|
void | RemoveEvent (int fd) |
| Removes registered events for a specific file descriptor.
|
|
void | RemoveEvent (THandle) |
| Removes events associated with a given coroutine handle.
|
|
auto | Sleep (TTime until) |
| Suspends execution until the specified time.
|
|
template<typename Rep, typename Period> |
auto | Sleep (std::chrono::duration< Rep, Period > duration) |
| Overload of Sleep() accepting a duration.
|
|
auto | Yield () |
| Yields execution to the next event loop iteration.
|
|
void | Wakeup (TEvent &&change) |
| Wakes up a coroutine waiting on an event.
|
|
void | WakeupReadyHandles () |
| Wakes up all coroutines waiting on ready events.
|
|
void | SetMaxDuration (std::chrono::milliseconds maxDuration) |
| Sets the maximum polling duration.
|
|
auto | TimersSize () const |
| Returns the number of scheduled timers.
|
|
|
int | MaxFd_ = -1 |
| Highest file descriptor in use.
|
|
std::vector< TEvent > | Changes_ |
| Pending changes (registered events).
|
|
std::vector< TEvent > | ReadyEvents_ |
| Events ready to wake up their coroutines.
|
|
unsigned | TimerId_ = 0 |
| Counter for generating unique timer IDs.
|
|
std::priority_queue< TTimer > | Timers_ |
| Priority queue for scheduled timers.
|
|
TTime | LastTimersProcessTime_ |
| Last time timers were processed.
|
|
unsigned | LastFiredTimer_ = (unsigned)(-1) |
| ID of the last fired timer.
|
|
std::chrono::milliseconds | MaxDuration_ = std::chrono::milliseconds(100) |
| Maximum poll duration.
|
|
timespec | MaxDurationTs_ = GetMaxDuration(MaxDuration_) |
| Max duration represented as timespec.
|
|
Base class for pollers managing asynchronous I/O events and timers.
TPollerBase provides the common interface and functionality for pollers that drive asynchronous operations. It allows scheduling timers, registering I/O events (read, write, remote hang-up) on file descriptors, and waking up waiting coroutines. It also offers convenience methods such as Sleep() (and its overload accepting a std::chrono::duration) and Yield().
The class maintains internal collections for pending changes (events), ready events, and timers. It also keeps track of the maximum file descriptor, which is used in system-level polling calls.
Key methods include:
The class also provides helper methods for computing timeout values (via GetTimeout()).
The detailed comment inside Wakeup() describes how a waiting coroutine is resumed and how new event registrations are matched.
void NNet::TPollerBase::Wakeup |
( |
TEvent && | change | ) |
|
|
inline |
Wakes up a coroutine waiting on an event.
This method resumes the coroutine associated with the given TEvent (change
), then checks whether that coroutine has added a new wait for the same file descriptor.
- Parameters
-
change | The event change containing the file descriptor and coroutine handle. |
Wakes up the coroutine associated with the given TEvent change
, then checks whether that coroutine has added a new wait on the same file descriptor (Fd). We first store the current size of the Changes_
vector and call change.Handle.resume()
.
If after resuming, the coroutine does NOT add (or match) a wait entry for change.Fd
, we assume that this descriptor is not in use yet and remove it from the poller by resetting change.Handle
and appending change
to Changes_
.
In most cases, Changes_
grows by exactly one entry after resuming a coroutine (it suspends again on a descriptor), but there are scenarios where it can grow by more than one. For example, one coroutine might launch another coroutine without waiting; that second coroutine suspends on ReadSome/WriteSome, returns control to the first coroutine, which then also suspends on ReadSome/WriteSome. In that chain of calls, multiple new waits can be added before returning here, so Changes_
can increase by 2 (or potentially more) in a single wake-up cycle.