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:
|
2020-08-10 17:16:06 +00:00
|
|
|
//! - [epoll](https://en.wikipedia.org/wiki/Epoll): Linux, Android
|
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
|
2020-12-17 18:56:48 +00:00
|
|
|
//! - [poll](https://en.wikipedia.org/wiki/Poll_(Unix)): VxWorks, Fuchsia, 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
|
|
|
|
//! use polling::{Event, Poller};
|
|
|
|
//! 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()?;
|
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.add(&socket, Event::readable(key))?;
|
2020-08-06 13:02:59 +00:00
|
|
|
//!
|
|
|
|
//! // The event loop.
|
|
|
|
//! let mut events = Vec::new();
|
|
|
|
//! loop {
|
|
|
|
//! // Wait for at least one I/O event.
|
|
|
|
//! events.clear();
|
|
|
|
//! poller.wait(&mut events, None)?;
|
|
|
|
//!
|
|
|
|
//! for ev in &events {
|
|
|
|
//! 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
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! # std::io::Result::Ok(())
|
|
|
|
//! ```
|
|
|
|
|
2020-09-20 15:45:24 +00:00
|
|
|
#![cfg(feature = "std")]
|
|
|
|
#![cfg_attr(not(feature = "std"), no_std)]
|
2020-08-06 13:02:59 +00:00
|
|
|
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
|
2022-01-08 17:00:40 +00:00
|
|
|
#![allow(clippy::useless_conversion, clippy::unnecessary_cast)]
|
2022-08-21 10:47:00 +00:00
|
|
|
#![cfg_attr(docsrs, feature(doc_cfg))]
|
2020-08-06 13:02:59 +00:00
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
use std::io;
|
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;
|
|
|
|
use std::time::Duration;
|
|
|
|
use std::usize;
|
|
|
|
|
|
|
|
use cfg_if::cfg_if;
|
|
|
|
|
|
|
|
/// Calls a libc function and results in `io::Result`.
|
|
|
|
#[cfg(unix)]
|
|
|
|
macro_rules! syscall {
|
|
|
|
($fn:ident $args:tt) => {{
|
|
|
|
let res = unsafe { libc::$fn $args };
|
|
|
|
if res == -1 {
|
|
|
|
Err(std::io::Error::last_os_error())
|
|
|
|
} else {
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
} else if #[cfg(any(target_os = "linux", target_os = "android"))] {
|
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",
|
|
|
|
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.
|
|
|
|
const NOTIFY_KEY: usize = std::usize::MAX;
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2020-08-06 13:02:59 +00:00
|
|
|
impl Event {
|
|
|
|
/// All kinds of events (readable and writable).
|
|
|
|
///
|
|
|
|
/// Equivalent to: `Event { key, readable: true, writable: true }`
|
|
|
|
pub fn all(key: usize) -> Event {
|
|
|
|
Event {
|
|
|
|
key,
|
|
|
|
readable: true,
|
|
|
|
writable: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Only the readable event.
|
|
|
|
///
|
|
|
|
/// Equivalent to: `Event { key, readable: true, writable: false }`
|
|
|
|
pub fn readable(key: usize) -> Event {
|
|
|
|
Event {
|
|
|
|
key,
|
|
|
|
readable: true,
|
|
|
|
writable: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Only the writable event.
|
|
|
|
///
|
|
|
|
/// Equivalent to: `Event { key, readable: false, writable: true }`
|
|
|
|
pub fn writable(key: usize) -> Event {
|
|
|
|
Event {
|
|
|
|
key,
|
|
|
|
readable: false,
|
|
|
|
writable: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// No events.
|
|
|
|
///
|
|
|
|
/// Equivalent to: `Event { key, readable: false, writable: false }`
|
|
|
|
pub fn none(key: usize) -> Event {
|
|
|
|
Event {
|
|
|
|
key,
|
2020-08-06 16:03:28 +00:00
|
|
|
readable: false,
|
|
|
|
writable: false,
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Waits for I/O events.
|
|
|
|
pub struct Poller {
|
|
|
|
poller: sys::Poller,
|
|
|
|
events: Mutex<sys::Events>,
|
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()?,
|
|
|
|
events: Mutex::new(sys::Events::new()),
|
|
|
|
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.
|
|
|
|
///
|
|
|
|
/// Don't forget to [`delete()`][`Poller::delete()`] the file descriptor or socket when it is
|
|
|
|
/// no longer used!
|
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()?;
|
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.add(&source, Event::all(key))?;
|
2020-08-06 13:02:59 +00:00
|
|
|
/// # std::io::Result::Ok(())
|
|
|
|
/// ```
|
2020-10-02 14:40:09 +00:00
|
|
|
pub fn add(&self, source: impl Source, 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.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// If the operating system does not support the specified mode, this function
|
|
|
|
/// will return an error.
|
|
|
|
pub fn add_with_mode(
|
|
|
|
&self,
|
|
|
|
source: impl Source,
|
|
|
|
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()?;
|
2020-10-09 12:10:22 +00:00
|
|
|
/// # poller.add(&source, Event::none(key))?;
|
|
|
|
/// 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()?;
|
|
|
|
/// # poller.add(&source, Event::none(key))?;
|
|
|
|
/// poller.modify(&source, Event::readable(key))?;
|
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")?;
|
2020-10-02 14:40:09 +00:00
|
|
|
/// # poller.add(&source, Event::none(key))?;
|
|
|
|
/// poller.modify(&source, Event::writable(key))?;
|
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()?;
|
|
|
|
/// # poller.add(&source, Event::none(key))?;
|
|
|
|
/// poller.modify(&source, Event::none(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
|
|
|
/// # std::io::Result::Ok(())
|
|
|
|
/// ```
|
2020-10-02 14:40:09 +00:00
|
|
|
pub fn modify(&self, source: impl Source, 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,
|
|
|
|
source: impl Source,
|
|
|
|
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.modify(source.raw(), 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()?;
|
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.add(&socket, Event::all(key))?;
|
|
|
|
/// poller.delete(&socket)?;
|
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
|
|
|
pub fn delete(&self, source: impl Source) -> io::Result<()> {
|
|
|
|
self.poller.delete(source.raw())
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Waits for at least one I/O event and returns the number of new events.
|
|
|
|
///
|
2020-08-08 08:19:39 +00:00
|
|
|
/// New events will be appended to `events`. If necessary, make sure to clear the [`Vec`]
|
|
|
|
/// 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
|
|
|
|
///
|
|
|
|
/// ```
|
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;
|
|
|
|
/// 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()?;
|
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.add(&socket, Event::all(key))?;
|
2020-08-06 13:02:59 +00:00
|
|
|
///
|
|
|
|
/// let mut events = Vec::new();
|
|
|
|
/// let n = poller.wait(&mut events, Some(Duration::from_secs(1)))?;
|
|
|
|
/// # std::io::Result::Ok(())
|
|
|
|
/// ```
|
|
|
|
pub fn wait(&self, events: &mut Vec<Event>, timeout: Option<Duration>) -> io::Result<usize> {
|
2020-09-03 10:55:46 +00:00
|
|
|
log::trace!("Poller::wait(_, {:?})", timeout);
|
2020-08-29 14:00:59 +00:00
|
|
|
|
2020-08-06 13:02:59 +00:00
|
|
|
if let Ok(mut lock) = self.events.try_lock() {
|
2020-08-14 14:07:53 +00:00
|
|
|
// Wait for I/O events.
|
2020-08-14 11:46:58 +00:00
|
|
|
self.poller.wait(&mut lock, timeout)?;
|
2020-08-14 14:07:53 +00:00
|
|
|
|
|
|
|
// Clear the notification, if any.
|
|
|
|
self.notified.swap(false, Ordering::SeqCst);
|
|
|
|
|
|
|
|
// Collect events.
|
2020-08-14 11:46:58 +00:00
|
|
|
let len = events.len();
|
2020-08-06 13:02:59 +00:00
|
|
|
events.extend(lock.iter().filter(|ev| ev.key != usize::MAX));
|
2020-08-14 11:46:58 +00:00
|
|
|
Ok(events.len() - len)
|
2020-08-06 13:02:59 +00:00
|
|
|
} else {
|
2020-08-29 14:00:59 +00:00
|
|
|
log::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
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use polling::Poller;
|
|
|
|
///
|
|
|
|
/// let poller = Poller::new()?;
|
|
|
|
///
|
|
|
|
/// // Notify the poller.
|
|
|
|
/// poller.notify()?;
|
|
|
|
///
|
|
|
|
/// let mut events = Vec::new();
|
|
|
|
/// poller.wait(&mut events, None)?; // wakes up immediately
|
|
|
|
/// assert!(events.is_empty());
|
|
|
|
/// # std::io::Result::Ok(())
|
|
|
|
/// ```
|
|
|
|
pub fn notify(&self) -> io::Result<()> {
|
2020-09-03 10:55:46 +00:00
|
|
|
log::trace!("Poller::notify()");
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
use std::os::unix::io::{AsRawFd, RawFd};
|
|
|
|
|
|
|
|
#[cfg(not(polling_no_io_safety))]
|
|
|
|
use std::os::unix::io::{AsFd, BorrowedFd};
|
|
|
|
|
|
|
|
impl AsRawFd for Poller {
|
|
|
|
fn as_raw_fd(&self) -> RawFd {
|
|
|
|
self.poller.as_raw_fd()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(polling_no_io_safety))]
|
|
|
|
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;
|
|
|
|
use std::os::windows::io::{AsRawHandle, RawHandle};
|
|
|
|
|
|
|
|
#[cfg(not(polling_no_io_safety))]
|
|
|
|
use std::os::windows::io::{AsHandle, BorrowedHandle};
|
|
|
|
|
|
|
|
impl AsRawHandle for Poller {
|
|
|
|
fn as_raw_handle(&self) -> RawHandle {
|
|
|
|
self.poller.as_raw_handle()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(polling_no_io_safety))]
|
|
|
|
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! {
|
|
|
|
if #[cfg(unix)] {
|
|
|
|
use std::os::unix::io::{AsRawFd, RawFd};
|
|
|
|
|
|
|
|
/// A [`RawFd`] or a reference to a type implementing [`AsRawFd`].
|
|
|
|
pub trait Source {
|
|
|
|
/// Returns the [`RawFd`] for this I/O object.
|
|
|
|
fn raw(&self) -> RawFd;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Source for RawFd {
|
|
|
|
fn raw(&self) -> RawFd {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsRawFd> Source for &T {
|
|
|
|
fn raw(&self) -> RawFd {
|
|
|
|
self.as_raw_fd()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if #[cfg(windows)] {
|
|
|
|
use std::os::windows::io::{AsRawSocket, RawSocket};
|
|
|
|
|
|
|
|
/// A [`RawSocket`] or a reference to a type implementing [`AsRawSocket`].
|
|
|
|
pub trait Source {
|
|
|
|
/// Returns the [`RawSocket`] for this I/O object.
|
|
|
|
fn raw(&self) -> RawSocket;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Source for RawSocket {
|
|
|
|
fn raw(&self) -> RawSocket {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsRawSocket> Source for &T {
|
|
|
|
fn raw(&self) -> RawSocket {
|
2020-08-06 13:10:39 +00:00
|
|
|
self.as_raw_socket()
|
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 {
|
|
|
|
io::Error::new(
|
|
|
|
#[cfg(not(polling_no_unsupported_error_kind))]
|
|
|
|
io::ErrorKind::Unsupported,
|
|
|
|
#[cfg(polling_no_unsupported_error_kind)]
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
err.into(),
|
|
|
|
)
|
|
|
|
}
|