pub struct EpollContext<T> { /* private fields */ }
Expand description
EpollContext wraps linux epoll. It provides similar interface to PollContext. It is thread safe while PollContext is not. It requires user to pass in a reference of EpollEvents while PollContext does not. Always use PollContext if you don’t need to access the same epoll from different threads.
Implementations§
Source§impl<T: PollToken> EpollContext<T>
impl<T: PollToken> EpollContext<T>
Sourcepub fn new() -> Result<EpollContext<T>>
pub fn new() -> Result<EpollContext<T>>
Creates a new EpollContext
.
Sourcepub fn add(&self, fd: &dyn AsRawFd, token: T) -> Result<()>
pub fn add(&self, fd: &dyn AsRawFd, token: T) -> Result<()>
Adds the given fd
to this context and associates the given token
with the fd
’s
readable events.
A fd
can only be added once and does not need to be kept open. If the fd
is dropped and
there were no duplicated file descriptors (i.e. adding the same descriptor with a different
FD number) added to this context, events will not be reported by wait
anymore.
Sourcepub fn add_fd_with_events(
&self,
fd: &dyn AsRawFd,
events: WatchingEvents,
token: T,
) -> Result<()>
pub fn add_fd_with_events( &self, fd: &dyn AsRawFd, events: WatchingEvents, token: T, ) -> Result<()>
Adds the given fd
to this context, watching for the specified events and associates the
given ‘token’ with those events.
A fd
can only be added once and does not need to be kept open. If the fd
is dropped and
there were no duplicated file descriptors (i.e. adding the same descriptor with a different
FD number) added to this context, events will not be reported by wait
anymore.
Sourcepub fn modify(
&self,
fd: &dyn AsRawFd,
events: WatchingEvents,
token: T,
) -> Result<()>
pub fn modify( &self, fd: &dyn AsRawFd, events: WatchingEvents, token: T, ) -> Result<()>
If fd
was previously added to this context, the watched events will be replaced with
events
and the token associated with it will be replaced with the given token
.
Sourcepub fn delete(&self, fd: &dyn AsRawFd) -> Result<()>
pub fn delete(&self, fd: &dyn AsRawFd) -> Result<()>
Deletes the given fd
from this context.
If an fd
’s token shows up in the list of hangup events, it should be removed using this
method or by closing/dropping (if and only if the fd was never dup()’d/fork()’d) the fd
.
Failure to do so will cause the wait
method to always return immediately, causing ~100%
CPU load.
Sourcepub fn wait<'a>(&self, events: &'a EpollEvents) -> Result<PollEvents<'a, T>>
pub fn wait<'a>(&self, events: &'a EpollEvents) -> Result<PollEvents<'a, T>>
Waits for any events to occur in FDs that were previously added to this context.
The events are level-triggered, meaning that if any events are unhandled (i.e. not reading
for readable events and not closing for hungup events), subsequent calls to wait
will
return immediately. The consequence of not handling an event perpetually while calling
wait
is that the callers loop will degenerated to busy loop polling, pinning a CPU to
~100% usage.
Sourcepub fn wait_timeout<'a>(
&self,
events: &'a EpollEvents,
timeout: Duration,
) -> Result<PollEvents<'a, T>>
pub fn wait_timeout<'a>( &self, events: &'a EpollEvents, timeout: Duration, ) -> Result<PollEvents<'a, T>>
Like wait
except will only block for a maximum of the given timeout
.
This may return earlier than timeout
with zero events if the duration indicated exceeds
system limits.