Crate polling

source ·
Expand description

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

Supported platforms:

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

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.

Only one thread can be waiting for I/O events at a time.

§Examples

use polling::{Event, Events, 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()?;
unsafe {
    poller.add(&socket, Event::readable(key))?;
}

// The event loop.
let mut events = Events::new();
loop {
    // Wait for at least one I/O event.
    events.clear();
    poller.wait(&mut events, None)?;

    for ev in events.iter() {
        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))?;
        }
    }
}

poller.delete(&socket)?;

Modules§

  • Platform-specific functionality.

Structs§

  • Indicates that a file descriptor or socket can read or write without blocking.
  • A container for I/O events.
  • Waits for I/O events.

Enums§

  • The mode in which the poller waits for I/O events.

Traits§

  • A resource with a raw file descriptor.
  • A resource with a borrowed file descriptor.