Go to file
John Nunley 77b4ed1156
feat: On RedoxOS, use epoll instead of the poll backend
Technically RedoxOS supports the poll syscall, so we already support
RedoxOS. However, this is very slow. This commit ports this code to
epoll, which should be more efficient.

Closes #176
2024-02-11 08:31:13 -08:00
.github feat: On RedoxOS, use epoll instead of the poll backend 2024-02-11 08:31:13 -08:00
examples tests: Fix clippy error in wait-signal 2024-01-27 21:17:58 -08:00
src feat: On RedoxOS, use epoll instead of the poll backend 2024-02-11 08:31:13 -08:00
tests bugfix: Handle interrupts while polling 2023-10-27 07:02:08 -07:00
.cirrus.yml ci: Update FreeBSD image to 13.2 2024-01-07 16:11:02 +09:00
.gitignore Initial commit 2020-08-06 15:05:24 +02:00
CHANGELOG.md v3.4.0 2024-02-05 19:18:30 -08:00
Cargo.toml feat: On RedoxOS, use epoll instead of the poll backend 2024-02-11 08:31:13 -08:00
Cross.toml ci: Fix Android breakage (#99) 2023-03-22 11:21:31 -07:00
LICENSE-APACHE Initial commit 2020-08-06 15:05:24 +02:00
LICENSE-MIT Initial commit 2020-08-06 15:05:24 +02:00
README.md docs: Fix CI badge and update links 2023-09-04 08:33:22 -07:00

README.md

polling

Build License Cargo Documentation

Portable interface to epoll, kqueue, event ports, and IOCP.

Supported platforms:

  • epoll: Linux, Android
  • kqueue: macOS, iOS, tvOS, watchOS, FreeBSD, NetBSD, OpenBSD, DragonFly BSD
  • event ports: illumos, Solaris
  • poll: VxWorks, Fuchsia, other Unix systems
  • IOCP: Windows, Wine (version 7.13+)

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

use polling::{Event, Poller};
use std::net::TcpListener;

// Create a TCP listener.
let socket = TcpListener::bind("127.0.0.1:8000")?;
socket.set_nonblocking(true)?;
let key = 7; // Arbitrary key identifying the socket.

// Create a poller and register interest in readability on the socket.
let poller = Poller::new()?;
poller.add(&socket, Event::readable(key))?;

// 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.
            poller.modify(&socket, Event::readable(key))?;
        }
    }
}

License

Licensed under either of

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.