pub fn poll<T: Into<PollTimeout>>(
fds: &mut [PollFd<'_>],
timeout: T,
) -> Result<c_int>Expand description
poll waits for one of a set of file descriptors to become ready to perform I/O.
(poll(2))
fds contains all PollFd to poll.
The function will return as soon as any event occur for any of these PollFds.
The timeout argument specifies the number of milliseconds that poll()
should block waiting for a file descriptor to become ready. The call
will block until either:
- a file descriptor becomes ready;
- the call is interrupted by a signal handler; or
- the timeout expires.
Note that the timeout interval will be rounded up to the system clock
granularity, and kernel scheduling delays mean that the blocking
interval may overrun by a small amount. Specifying a PollTimeout::NONE
in timeout means an infinite timeout. Specifying a timeout of
PollTimeout::ZERO causes poll() to return immediately, even if no file
descriptors are ready.
The return value contains the number of fds which have selected events (PollFd::revents).
ยงExamples
let (r0, w0) = pipe().unwrap();
let (r1, w1) = pipe().unwrap();
let mut pollfds = [
PollFd::new(r0.as_fd(), PollFlags::POLLIN),
PollFd::new(r1.as_fd(), PollFlags::POLLIN),
];
let nready = poll(&mut pollfds, PollTimeout::NONE).unwrap();
assert!(nready >= 1); // Since there is no timeout
let mut buf = [0u8; 80];
if pollfds[0].any().unwrap_or_default() {
read(&r0, &mut buf[..]);
} else if pollfds[1].any().unwrap_or_default() {
read(&r1, &mut buf[..]);
}