2023-03-06 00:25:25 +00:00
|
|
|
//! Portable interface to epoll, kqueue, event ports, and IOCP.
|
2020-08-06 13:02:59 +00:00
|
|
|
//!
|
|
|
|
//! Supported platforms:
|
2024-02-11 16:31:13 +00:00
|
|
|
//! - [epoll](https://en.wikipedia.org/wiki/Epoll): Linux, Android, RedoxOS
|
2022-12-20 03:54:45 +00:00
|
|
|
//! - [kqueue](https://en.wikipedia.org/wiki/Kqueue): macOS, iOS, tvOS, watchOS, FreeBSD, NetBSD, OpenBSD,
|
2020-08-06 13:02:59 +00:00
|
|
|
//! DragonFly BSD
|
2020-08-10 17:16:06 +00:00
|
|
|
//! - [event ports](https://illumos.org/man/port_create): illumos, Solaris
|
2024-03-13 04:33:06 +00:00
|
|
|
//! - [poll](https://en.wikipedia.org/wiki/Poll_(Unix)): VxWorks, Fuchsia, HermitOS, other Unix systems
|
2023-03-06 00:25:25 +00:00
|
|
|
//! - [IOCP](https://learn.microsoft.com/en-us/windows/win32/fileio/i-o-completion-ports): Windows, Wine (version 7.13+)
|
2020-08-06 13:02:59 +00:00
|
|
|
//!
|
2022-12-30 22:43:47 +00:00
|
|
|
//! By default, polling is done in oneshot mode, which means interest in I/O events needs to
|
|
|
|
//! be re-enabled after an event is delivered if we're interested in the next event of the same
|
|
|
|
//! kind. However, level and edge triggered modes are also available for certain operating
|
|
|
|
//! systems. See the documentation of the [`PollMode`] type for more information.
|
2020-08-06 13:02:59 +00:00
|
|
|
//!
|
|
|
|
//! Only one thread can be waiting for I/O events at a time.
|
|
|
|
//!
|
|
|
|
//! # Examples
|
|
|
|
//!
|
|
|
|
//! ```no_run
|
2023-08-14 17:03:20 +00:00
|
|
|
//! use polling::{Event, Events, Poller};
|
2020-08-06 13:02:59 +00:00
|
|
|
//! use std::net::TcpListener;
|
|
|
|
//!
|
2020-08-07 12:56:53 +00:00
|
|
|
//! // Create a TCP listener.
|
2020-08-06 13:02:59 +00:00
|
|
|
//! let socket = TcpListener::bind("127.0.0.1:8000")?;
|
2020-09-30 23:08:20 +00:00
|
|
|
//! socket.set_nonblocking(true)?;
|
2020-10-02 14:40:09 +00:00
|
|
|
//! let key = 7; // Arbitrary key identifying the socket.
|
2020-09-30 23:08:20 +00:00
|
|
|
//!
|
2020-08-06 13:02:59 +00:00
|
|
|
//! // Create a poller and register interest in readability on the socket.
|
|
|
|
//! let poller = Poller::new()?;
|
2023-08-04 03:15:59 +00:00
|
|
|
//! unsafe {
|
|
|
|
//! poller.add(&socket, Event::readable(key))?;
|
|
|
|
//! }
|
2020-08-06 13:02:59 +00:00
|
|
|
//!
|
|
|
|
//! // The event loop.
|
2023-08-14 17:03:20 +00:00
|
|
|
//! let mut events = Events::new();
|
2020-08-06 13:02:59 +00:00
|
|
|
//! loop {
|
|
|
|
//! // Wait for at least one I/O event.
|
|
|
|
//! events.clear();
|
|
|
|
//! poller.wait(&mut events, None)?;
|
|
|
|
//!
|
2023-08-14 17:03:20 +00:00
|
|
|
//! for ev in events.iter() {
|
2020-08-06 13:02:59 +00:00
|
|
|
//! if ev.key == key {
|
|
|
|
//! // Perform a non-blocking accept operation.
|
|
|
|
//! socket.accept()?;
|
|
|
|
//! // Set interest in the next readability event.
|
Separate adding and modifying of file descriptors
This replaces Poller.insert() and Poller.interest() with Poller.add()
and Poller.modify(), and renames Poller.remove() to Poller.delete().
The method Poller.add() is used for adding a new file descriptor, while
Poller.modify() is used for updating an existing one. Poller.remove() is
renamed to Poller.delete() so the naming scheme of these methods follows
that of epoll, wepoll, etc.
This new setup means that adding a new socket only requires a single
call of Poller.add(), instead of a combination of Poller.insert() and
Poller.interest(). This reduces the amount of system calls necessary,
and leads to a more pleasant API.
On systems that use kqueue or ports, the behaviour of Poller.add() and
Poller.modify() is the same. This is because on these systems adding an
already existing file descriptor will just update its configuration.
This however is an implementation detail and should not be relied upon
by users.
Migrating to this new API is pretty simple, simply replace this:
poller.insert(&socket);
poller.interest(&socket, event);
With this:
poller.add(&socket, event);
And for cases where Poller.interest() was used for updating an existing
file descriptor, simply replace it will a call to Poller.modify().
See https://github.com/stjepang/polling/issues/16 and
https://github.com/stjepang/polling/pull/17 for more information.
2020-10-01 19:26:44 +00:00
|
|
|
//! poller.modify(&socket, Event::readable(key))?;
|
2020-08-06 13:02:59 +00:00
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! }
|
2023-08-04 03:15:59 +00:00
|
|
|
//!
|
|
|
|
//! poller.delete(&socket)?;
|
2020-08-06 13:02:59 +00:00
|
|
|
//! # std::io::Result::Ok(())
|
|
|
|
//! ```
|
|
|
|
|
|
|
|
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
|
2023-08-04 03:15:59 +00:00
|
|
|
#![allow(clippy::useless_conversion, clippy::unnecessary_cast, unused_unsafe)]
|
2022-08-21 10:47:00 +00:00
|
|
|
#![cfg_attr(docsrs, feature(doc_cfg))]
|
2023-07-17 05:30:22 +00:00
|
|
|
#![doc(
|
|
|
|
html_favicon_url = "https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png"
|
|
|
|
)]
|
|
|
|
#![doc(
|
|
|
|
html_logo_url = "https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png"
|
|
|
|
)]
|
2020-08-06 13:02:59 +00:00
|
|
|
|
2023-08-14 17:03:20 +00:00
|
|
|
use std::cell::Cell;
|
2020-08-06 13:02:59 +00:00
|
|
|
use std::fmt;
|
|
|
|
use std::io;
|
2023-08-14 17:03:20 +00:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::num::NonZeroUsize;
|
2020-08-14 14:07:53 +00:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2020-08-06 13:02:59 +00:00
|
|
|
use std::sync::Mutex;
|
2023-10-27 14:02:08 +00:00
|
|
|
use std::time::{Duration, Instant};
|
2020-08-06 13:02:59 +00:00
|
|
|
|
|
|
|
use cfg_if::cfg_if;
|
|
|
|
|
|
|
|
cfg_if! {
|
2022-12-29 07:01:25 +00:00
|
|
|
// Note: This cfg is intended to make it easy for polling developers to test
|
|
|
|
// the backend that uses poll, and is not a public API.
|
|
|
|
if #[cfg(polling_test_poll_backend)] {
|
|
|
|
mod poll;
|
|
|
|
use poll as sys;
|
2024-02-11 16:31:13 +00:00
|
|
|
} else if #[cfg(any(
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "android",
|
|
|
|
target_os = "redox"
|
|
|
|
))] {
|
2020-08-06 13:02:59 +00:00
|
|
|
mod epoll;
|
2022-01-08 16:58:56 +00:00
|
|
|
use epoll as sys;
|
2020-12-18 14:33:05 +00:00
|
|
|
} else if #[cfg(any(
|
2020-08-06 20:28:38 +00:00
|
|
|
target_os = "illumos",
|
|
|
|
target_os = "solaris",
|
|
|
|
))] {
|
|
|
|
mod port;
|
2022-01-08 16:58:56 +00:00
|
|
|
use port as sys;
|
2020-08-06 13:02:59 +00:00
|
|
|
} else if #[cfg(any(
|
|
|
|
target_os = "macos",
|
|
|
|
target_os = "ios",
|
2022-12-20 03:54:45 +00:00
|
|
|
target_os = "tvos",
|
2022-12-28 03:00:18 +00:00
|
|
|
target_os = "watchos",
|
2020-08-06 13:02:59 +00:00
|
|
|
target_os = "freebsd",
|
|
|
|
target_os = "netbsd",
|
|
|
|
target_os = "openbsd",
|
|
|
|
target_os = "dragonfly",
|
|
|
|
))] {
|
|
|
|
mod kqueue;
|
2022-01-08 16:58:56 +00:00
|
|
|
use kqueue as sys;
|
2020-12-17 18:56:48 +00:00
|
|
|
} else if #[cfg(any(
|
|
|
|
target_os = "vxworks",
|
2024-03-13 04:33:06 +00:00
|
|
|
target_os = "hermit",
|
2020-12-17 18:56:48 +00:00
|
|
|
target_os = "fuchsia",
|
2022-08-18 16:52:28 +00:00
|
|
|
target_os = "horizon",
|
2020-12-17 18:56:48 +00:00
|
|
|
unix,
|
|
|
|
))] {
|
|
|
|
mod poll;
|
2022-01-08 16:58:56 +00:00
|
|
|
use poll as sys;
|
2020-08-06 13:02:59 +00:00
|
|
|
} else if #[cfg(target_os = "windows")] {
|
2023-03-06 00:25:25 +00:00
|
|
|
mod iocp;
|
|
|
|
use iocp as sys;
|
2020-08-06 13:02:59 +00:00
|
|
|
} else {
|
|
|
|
compile_error!("polling does not support this target OS");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 19:14:33 +00:00
|
|
|
pub mod os;
|
|
|
|
|
2020-09-01 04:48:09 +00:00
|
|
|
/// Key associated with notifications.
|
2023-10-06 03:10:18 +00:00
|
|
|
const NOTIFY_KEY: usize = usize::MAX;
|
2020-09-01 04:48:09 +00:00
|
|
|
|
2020-08-06 13:02:59 +00:00
|
|
|
/// Indicates that a file descriptor or socket can read or write without blocking.
|
2021-09-04 18:19:02 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
2020-08-06 13:02:59 +00:00
|
|
|
pub struct Event {
|
|
|
|
/// Key identifying the file descriptor or socket.
|
|
|
|
pub key: usize,
|
|
|
|
/// Can it do a read operation without blocking?
|
|
|
|
pub readable: bool,
|
|
|
|
/// Can it do a write operation without blocking?
|
|
|
|
pub writable: bool,
|
2023-08-14 17:03:20 +00:00
|
|
|
/// System-specific event data.
|
|
|
|
extra: sys::EventExtra,
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 22:43:47 +00:00
|
|
|
/// The mode in which the poller waits for I/O events.
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
#[non_exhaustive]
|
|
|
|
pub enum PollMode {
|
|
|
|
/// Poll in oneshot mode.
|
|
|
|
///
|
|
|
|
/// In this mode, the poller will only deliver one event per file descriptor or socket.
|
|
|
|
/// Once an event has been delivered, interest in the event needs to be re-enabled
|
|
|
|
/// by calling `Poller::modify` or `Poller::add`.
|
|
|
|
///
|
|
|
|
/// This is the default mode.
|
|
|
|
Oneshot,
|
|
|
|
|
|
|
|
/// Poll in level-triggered mode.
|
|
|
|
///
|
|
|
|
/// Once an event has been delivered, polling will continue to deliver that event
|
|
|
|
/// until interest in the event is disabled by calling `Poller::modify` or `Poller::delete`.
|
|
|
|
///
|
|
|
|
/// Not all operating system support this mode. Trying to register a file descriptor with
|
|
|
|
/// this mode in an unsupported operating system will raise an error. You can check if
|
|
|
|
/// the operating system supports this mode by calling `Poller::supports_level`.
|
|
|
|
Level,
|
|
|
|
|
|
|
|
/// Poll in edge-triggered mode.
|
|
|
|
///
|
|
|
|
/// Once an event has been delivered, polling will not deliver that event again unless
|
|
|
|
/// a new event occurs.
|
|
|
|
///
|
|
|
|
/// Not all operating system support this mode. Trying to register a file descriptor with
|
|
|
|
/// this mode in an unsupported operating system will raise an error. You can check if
|
|
|
|
/// the operating system supports this mode by calling `Poller::supports_edge`.
|
|
|
|
Edge,
|
2023-03-25 14:22:45 +00:00
|
|
|
|
|
|
|
/// Poll in both edge-triggered and oneshot mode.
|
|
|
|
///
|
|
|
|
/// This mode is similar to the `Oneshot` mode, but it will only deliver one event per new
|
|
|
|
/// event.
|
|
|
|
///
|
|
|
|
/// Not all operating system support this mode. Trying to register a file descriptor with
|
|
|
|
/// this mode in an unsupported operating system will raise an error. You can check if
|
|
|
|
/// the operating system supports this mode by calling `Poller::supports_edge`.
|
|
|
|
EdgeOneshot,
|
2022-12-30 22:43:47 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 13:02:59 +00:00
|
|
|
impl Event {
|
2023-09-12 13:02:02 +00:00
|
|
|
/// Create a new event.
|
|
|
|
pub const fn new(key: usize, readable: bool, writable: bool) -> Event {
|
2020-08-06 13:02:59 +00:00
|
|
|
Event {
|
|
|
|
key,
|
2023-09-12 13:02:02 +00:00
|
|
|
readable,
|
|
|
|
writable,
|
2023-08-14 17:03:20 +00:00
|
|
|
extra: sys::EventExtra::empty(),
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-12 13:02:02 +00:00
|
|
|
/// All kinds of events (readable and writable).
|
|
|
|
///
|
|
|
|
/// Equivalent to: `Event::new(key, true, true)`
|
|
|
|
#[inline]
|
|
|
|
pub const fn all(key: usize) -> Event {
|
|
|
|
Event::new(key, true, true)
|
|
|
|
}
|
|
|
|
|
2020-08-06 13:02:59 +00:00
|
|
|
/// Only the readable event.
|
|
|
|
///
|
2023-09-12 13:02:02 +00:00
|
|
|
/// Equivalent to: `Event::new(key, true, false)`
|
|
|
|
#[inline]
|
2023-08-22 01:32:21 +00:00
|
|
|
pub const fn readable(key: usize) -> Event {
|
2023-09-12 13:02:02 +00:00
|
|
|
Event::new(key, true, false)
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Only the writable event.
|
|
|
|
///
|
2023-09-12 13:02:02 +00:00
|
|
|
/// Equivalent to: `Event::new(key, false, true)`
|
|
|
|
#[inline]
|
2023-08-22 01:32:21 +00:00
|
|
|
pub const fn writable(key: usize) -> Event {
|
2023-09-12 13:02:02 +00:00
|
|
|
Event::new(key, false, true)
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// No events.
|
|
|
|
///
|
2023-09-12 13:02:02 +00:00
|
|
|
/// Equivalent to: `Event::new(key, false, false)`
|
|
|
|
#[inline]
|
2023-08-22 01:32:21 +00:00
|
|
|
pub const fn none(key: usize) -> Event {
|
2023-09-12 13:02:02 +00:00
|
|
|
Event::new(key, false, false)
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
2023-08-14 17:03:20 +00:00
|
|
|
|
|
|
|
/// Add interruption events to this interest.
|
|
|
|
///
|
|
|
|
/// This usually indicates that the file descriptor or socket has been closed. It corresponds
|
|
|
|
/// to the `EPOLLHUP` and `POLLHUP` events.
|
|
|
|
///
|
|
|
|
/// Interruption events are only supported on the following platforms:
|
|
|
|
///
|
|
|
|
/// - `epoll`
|
|
|
|
/// - `poll`
|
|
|
|
/// - IOCP
|
|
|
|
/// - Event Ports
|
|
|
|
///
|
|
|
|
/// On other platforms, this function is a no-op.
|
|
|
|
#[inline]
|
|
|
|
pub fn set_interrupt(&mut self, active: bool) {
|
|
|
|
self.extra.set_hup(active);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add interruption events to this interest.
|
|
|
|
///
|
|
|
|
/// This usually indicates that the file descriptor or socket has been closed. It corresponds
|
|
|
|
/// to the `EPOLLHUP` and `POLLHUP` events.
|
|
|
|
///
|
|
|
|
/// Interruption events are only supported on the following platforms:
|
|
|
|
///
|
|
|
|
/// - `epoll`
|
|
|
|
/// - `poll`
|
|
|
|
/// - IOCP
|
|
|
|
/// - Event Ports
|
|
|
|
///
|
|
|
|
/// On other platforms, this function is a no-op.
|
|
|
|
#[inline]
|
|
|
|
pub fn with_interrupt(mut self) -> Self {
|
|
|
|
self.set_interrupt(true);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add priority events to this interest.
|
|
|
|
///
|
|
|
|
/// This indicates that there is urgent data to read. It corresponds to the `EPOLLPRI` and
|
|
|
|
/// `POLLPRI` events.
|
|
|
|
///
|
|
|
|
/// Priority events are only supported on the following platforms:
|
|
|
|
///
|
|
|
|
/// - `epoll`
|
|
|
|
/// - `poll`
|
|
|
|
/// - IOCP
|
|
|
|
/// - Event Ports
|
|
|
|
///
|
|
|
|
/// On other platforms, this function is a no-op.
|
|
|
|
#[inline]
|
|
|
|
pub fn set_priority(&mut self, active: bool) {
|
|
|
|
self.extra.set_pri(active);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add priority events to this interest.
|
|
|
|
///
|
|
|
|
/// This indicates that there is urgent data to read. It corresponds to the `EPOLLPRI` and
|
|
|
|
/// `POLLPRI` events.
|
|
|
|
///
|
|
|
|
/// Priority events are only supported on the following platforms:
|
|
|
|
///
|
|
|
|
/// - `epoll`
|
|
|
|
/// - `poll`
|
|
|
|
/// - IOCP
|
|
|
|
/// - Event Ports
|
|
|
|
///
|
|
|
|
/// On other platforms, this function is a no-op.
|
|
|
|
#[inline]
|
|
|
|
pub fn with_priority(mut self) -> Self {
|
|
|
|
self.set_priority(true);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tell if this event is the result of an interrupt notification.
|
|
|
|
///
|
|
|
|
/// This usually indicates that the file descriptor or socket has been closed. It corresponds
|
|
|
|
/// to the `EPOLLHUP` and `POLLHUP` events.
|
|
|
|
///
|
|
|
|
/// Interruption events are only supported on the following platforms:
|
|
|
|
///
|
|
|
|
/// - `epoll`
|
|
|
|
/// - `poll`
|
|
|
|
/// - IOCP
|
|
|
|
/// - Event Ports
|
|
|
|
///
|
|
|
|
/// On other platforms, this always returns `false`.
|
|
|
|
#[inline]
|
|
|
|
pub fn is_interrupt(&self) -> bool {
|
|
|
|
self.extra.is_hup()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tell if this event is the result of a priority notification.
|
|
|
|
///
|
|
|
|
/// This indicates that there is urgent data to read. It corresponds to the `EPOLLPRI` and
|
|
|
|
/// `POLLPRI` events.
|
|
|
|
///
|
|
|
|
/// Priority events are only supported on the following platforms:
|
|
|
|
///
|
|
|
|
/// - `epoll`
|
|
|
|
/// - `poll`
|
|
|
|
/// - IOCP
|
|
|
|
/// - Event Ports
|
|
|
|
///
|
|
|
|
/// On other platforms, this always returns `false`.
|
|
|
|
#[inline]
|
|
|
|
pub fn is_priority(&self) -> bool {
|
|
|
|
self.extra.is_pri()
|
|
|
|
}
|
|
|
|
|
2024-01-26 20:58:39 +00:00
|
|
|
/// Tells if this event is the result of a connection failure.
|
|
|
|
///
|
|
|
|
/// This function checks if a TCP connection has failed. It corresponds to the `EPOLLERR` or `EPOLLHUP` event in Linux
|
|
|
|
/// and `CONNECT_FAILED` event in Windows IOCP.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::{io, net};
|
|
|
|
/// // Assuming polling and socket2 are included as dependencies in Cargo.toml
|
|
|
|
/// use polling::Event;
|
|
|
|
/// use socket2::Type;
|
|
|
|
///
|
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let socket = socket2::Socket::new(socket2::Domain::IPV4, Type::STREAM, None)?;
|
|
|
|
/// let poller = polling::Poller::new()?;
|
|
|
|
/// unsafe {
|
|
|
|
/// poller.add(&socket, Event::new(0, true, true))?;
|
|
|
|
/// }
|
|
|
|
/// let addr = net::SocketAddr::new(net::Ipv4Addr::LOCALHOST.into(), 8080);
|
|
|
|
/// socket.set_nonblocking(true)?;
|
|
|
|
/// let _ = socket.connect(&addr.into());
|
|
|
|
///
|
|
|
|
/// let mut events = polling::Events::new();
|
|
|
|
///
|
|
|
|
/// events.clear();
|
|
|
|
/// poller.wait(&mut events, None)?;
|
|
|
|
///
|
|
|
|
/// let event = events.iter().next();
|
|
|
|
///
|
|
|
|
/// let event = match event {
|
|
|
|
/// Some(event) => event,
|
|
|
|
/// None => {
|
|
|
|
/// println!("no event");
|
|
|
|
/// return Ok(());
|
|
|
|
/// },
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// println!("event: {:?}", event);
|
|
|
|
/// if event
|
|
|
|
/// .is_connect_failed()
|
|
|
|
/// .unwrap_or_default()
|
|
|
|
/// {
|
|
|
|
/// println!("connect failed");
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
///
|
|
|
|
/// Returns `Some(true)` if the connection has failed, `Some(false)` if the connection has not failed,
|
|
|
|
/// or `None` if the platform does not support detecting this condition.
|
|
|
|
#[inline]
|
2024-03-21 05:04:46 +00:00
|
|
|
#[deprecated(
|
|
|
|
since = "3.4.0",
|
|
|
|
note = "use `is_err` in combination of is_hup instead, see documentation for `is_err`"
|
|
|
|
)]
|
2024-01-26 20:58:39 +00:00
|
|
|
pub fn is_connect_failed(&self) -> Option<bool> {
|
|
|
|
self.extra.is_connect_failed()
|
|
|
|
}
|
|
|
|
|
2024-03-21 05:04:46 +00:00
|
|
|
/// Tells if this event is the result of a connection failure.
|
|
|
|
///
|
2024-04-20 18:22:46 +00:00
|
|
|
/// This function checks if an error exist, particularly useful in detecting if TCP connection failed. It corresponds to the `EPOLLERR` event in Linux
|
2024-03-21 05:04:46 +00:00
|
|
|
/// and `CONNECT_FAILED` event in Windows IOCP.
|
|
|
|
///
|
|
|
|
/// ## Caveats
|
|
|
|
///
|
|
|
|
/// In `epoll`, a TCP connection failure is indicated by `EPOLLERR` + `EPOLLHUP`, though just `EPOLLERR` is enough to indicate a connection failure.
|
|
|
|
/// EPOLLHUP may happen when we haven't event called `connect` on the socket, but it is still a valid event to check for.
|
|
|
|
///
|
|
|
|
/// Returns `Some(true)` if the connection has failed, `Some(false)` if there is an error,
|
|
|
|
/// or `None` if the platform does not support detecting this condition.
|
|
|
|
#[inline]
|
|
|
|
pub fn is_err(&self) -> Option<bool> {
|
|
|
|
self.extra.is_err()
|
|
|
|
}
|
|
|
|
|
2023-08-14 17:03:20 +00:00
|
|
|
/// Remove any extra information from this event.
|
|
|
|
#[inline]
|
|
|
|
pub fn clear_extra(&mut self) {
|
|
|
|
self.extra = sys::EventExtra::empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a version of this event with no extra information.
|
|
|
|
///
|
|
|
|
/// This is useful for comparing events with `==`.
|
|
|
|
#[inline]
|
|
|
|
pub fn with_no_extra(mut self) -> Self {
|
|
|
|
self.clear_extra();
|
|
|
|
self
|
|
|
|
}
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Waits for I/O events.
|
|
|
|
pub struct Poller {
|
|
|
|
poller: sys::Poller,
|
2023-08-14 17:03:20 +00:00
|
|
|
lock: Mutex<()>,
|
2020-08-14 14:07:53 +00:00
|
|
|
notified: AtomicBool,
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Poller {
|
|
|
|
/// Creates a new poller.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use polling::Poller;
|
|
|
|
///
|
|
|
|
/// let poller = Poller::new()?;
|
|
|
|
/// # std::io::Result::Ok(())
|
|
|
|
/// ```
|
|
|
|
pub fn new() -> io::Result<Poller> {
|
2020-08-14 14:07:53 +00:00
|
|
|
Ok(Poller {
|
|
|
|
poller: sys::Poller::new()?,
|
2023-08-14 17:03:20 +00:00
|
|
|
lock: Mutex::new(()),
|
2020-08-14 14:07:53 +00:00
|
|
|
notified: AtomicBool::new(false),
|
|
|
|
})
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 22:43:47 +00:00
|
|
|
/// Tell whether or not this `Poller` supports level-triggered polling.
|
|
|
|
pub fn supports_level(&self) -> bool {
|
|
|
|
self.poller.supports_level()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tell whether or not this `Poller` supports edge-triggered polling.
|
|
|
|
pub fn supports_edge(&self) -> bool {
|
|
|
|
self.poller.supports_edge()
|
|
|
|
}
|
|
|
|
|
2020-10-02 14:40:09 +00:00
|
|
|
/// Adds a file descriptor or socket to the poller.
|
2020-08-06 13:02:59 +00:00
|
|
|
///
|
|
|
|
/// A file descriptor or socket is considered readable or writable when a read or write
|
|
|
|
/// operation on it would not block. This doesn't mean the read or write operation will
|
|
|
|
/// succeed, it only means the operation will return immediately.
|
|
|
|
///
|
|
|
|
/// If interest is set in both readability and writability, the two kinds of events might be
|
|
|
|
/// delivered either separately or together.
|
|
|
|
///
|
|
|
|
/// For example, interest in `Event { key: 7, readable: true, writable: true }` might result in
|
|
|
|
/// a single [`Event`] of the same form, or in two separate [`Event`]s:
|
|
|
|
/// - `Event { key: 7, readable: true, writable: false }`
|
|
|
|
/// - `Event { key: 7, readable: false, writable: true }`
|
|
|
|
///
|
2020-10-02 14:40:09 +00:00
|
|
|
/// Note that interest in I/O events needs to be re-enabled using
|
|
|
|
/// [`modify()`][`Poller::modify()`] again after an event is delivered if we're interested in
|
|
|
|
/// the next event of the same kind.
|
|
|
|
///
|
2023-08-16 16:48:14 +00:00
|
|
|
/// It is possible to register interest in the same file descriptor or socket using multiple
|
|
|
|
/// separate [`Poller`] instances. When the event is delivered, one or more [`Poller`]s are
|
|
|
|
/// notified with that event. The exact number of [`Poller`]s notified depends on the
|
|
|
|
/// underlying platform. When registering multiple sources into one event, the user should
|
|
|
|
/// be careful to accommodate for events lost to other pollers.
|
|
|
|
///
|
|
|
|
/// One may also register one source into other, non-`polling` event loops, like GLib's
|
|
|
|
/// context. While the plumbing will vary from platform to platform, in general the [`Poller`]
|
|
|
|
/// will act as if the source was registered with another [`Poller`], with the same caveats
|
|
|
|
/// as above.
|
|
|
|
///
|
2023-08-04 03:15:59 +00:00
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The source must be [`delete()`]d from this `Poller` before it is dropped.
|
|
|
|
///
|
|
|
|
/// [`delete()`]: Poller::delete
|
Separate adding and modifying of file descriptors
This replaces Poller.insert() and Poller.interest() with Poller.add()
and Poller.modify(), and renames Poller.remove() to Poller.delete().
The method Poller.add() is used for adding a new file descriptor, while
Poller.modify() is used for updating an existing one. Poller.remove() is
renamed to Poller.delete() so the naming scheme of these methods follows
that of epoll, wepoll, etc.
This new setup means that adding a new socket only requires a single
call of Poller.add(), instead of a combination of Poller.insert() and
Poller.interest(). This reduces the amount of system calls necessary,
and leads to a more pleasant API.
On systems that use kqueue or ports, the behaviour of Poller.add() and
Poller.modify() is the same. This is because on these systems adding an
already existing file descriptor will just update its configuration.
This however is an implementation detail and should not be relied upon
by users.
Migrating to this new API is pretty simple, simply replace this:
poller.insert(&socket);
poller.interest(&socket, event);
With this:
poller.add(&socket, event);
And for cases where Poller.interest() was used for updating an existing
file descriptor, simply replace it will a call to Poller.modify().
See https://github.com/stjepang/polling/issues/16 and
https://github.com/stjepang/polling/pull/17 for more information.
2020-10-01 19:26:44 +00:00
|
|
|
///
|
2020-08-06 13:02:59 +00:00
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This method returns an error in the following situations:
|
|
|
|
///
|
|
|
|
/// * If `key` equals `usize::MAX` because that key is reserved for internal use.
|
|
|
|
/// * If an error is returned by the syscall.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2020-10-02 14:40:09 +00:00
|
|
|
/// Set interest in all events:
|
2020-08-06 13:02:59 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
2020-10-02 14:40:09 +00:00
|
|
|
/// use polling::{Event, Poller};
|
|
|
|
///
|
|
|
|
/// let source = std::net::TcpListener::bind("127.0.0.1:0")?;
|
|
|
|
/// source.set_nonblocking(true)?;
|
|
|
|
/// let key = 7;
|
|
|
|
///
|
|
|
|
/// let poller = Poller::new()?;
|
2023-08-04 03:15:59 +00:00
|
|
|
/// unsafe {
|
|
|
|
/// poller.add(&source, Event::all(key))?;
|
|
|
|
/// }
|
|
|
|
/// poller.delete(&source)?;
|
2020-08-06 13:02:59 +00:00
|
|
|
/// # std::io::Result::Ok(())
|
|
|
|
/// ```
|
2023-08-04 03:15:59 +00:00
|
|
|
pub unsafe fn add(&self, source: impl AsRawSource, interest: Event) -> io::Result<()> {
|
2022-12-30 22:43:47 +00:00
|
|
|
self.add_with_mode(source, interest, PollMode::Oneshot)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a file descriptor or socket to the poller in the specified mode.
|
|
|
|
///
|
|
|
|
/// This is identical to the `add()` function, but allows specifying the
|
|
|
|
/// polling mode to use for this socket.
|
|
|
|
///
|
2023-08-04 03:15:59 +00:00
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The source must be [`delete()`]d from this `Poller` before it is dropped.
|
|
|
|
///
|
|
|
|
/// [`delete()`]: Poller::delete
|
|
|
|
///
|
2022-12-30 22:43:47 +00:00
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// If the operating system does not support the specified mode, this function
|
|
|
|
/// will return an error.
|
2023-08-04 03:15:59 +00:00
|
|
|
pub unsafe fn add_with_mode(
|
2022-12-30 22:43:47 +00:00
|
|
|
&self,
|
2023-08-04 03:15:59 +00:00
|
|
|
source: impl AsRawSource,
|
2022-12-30 22:43:47 +00:00
|
|
|
interest: Event,
|
|
|
|
mode: PollMode,
|
|
|
|
) -> io::Result<()> {
|
2020-10-02 14:40:09 +00:00
|
|
|
if interest.key == NOTIFY_KEY {
|
|
|
|
return Err(io::Error::new(
|
|
|
|
io::ErrorKind::InvalidInput,
|
|
|
|
"the key is not allowed to be `usize::MAX`",
|
|
|
|
));
|
|
|
|
}
|
2022-12-30 22:43:47 +00:00
|
|
|
self.poller.add(source.raw(), interest, mode)
|
2020-10-02 14:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Modifies the interest in a file descriptor or socket.
|
2020-08-06 13:02:59 +00:00
|
|
|
///
|
2020-10-02 14:40:09 +00:00
|
|
|
/// This method has the same behavior as [`add()`][`Poller::add()`] except it modifies the
|
|
|
|
/// interest of a previously added file descriptor or socket.
|
|
|
|
///
|
|
|
|
/// To use this method with a file descriptor or socket, you must first add it using
|
|
|
|
/// [`add()`][`Poller::add()`].
|
|
|
|
///
|
|
|
|
/// Note that interest in I/O events needs to be re-enabled using
|
|
|
|
/// [`modify()`][`Poller::modify()`] again after an event is delivered if we're interested in
|
|
|
|
/// the next event of the same kind.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This method returns an error in the following situations:
|
|
|
|
///
|
|
|
|
/// * If `key` equals `usize::MAX` because that key is reserved for internal use.
|
|
|
|
/// * If an error is returned by the syscall.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// To enable interest in all events:
|
2020-08-06 13:02:59 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # use polling::{Event, Poller};
|
|
|
|
/// # let source = std::net::TcpListener::bind("127.0.0.1:0")?;
|
2020-10-02 14:40:09 +00:00
|
|
|
/// # let key = 7;
|
|
|
|
/// # let poller = Poller::new()?;
|
2023-08-04 03:15:59 +00:00
|
|
|
/// # unsafe { poller.add(&source, Event::none(key))?; }
|
2020-10-09 12:10:22 +00:00
|
|
|
/// poller.modify(&source, Event::all(key))?;
|
2020-08-06 13:02:59 +00:00
|
|
|
/// # std::io::Result::Ok(())
|
|
|
|
/// ```
|
|
|
|
///
|
2020-10-02 14:40:09 +00:00
|
|
|
/// To enable interest in readable events and disable interest in writable events:
|
2020-08-06 13:02:59 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # use polling::{Event, Poller};
|
|
|
|
/// # let source = std::net::TcpListener::bind("127.0.0.1:0")?;
|
2020-10-02 14:40:09 +00:00
|
|
|
/// # let key = 7;
|
|
|
|
/// # let poller = Poller::new()?;
|
2023-08-04 03:15:59 +00:00
|
|
|
/// # unsafe { poller.add(&source, Event::none(key))?; }
|
2020-10-02 14:40:09 +00:00
|
|
|
/// poller.modify(&source, Event::readable(key))?;
|
2023-08-04 03:15:59 +00:00
|
|
|
/// # poller.delete(&source)?;
|
2020-08-06 13:02:59 +00:00
|
|
|
/// # std::io::Result::Ok(())
|
|
|
|
/// ```
|
|
|
|
///
|
2020-10-02 14:40:09 +00:00
|
|
|
/// To disable interest in readable events and enable interest in writable events:
|
2020-08-06 13:02:59 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # use polling::{Event, Poller};
|
|
|
|
/// # let poller = Poller::new()?;
|
|
|
|
/// # let key = 7;
|
|
|
|
/// # let source = std::net::TcpListener::bind("127.0.0.1:0")?;
|
2023-08-04 03:15:59 +00:00
|
|
|
/// # unsafe { poller.add(&source, Event::none(key))? };
|
2020-10-02 14:40:09 +00:00
|
|
|
/// poller.modify(&source, Event::writable(key))?;
|
2023-08-04 03:15:59 +00:00
|
|
|
/// # poller.delete(&source)?;
|
2020-08-06 13:02:59 +00:00
|
|
|
/// # std::io::Result::Ok(())
|
|
|
|
/// ```
|
Separate adding and modifying of file descriptors
This replaces Poller.insert() and Poller.interest() with Poller.add()
and Poller.modify(), and renames Poller.remove() to Poller.delete().
The method Poller.add() is used for adding a new file descriptor, while
Poller.modify() is used for updating an existing one. Poller.remove() is
renamed to Poller.delete() so the naming scheme of these methods follows
that of epoll, wepoll, etc.
This new setup means that adding a new socket only requires a single
call of Poller.add(), instead of a combination of Poller.insert() and
Poller.interest(). This reduces the amount of system calls necessary,
and leads to a more pleasant API.
On systems that use kqueue or ports, the behaviour of Poller.add() and
Poller.modify() is the same. This is because on these systems adding an
already existing file descriptor will just update its configuration.
This however is an implementation detail and should not be relied upon
by users.
Migrating to this new API is pretty simple, simply replace this:
poller.insert(&socket);
poller.interest(&socket, event);
With this:
poller.add(&socket, event);
And for cases where Poller.interest() was used for updating an existing
file descriptor, simply replace it will a call to Poller.modify().
See https://github.com/stjepang/polling/issues/16 and
https://github.com/stjepang/polling/pull/17 for more information.
2020-10-01 19:26:44 +00:00
|
|
|
///
|
2020-10-02 14:40:09 +00:00
|
|
|
/// To disable interest in all events:
|
Separate adding and modifying of file descriptors
This replaces Poller.insert() and Poller.interest() with Poller.add()
and Poller.modify(), and renames Poller.remove() to Poller.delete().
The method Poller.add() is used for adding a new file descriptor, while
Poller.modify() is used for updating an existing one. Poller.remove() is
renamed to Poller.delete() so the naming scheme of these methods follows
that of epoll, wepoll, etc.
This new setup means that adding a new socket only requires a single
call of Poller.add(), instead of a combination of Poller.insert() and
Poller.interest(). This reduces the amount of system calls necessary,
and leads to a more pleasant API.
On systems that use kqueue or ports, the behaviour of Poller.add() and
Poller.modify() is the same. This is because on these systems adding an
already existing file descriptor will just update its configuration.
This however is an implementation detail and should not be relied upon
by users.
Migrating to this new API is pretty simple, simply replace this:
poller.insert(&socket);
poller.interest(&socket, event);
With this:
poller.add(&socket, event);
And for cases where Poller.interest() was used for updating an existing
file descriptor, simply replace it will a call to Poller.modify().
See https://github.com/stjepang/polling/issues/16 and
https://github.com/stjepang/polling/pull/17 for more information.
2020-10-01 19:26:44 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # use polling::{Event, Poller};
|
|
|
|
/// # let source = std::net::TcpListener::bind("127.0.0.1:0")?;
|
2020-10-02 14:40:09 +00:00
|
|
|
/// # let key = 7;
|
|
|
|
/// # let poller = Poller::new()?;
|
2023-08-04 03:15:59 +00:00
|
|
|
/// # unsafe { poller.add(&source, Event::none(key))?; }
|
2020-10-02 14:40:09 +00:00
|
|
|
/// poller.modify(&source, Event::none(key))?;
|
2023-08-04 03:15:59 +00:00
|
|
|
/// # poller.delete(&source)?;
|
Separate adding and modifying of file descriptors
This replaces Poller.insert() and Poller.interest() with Poller.add()
and Poller.modify(), and renames Poller.remove() to Poller.delete().
The method Poller.add() is used for adding a new file descriptor, while
Poller.modify() is used for updating an existing one. Poller.remove() is
renamed to Poller.delete() so the naming scheme of these methods follows
that of epoll, wepoll, etc.
This new setup means that adding a new socket only requires a single
call of Poller.add(), instead of a combination of Poller.insert() and
Poller.interest(). This reduces the amount of system calls necessary,
and leads to a more pleasant API.
On systems that use kqueue or ports, the behaviour of Poller.add() and
Poller.modify() is the same. This is because on these systems adding an
already existing file descriptor will just update its configuration.
This however is an implementation detail and should not be relied upon
by users.
Migrating to this new API is pretty simple, simply replace this:
poller.insert(&socket);
poller.interest(&socket, event);
With this:
poller.add(&socket, event);
And for cases where Poller.interest() was used for updating an existing
file descriptor, simply replace it will a call to Poller.modify().
See https://github.com/stjepang/polling/issues/16 and
https://github.com/stjepang/polling/pull/17 for more information.
2020-10-01 19:26:44 +00:00
|
|
|
/// # std::io::Result::Ok(())
|
|
|
|
/// ```
|
2023-08-04 03:15:59 +00:00
|
|
|
pub fn modify(&self, source: impl AsSource, interest: Event) -> io::Result<()> {
|
2022-12-30 22:43:47 +00:00
|
|
|
self.modify_with_mode(source, interest, PollMode::Oneshot)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Modifies interest in a file descriptor or socket to the poller, but with the specified
|
|
|
|
/// mode.
|
|
|
|
///
|
|
|
|
/// This is identical to the `modify()` function, but allows specifying the polling mode
|
|
|
|
/// to use for this socket.
|
|
|
|
///
|
|
|
|
/// # Performance Notes
|
|
|
|
///
|
|
|
|
/// This function can be used to change a source from one polling mode to another. However,
|
|
|
|
/// on some platforms, this switch can cause delays in the delivery of events.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// If the operating system does not support the specified mode, this function will return
|
|
|
|
/// an error.
|
|
|
|
pub fn modify_with_mode(
|
|
|
|
&self,
|
2023-08-04 03:15:59 +00:00
|
|
|
source: impl AsSource,
|
2022-12-30 22:43:47 +00:00
|
|
|
interest: Event,
|
|
|
|
mode: PollMode,
|
|
|
|
) -> io::Result<()> {
|
2020-10-02 14:40:09 +00:00
|
|
|
if interest.key == NOTIFY_KEY {
|
|
|
|
return Err(io::Error::new(
|
|
|
|
io::ErrorKind::InvalidInput,
|
|
|
|
"the key is not allowed to be `usize::MAX`",
|
|
|
|
));
|
|
|
|
}
|
2023-08-04 03:15:59 +00:00
|
|
|
self.poller.modify(source.source(), interest, mode)
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes a file descriptor or socket from the poller.
|
|
|
|
///
|
Separate adding and modifying of file descriptors
This replaces Poller.insert() and Poller.interest() with Poller.add()
and Poller.modify(), and renames Poller.remove() to Poller.delete().
The method Poller.add() is used for adding a new file descriptor, while
Poller.modify() is used for updating an existing one. Poller.remove() is
renamed to Poller.delete() so the naming scheme of these methods follows
that of epoll, wepoll, etc.
This new setup means that adding a new socket only requires a single
call of Poller.add(), instead of a combination of Poller.insert() and
Poller.interest(). This reduces the amount of system calls necessary,
and leads to a more pleasant API.
On systems that use kqueue or ports, the behaviour of Poller.add() and
Poller.modify() is the same. This is because on these systems adding an
already existing file descriptor will just update its configuration.
This however is an implementation detail and should not be relied upon
by users.
Migrating to this new API is pretty simple, simply replace this:
poller.insert(&socket);
poller.interest(&socket, event);
With this:
poller.add(&socket, event);
And for cases where Poller.interest() was used for updating an existing
file descriptor, simply replace it will a call to Poller.modify().
See https://github.com/stjepang/polling/issues/16 and
https://github.com/stjepang/polling/pull/17 for more information.
2020-10-01 19:26:44 +00:00
|
|
|
/// Unlike [`add()`][`Poller::add()`], this method only removes the file descriptor or
|
2020-08-07 12:56:53 +00:00
|
|
|
/// socket from the poller without putting it back into blocking mode.
|
|
|
|
///
|
2020-08-06 13:02:59 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
Separate adding and modifying of file descriptors
This replaces Poller.insert() and Poller.interest() with Poller.add()
and Poller.modify(), and renames Poller.remove() to Poller.delete().
The method Poller.add() is used for adding a new file descriptor, while
Poller.modify() is used for updating an existing one. Poller.remove() is
renamed to Poller.delete() so the naming scheme of these methods follows
that of epoll, wepoll, etc.
This new setup means that adding a new socket only requires a single
call of Poller.add(), instead of a combination of Poller.insert() and
Poller.interest(). This reduces the amount of system calls necessary,
and leads to a more pleasant API.
On systems that use kqueue or ports, the behaviour of Poller.add() and
Poller.modify() is the same. This is because on these systems adding an
already existing file descriptor will just update its configuration.
This however is an implementation detail and should not be relied upon
by users.
Migrating to this new API is pretty simple, simply replace this:
poller.insert(&socket);
poller.interest(&socket, event);
With this:
poller.add(&socket, event);
And for cases where Poller.interest() was used for updating an existing
file descriptor, simply replace it will a call to Poller.modify().
See https://github.com/stjepang/polling/issues/16 and
https://github.com/stjepang/polling/pull/17 for more information.
2020-10-01 19:26:44 +00:00
|
|
|
/// use polling::{Event, Poller};
|
2020-08-06 13:02:59 +00:00
|
|
|
/// use std::net::TcpListener;
|
|
|
|
///
|
|
|
|
/// let socket = TcpListener::bind("127.0.0.1:0")?;
|
2020-09-30 23:08:20 +00:00
|
|
|
/// socket.set_nonblocking(true)?;
|
2020-10-02 14:40:09 +00:00
|
|
|
/// let key = 7;
|
2020-09-30 23:08:20 +00:00
|
|
|
///
|
2020-10-02 14:40:09 +00:00
|
|
|
/// let poller = Poller::new()?;
|
2023-08-04 03:15:59 +00:00
|
|
|
/// unsafe { poller.add(&socket, Event::all(key))?; }
|
Separate adding and modifying of file descriptors
This replaces Poller.insert() and Poller.interest() with Poller.add()
and Poller.modify(), and renames Poller.remove() to Poller.delete().
The method Poller.add() is used for adding a new file descriptor, while
Poller.modify() is used for updating an existing one. Poller.remove() is
renamed to Poller.delete() so the naming scheme of these methods follows
that of epoll, wepoll, etc.
This new setup means that adding a new socket only requires a single
call of Poller.add(), instead of a combination of Poller.insert() and
Poller.interest(). This reduces the amount of system calls necessary,
and leads to a more pleasant API.
On systems that use kqueue or ports, the behaviour of Poller.add() and
Poller.modify() is the same. This is because on these systems adding an
already existing file descriptor will just update its configuration.
This however is an implementation detail and should not be relied upon
by users.
Migrating to this new API is pretty simple, simply replace this:
poller.insert(&socket);
poller.interest(&socket, event);
With this:
poller.add(&socket, event);
And for cases where Poller.interest() was used for updating an existing
file descriptor, simply replace it will a call to Poller.modify().
See https://github.com/stjepang/polling/issues/16 and
https://github.com/stjepang/polling/pull/17 for more information.
2020-10-01 19:26:44 +00:00
|
|
|
/// poller.delete(&socket)?;
|
2020-08-06 13:02:59 +00:00
|
|
|
/// # std::io::Result::Ok(())
|
|
|
|
/// ```
|
2023-08-04 03:15:59 +00:00
|
|
|
pub fn delete(&self, source: impl AsSource) -> io::Result<()> {
|
|
|
|
self.poller.delete(source.source())
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Waits for at least one I/O event and returns the number of new events.
|
|
|
|
///
|
2023-10-20 16:49:44 +00:00
|
|
|
/// New events will be appended to `events`. If necessary, make sure to clear the
|
|
|
|
/// [`Events`][Events::clear()] before calling [`wait()`][`Poller::wait()`]!
|
2020-08-06 13:02:59 +00:00
|
|
|
///
|
2020-08-14 14:08:13 +00:00
|
|
|
/// This method will return with no new events if a notification is delivered by the
|
2020-09-01 04:48:09 +00:00
|
|
|
/// [`notify()`] method, or the timeout is reached. Sometimes it may even return with no events
|
|
|
|
/// spuriously.
|
2020-08-06 13:02:59 +00:00
|
|
|
///
|
|
|
|
/// Only one thread can wait on I/O. If another thread is already in [`wait()`], concurrent
|
|
|
|
/// calls to this method will return immediately with no new events.
|
|
|
|
///
|
|
|
|
/// If the operating system is ready to deliver a large number of events at once, this method
|
|
|
|
/// may decide to deliver them in smaller batches.
|
|
|
|
///
|
|
|
|
/// [`notify()`]: `Poller::notify()`
|
|
|
|
/// [`wait()`]: `Poller::wait()`
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2023-08-14 17:03:20 +00:00
|
|
|
/// use polling::{Event, Events, Poller};
|
2020-08-06 13:02:59 +00:00
|
|
|
/// use std::net::TcpListener;
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// let socket = TcpListener::bind("127.0.0.1:0")?;
|
2020-10-02 14:40:09 +00:00
|
|
|
/// socket.set_nonblocking(true)?;
|
Separate adding and modifying of file descriptors
This replaces Poller.insert() and Poller.interest() with Poller.add()
and Poller.modify(), and renames Poller.remove() to Poller.delete().
The method Poller.add() is used for adding a new file descriptor, while
Poller.modify() is used for updating an existing one. Poller.remove() is
renamed to Poller.delete() so the naming scheme of these methods follows
that of epoll, wepoll, etc.
This new setup means that adding a new socket only requires a single
call of Poller.add(), instead of a combination of Poller.insert() and
Poller.interest(). This reduces the amount of system calls necessary,
and leads to a more pleasant API.
On systems that use kqueue or ports, the behaviour of Poller.add() and
Poller.modify() is the same. This is because on these systems adding an
already existing file descriptor will just update its configuration.
This however is an implementation detail and should not be relied upon
by users.
Migrating to this new API is pretty simple, simply replace this:
poller.insert(&socket);
poller.interest(&socket, event);
With this:
poller.add(&socket, event);
And for cases where Poller.interest() was used for updating an existing
file descriptor, simply replace it will a call to Poller.modify().
See https://github.com/stjepang/polling/issues/16 and
https://github.com/stjepang/polling/pull/17 for more information.
2020-10-01 19:26:44 +00:00
|
|
|
/// let key = 7;
|
2020-09-30 23:08:20 +00:00
|
|
|
///
|
2020-10-02 14:40:09 +00:00
|
|
|
/// let poller = Poller::new()?;
|
2023-08-04 03:15:59 +00:00
|
|
|
/// unsafe {
|
|
|
|
/// poller.add(&socket, Event::all(key))?;
|
|
|
|
/// }
|
2020-08-06 13:02:59 +00:00
|
|
|
///
|
2023-08-14 17:03:20 +00:00
|
|
|
/// let mut events = Events::new();
|
2020-08-06 13:02:59 +00:00
|
|
|
/// let n = poller.wait(&mut events, Some(Duration::from_secs(1)))?;
|
2023-08-04 03:15:59 +00:00
|
|
|
/// poller.delete(&socket)?;
|
2020-08-06 13:02:59 +00:00
|
|
|
/// # std::io::Result::Ok(())
|
|
|
|
/// ```
|
2023-08-14 17:03:20 +00:00
|
|
|
pub fn wait(&self, events: &mut Events, timeout: Option<Duration>) -> io::Result<usize> {
|
2023-06-21 03:28:18 +00:00
|
|
|
let span = tracing::trace_span!("Poller::wait", ?timeout);
|
|
|
|
let _enter = span.enter();
|
2020-08-29 14:00:59 +00:00
|
|
|
|
2023-08-14 17:03:20 +00:00
|
|
|
if let Ok(_lock) = self.lock.try_lock() {
|
2023-10-27 14:02:08 +00:00
|
|
|
let deadline = timeout.and_then(|timeout| Instant::now().checked_add(timeout));
|
|
|
|
|
|
|
|
loop {
|
|
|
|
// Figure out how long to wait for.
|
|
|
|
let timeout =
|
|
|
|
deadline.map(|deadline| deadline.saturating_duration_since(Instant::now()));
|
|
|
|
|
|
|
|
// Wait for I/O events.
|
|
|
|
if let Err(e) = self.poller.wait(&mut events.events, timeout) {
|
|
|
|
// If the wait was interrupted by a signal, clear events and try again.
|
|
|
|
if e.kind() == io::ErrorKind::Interrupted {
|
|
|
|
events.clear();
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the notification, if any.
|
|
|
|
self.notified.swap(false, Ordering::SeqCst);
|
|
|
|
|
|
|
|
// Indicate number of events.
|
|
|
|
return Ok(events.len());
|
|
|
|
}
|
2020-08-06 13:02:59 +00:00
|
|
|
} else {
|
2023-06-21 03:28:18 +00:00
|
|
|
tracing::trace!("wait: skipping because another thread is already waiting on I/O");
|
2020-08-06 13:02:59 +00:00
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wakes up the current or the following invocation of [`wait()`].
|
|
|
|
///
|
|
|
|
/// If no thread is calling [`wait()`] right now, this method will cause the following call
|
|
|
|
/// to wake up immediately.
|
|
|
|
///
|
|
|
|
/// [`wait()`]: `Poller::wait()`
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2023-08-14 17:03:20 +00:00
|
|
|
/// use polling::{Events, Poller};
|
2020-08-06 13:02:59 +00:00
|
|
|
///
|
|
|
|
/// let poller = Poller::new()?;
|
|
|
|
///
|
|
|
|
/// // Notify the poller.
|
|
|
|
/// poller.notify()?;
|
|
|
|
///
|
2023-08-14 17:03:20 +00:00
|
|
|
/// let mut events = Events::new();
|
2020-08-06 13:02:59 +00:00
|
|
|
/// poller.wait(&mut events, None)?; // wakes up immediately
|
|
|
|
/// assert!(events.is_empty());
|
|
|
|
/// # std::io::Result::Ok(())
|
|
|
|
/// ```
|
|
|
|
pub fn notify(&self) -> io::Result<()> {
|
2023-06-21 03:28:18 +00:00
|
|
|
let span = tracing::trace_span!("Poller::notify");
|
|
|
|
let _enter = span.enter();
|
|
|
|
|
2020-12-24 12:18:55 +00:00
|
|
|
if self
|
2020-08-14 14:07:53 +00:00
|
|
|
.notified
|
2020-12-24 12:18:55 +00:00
|
|
|
.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
|
|
|
|
.is_ok()
|
2020-08-14 14:07:53 +00:00
|
|
|
{
|
|
|
|
self.poller.notify()?;
|
|
|
|
}
|
|
|
|
Ok(())
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-14 17:03:20 +00:00
|
|
|
/// A container for I/O events.
|
|
|
|
pub struct Events {
|
|
|
|
events: sys::Events,
|
|
|
|
|
|
|
|
/// This is intended to be used from &mut, thread locally, so we should make it !Sync
|
|
|
|
/// for consistency with the rest of the API.
|
|
|
|
_not_sync: PhantomData<Cell<()>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Events {
|
|
|
|
#[inline]
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Events {
|
|
|
|
/// Create a new container for events, using the default capacity.
|
|
|
|
///
|
|
|
|
/// The default capacity is 1024.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use polling::Events;
|
|
|
|
///
|
|
|
|
/// let events = Events::new();
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn new() -> Self {
|
|
|
|
// ESP-IDF has a low amount of RAM, so we use a smaller default capacity.
|
|
|
|
#[cfg(target_os = "espidf")]
|
|
|
|
const DEFAULT_CAPACITY: usize = 32;
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "espidf"))]
|
|
|
|
const DEFAULT_CAPACITY: usize = 1024;
|
|
|
|
|
|
|
|
Self::with_capacity(NonZeroUsize::new(DEFAULT_CAPACITY).unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new container with the provided capacity.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use polling::Events;
|
|
|
|
/// use std::num::NonZeroUsize;
|
|
|
|
///
|
|
|
|
/// let capacity = NonZeroUsize::new(1024).unwrap();
|
|
|
|
/// let events = Events::with_capacity(capacity);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn with_capacity(capacity: NonZeroUsize) -> Self {
|
|
|
|
Self {
|
|
|
|
events: sys::Events::with_capacity(capacity.get()),
|
|
|
|
_not_sync: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new iterator over I/O events.
|
|
|
|
///
|
|
|
|
/// This returns all of the events in the container, excluding the notification event.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use polling::{Event, Events, Poller};
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// # fn main() -> std::io::Result<()> {
|
|
|
|
/// let poller = Poller::new()?;
|
|
|
|
/// let mut events = Events::new();
|
|
|
|
///
|
|
|
|
/// poller.wait(&mut events, Some(Duration::from_secs(0)))?;
|
|
|
|
/// assert!(events.iter().next().is_none());
|
|
|
|
/// # Ok(()) }
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = Event> + '_ {
|
|
|
|
self.events.iter().filter(|ev| ev.key != NOTIFY_KEY)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Delete all of the events in the container.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use polling::{Event, Events, Poller};
|
|
|
|
///
|
|
|
|
/// # fn main() -> std::io::Result<()> {
|
|
|
|
/// let poller = Poller::new()?;
|
|
|
|
/// let mut events = Events::new();
|
|
|
|
///
|
|
|
|
/// /* register some sources */
|
|
|
|
///
|
|
|
|
/// poller.wait(&mut events, None)?;
|
|
|
|
///
|
|
|
|
/// events.clear();
|
|
|
|
/// # Ok(()) }
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.events.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of events in the container.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use polling::Events;
|
|
|
|
///
|
|
|
|
/// let events = Events::new();
|
|
|
|
/// assert_eq!(events.len(), 0);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.iter().count()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the container contains no events.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use polling::Events;
|
|
|
|
///
|
|
|
|
/// let events = Events::new();
|
|
|
|
/// assert!(events.is_empty());
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.len() == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the total capacity of the list.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use polling::Events;
|
|
|
|
/// use std::num::NonZeroUsize;
|
|
|
|
///
|
|
|
|
/// let cap = NonZeroUsize::new(10).unwrap();
|
|
|
|
/// let events = Events::with_capacity(std::num::NonZeroUsize::new(10).unwrap());
|
|
|
|
/// assert_eq!(events.capacity(), cap);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn capacity(&self) -> NonZeroUsize {
|
|
|
|
NonZeroUsize::new(self.events.capacity()).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Events {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.write_str("Events { .. }")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 07:01:25 +00:00
|
|
|
#[cfg(all(
|
|
|
|
any(
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "android",
|
|
|
|
target_os = "illumos",
|
|
|
|
target_os = "solaris",
|
|
|
|
target_os = "macos",
|
|
|
|
target_os = "ios",
|
|
|
|
target_os = "tvos",
|
|
|
|
target_os = "watchos",
|
|
|
|
target_os = "freebsd",
|
|
|
|
target_os = "netbsd",
|
|
|
|
target_os = "openbsd",
|
|
|
|
target_os = "dragonfly",
|
|
|
|
),
|
|
|
|
not(polling_test_poll_backend),
|
2022-08-18 16:52:28 +00:00
|
|
|
))]
|
2022-08-21 10:47:00 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
docsrs,
|
|
|
|
doc(cfg(any(
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "android",
|
|
|
|
target_os = "illumos",
|
|
|
|
target_os = "solaris",
|
|
|
|
target_os = "macos",
|
|
|
|
target_os = "ios",
|
2022-12-20 03:54:45 +00:00
|
|
|
target_os = "tvos",
|
2022-12-28 03:00:18 +00:00
|
|
|
target_os = "watchos",
|
2022-08-21 10:47:00 +00:00
|
|
|
target_os = "freebsd",
|
|
|
|
target_os = "netbsd",
|
|
|
|
target_os = "openbsd",
|
|
|
|
target_os = "dragonfly",
|
|
|
|
)))
|
|
|
|
)]
|
2022-08-18 16:52:28 +00:00
|
|
|
mod raw_fd_impl {
|
|
|
|
use crate::Poller;
|
2023-06-11 17:37:25 +00:00
|
|
|
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd};
|
2022-08-18 16:52:28 +00:00
|
|
|
|
|
|
|
impl AsRawFd for Poller {
|
|
|
|
fn as_raw_fd(&self) -> RawFd {
|
|
|
|
self.poller.as_raw_fd()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsFd for Poller {
|
|
|
|
fn as_fd(&self) -> BorrowedFd<'_> {
|
|
|
|
self.poller.as_fd()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
2022-08-21 10:47:00 +00:00
|
|
|
#[cfg_attr(docsrs, doc(cfg(windows)))]
|
2022-08-18 16:52:28 +00:00
|
|
|
mod raw_handle_impl {
|
|
|
|
use crate::Poller;
|
2023-06-11 17:37:25 +00:00
|
|
|
use std::os::windows::io::{AsHandle, AsRawHandle, BorrowedHandle, RawHandle};
|
2022-08-18 16:52:28 +00:00
|
|
|
|
|
|
|
impl AsRawHandle for Poller {
|
|
|
|
fn as_raw_handle(&self) -> RawHandle {
|
|
|
|
self.poller.as_raw_handle()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsHandle for Poller {
|
|
|
|
fn as_handle(&self) -> BorrowedHandle<'_> {
|
|
|
|
self.poller.as_handle()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 13:02:59 +00:00
|
|
|
impl fmt::Debug for Poller {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
self.poller.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg_if! {
|
2024-03-13 04:33:06 +00:00
|
|
|
if #[cfg(any(unix, target_os = "hermit"))] {
|
|
|
|
#[cfg(unix)]
|
2023-08-04 03:15:59 +00:00
|
|
|
use std::os::unix::io::{AsRawFd, RawFd, AsFd, BorrowedFd};
|
2024-03-13 04:33:06 +00:00
|
|
|
#[cfg(target_os = "hermit")]
|
|
|
|
use std::os::hermit::io::{AsRawFd, RawFd, AsFd, BorrowedFd};
|
2020-08-06 13:02:59 +00:00
|
|
|
|
2023-08-04 03:15:59 +00:00
|
|
|
/// A resource with a raw file descriptor.
|
|
|
|
pub trait AsRawSource {
|
|
|
|
/// Returns the raw file descriptor.
|
2020-08-06 13:02:59 +00:00
|
|
|
fn raw(&self) -> RawFd;
|
|
|
|
}
|
|
|
|
|
2023-08-04 03:15:59 +00:00
|
|
|
impl<T: AsRawFd> AsRawSource for &T {
|
2020-08-06 13:02:59 +00:00
|
|
|
fn raw(&self) -> RawFd {
|
2023-08-04 03:15:59 +00:00
|
|
|
self.as_raw_fd()
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-04 03:15:59 +00:00
|
|
|
impl AsRawSource for RawFd {
|
2020-08-06 13:02:59 +00:00
|
|
|
fn raw(&self) -> RawFd {
|
2023-08-04 03:15:59 +00:00
|
|
|
*self
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-04 03:15:59 +00:00
|
|
|
|
|
|
|
/// A resource with a borrowed file descriptor.
|
|
|
|
pub trait AsSource: AsFd {
|
|
|
|
/// Returns the borrowed file descriptor.
|
|
|
|
fn source(&self) -> BorrowedFd<'_> {
|
|
|
|
self.as_fd()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsFd> AsSource for T {}
|
2020-08-06 13:02:59 +00:00
|
|
|
} else if #[cfg(windows)] {
|
2023-08-04 03:15:59 +00:00
|
|
|
use std::os::windows::io::{AsRawSocket, RawSocket, AsSocket, BorrowedSocket};
|
2020-08-06 13:02:59 +00:00
|
|
|
|
2023-08-04 03:15:59 +00:00
|
|
|
/// A resource with a raw socket.
|
|
|
|
pub trait AsRawSource {
|
|
|
|
/// Returns the raw socket.
|
2020-08-06 13:02:59 +00:00
|
|
|
fn raw(&self) -> RawSocket;
|
|
|
|
}
|
|
|
|
|
2023-08-04 03:15:59 +00:00
|
|
|
impl<T: AsRawSocket> AsRawSource for &T {
|
2020-08-06 13:02:59 +00:00
|
|
|
fn raw(&self) -> RawSocket {
|
2023-08-04 03:15:59 +00:00
|
|
|
self.as_raw_socket()
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-04 03:15:59 +00:00
|
|
|
impl AsRawSource for RawSocket {
|
2020-08-06 13:02:59 +00:00
|
|
|
fn raw(&self) -> RawSocket {
|
2023-08-04 03:15:59 +00:00
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A resource with a borrowed socket.
|
|
|
|
pub trait AsSource: AsSocket {
|
|
|
|
/// Returns the borrowed socket.
|
|
|
|
fn source(&self) -> BorrowedSocket<'_> {
|
|
|
|
self.as_socket()
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-04 03:15:59 +00:00
|
|
|
|
|
|
|
impl<T: AsSocket> AsSource for T {}
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-30 22:43:47 +00:00
|
|
|
|
|
|
|
#[allow(unused)]
|
|
|
|
fn unsupported_error(err: impl Into<String>) -> io::Error {
|
2023-06-11 17:37:25 +00:00
|
|
|
io::Error::new(io::ErrorKind::Unsupported, err.into())
|
2022-12-30 22:43:47 +00:00
|
|
|
}
|
2023-08-14 17:03:20 +00:00
|
|
|
|
|
|
|
fn _assert_send_and_sync() {
|
|
|
|
fn assert_send<T: Send>() {}
|
|
|
|
fn assert_sync<T: Sync>() {}
|
|
|
|
|
|
|
|
assert_send::<Poller>();
|
|
|
|
assert_sync::<Poller>();
|
|
|
|
|
|
|
|
assert_send::<Event>();
|
|
|
|
assert_sync::<Event>();
|
|
|
|
|
|
|
|
assert_send::<Events>();
|
|
|
|
// Events can be !Sync
|
|
|
|
}
|