2020-08-06 13:02:59 +00:00
|
|
|
# polling
|
|
|
|
|
2020-12-26 14:47:13 +00:00
|
|
|
[![Build](https://github.com/smol-rs/polling/workflows/Build%20and%20test/badge.svg)](
|
|
|
|
https://github.com/smol-rs/polling/actions)
|
2021-02-14 04:36:54 +00:00
|
|
|
[![License](https://img.shields.io/badge/license-Apache--2.0_OR_MIT-blue.svg)](
|
2020-12-26 14:47:13 +00:00
|
|
|
https://github.com/smol-rs/polling)
|
2020-08-06 13:02:59 +00:00
|
|
|
[![Cargo](https://img.shields.io/crates/v/polling.svg)](
|
|
|
|
https://crates.io/crates/polling)
|
|
|
|
[![Documentation](https://docs.rs/polling/badge.svg)](
|
|
|
|
https://docs.rs/polling)
|
|
|
|
|
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-06 20:28:38 +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-06 20:28:38 +00:00
|
|
|
- [event ports](https://illumos.org/man/port_create): illumos, Solaris
|
2021-09-04 17:00:25 +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
|
|
|
|
|
|
|
Polling is done in oneshot mode, which means interest in I/O events needs to be reset after
|
|
|
|
an event is delivered if we're interested in the next event of the same kind.
|
|
|
|
|
|
|
|
Only one thread can be waiting for I/O events at a time.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2020-08-06 14:12:52 +00:00
|
|
|
```rust,no_run
|
2020-08-06 13:02:59 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
|
|
Licensed under either of
|
|
|
|
|
|
|
|
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
|
|
|
|
|
|
|
at your option.
|
|
|
|
|
|
|
|
#### Contribution
|
|
|
|
|
|
|
|
Unless you explicitly state otherwise, any contribution intentionally submitted
|
|
|
|
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
|
|
|
|
dual licensed as above, without any additional terms or conditions.
|