Skip to content

Commit ed91f2f

Browse files
committed
document poll module
1 parent f14c586 commit ed91f2f

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

src/poll.rs

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,29 @@
22
use sys::time::TimeSpec;
33
#[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "freebsd", target_os = "linux"))]
44
use sys::signal::SigSet;
5+
use std::os::unix::io::RawFd;
56

67
use libc;
78
use {Errno, Result};
89

10+
/// This is a wrapper around `libc::pollfd`.
11+
///
12+
/// It's meant to be used as an argument to the [`poll`](fn.poll.html) and
13+
/// [`ppoll`](fn.ppoll.html) functions to specify the events of interes
14+
/// for a specific file descriptor.
15+
///
16+
/// After a call to `poll` or `ppoll`, the events that occured can be
17+
/// retrieved by calling [`revents()`](#method.revents) on the `PollFd`.
918
#[repr(C)]
1019
#[derive(Clone, Copy)]
1120
pub struct PollFd {
1221
pollfd: libc::pollfd,
1322
}
1423

1524
impl PollFd {
16-
pub fn new(fd: libc::c_int, events: EventFlags) -> PollFd {
25+
/// Creates a new `PollFd` specifying the events of interested
26+
/// for a given file descriptor.
27+
pub fn new(fd: RawFd, events: EventFlags) -> PollFd {
1728
PollFd {
1829
pollfd: libc::pollfd {
1930
fd: fd,
@@ -23,26 +34,82 @@ impl PollFd {
2334
}
2435
}
2536

37+
/// Returns the events that occured in the last call to `poll` or `ppoll`.
2638
pub fn revents(&self) -> Option<EventFlags> {
2739
EventFlags::from_bits(self.pollfd.revents)
2840
}
2941
}
3042

3143
libc_bitflags! {
44+
/// These flags define the different events that can be monitored by `poll` and `ppoll`
3245
pub flags EventFlags: libc::c_short {
46+
/// There is data to read.
3347
POLLIN,
48+
/// There is some exceptional condition on the file descriptor.
49+
///
50+
/// Possibilities include:
51+
///
52+
/// * There is out-of-band data on a TCP socket (see
53+
/// [tcp(7)](http://man7.org/linux/man-pages/man7/tcp.7.html)).
54+
/// * A pseudoterminal master in packet mode has seen a state
55+
/// change on the slave (see
56+
/// [ioctl_tty(2)](http://man7.org/linux/man-pages/man2/ioctl_tty.2.html)).
57+
/// * A cgroup.events file has been modified (see
58+
/// [cgroups(7)](http://man7.org/linux/man-pages/man7/cgroups.7.html)).
3459
POLLPRI,
60+
/// Writing is now possible, though a write larger that the
61+
/// available space in a socket or pipe will still block (unless
62+
/// `O_NONBLOCK` is set).
3563
POLLOUT,
64+
/// Equivalent to [`POLLIN`](constant.POLLIN.html)
3665
POLLRDNORM,
66+
/// Equivalent to [`POLLOUT`](constant.POLLOUT.html)
3767
POLLWRNORM,
68+
/// Priority band data can be read (generally unused on Linux).
3869
POLLRDBAND,
70+
/// Priority data may be written.
3971
POLLWRBAND,
72+
/// Error condition (only returned in
73+
/// [`PollFd::revents`](struct.PollFd.html#method.revents);
74+
/// ignored in [`PollFd::new`](struct.PollFd.html#method.new)).
75+
/// This bit is also set for a file descriptor referring to the
76+
/// write end of a pipe when the read end has been closed.
4077
POLLERR,
78+
/// Hang up (only returned in [`PollFd::revents`](struct.PollFd.html#method.revents);
79+
/// ignored in [`PollFd::new`](struct.PollFd.html#method.new)).
80+
/// Note that when reading from a channel such as a pipe or a stream
81+
/// socket, this event merely indicates that the peer closed its
82+
/// end of the channel. Subsequent reads from the channel will
83+
/// return 0 (end of file) only after all outstanding data in the
84+
/// channel has been consumed.
4185
POLLHUP,
86+
/// Invalid request: `fd` not open (only returned in
87+
/// [`PollFd::revents`](struct.PollFd.html#method.revents);
88+
/// ignored in [`PollFd::new`](struct.PollFd.html#method.new)).
4289
POLLNVAL,
4390
}
4491
}
4592

93+
/// `poll` waits for one of a set of file descriptors to become ready to perform I/O.
94+
/// ([`poll(2)`](http://man7.org/linux/man-pages/man2/poll.2.html))
95+
///
96+
/// `fds` is a slice containing all [`PollFd`](struct.PollFd.html) to poll.
97+
/// The function will return as soon as one event registered in one of the `PollFd` occurs.
98+
///
99+
/// The `timeout` argument specifies the number of milliseconds that `poll()`
100+
/// should block waiting for a file descriptor to become ready. The call
101+
/// will block until either:
102+
///
103+
/// * a file descriptor becomes ready;
104+
/// * the call is interrupted by a signal handler; or
105+
/// * the timeout expires.
106+
///
107+
/// Note that the timeout interval will be rounded up to the system clock
108+
/// granularity, and kernel scheduling delays mean that the blocking
109+
/// interval may overrun by a small amount. Specifying a negative value
110+
/// in timeout means an infinite timeout. Specifying a timeout of zero
111+
/// causes `poll()` to return immediately, even if no file descriptors are
112+
/// ready.
46113
pub fn poll(fds: &mut [PollFd], timeout: libc::c_int) -> Result<libc::c_int> {
47114
let res = unsafe {
48115
libc::poll(fds.as_mut_ptr() as *mut libc::pollfd,
@@ -53,6 +120,13 @@ pub fn poll(fds: &mut [PollFd], timeout: libc::c_int) -> Result<libc::c_int> {
53120
Errno::result(res)
54121
}
55122

123+
/// `ppoll()` allows an application to safely wait until either a file
124+
/// descriptor becomes ready or until a signal is caught.
125+
/// ([`poll(2)`](http://man7.org/linux/man-pages/man2/poll.2.html))
126+
///
127+
/// `ppoll` behaves like `poll`, but let you precise what signals may interrupt it
128+
/// with the `sigmask` argument.
129+
///
56130
#[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "freebsd", target_os = "linux"))]
57131
pub fn ppoll(fds: &mut [PollFd], timeout: TimeSpec, sigmask: SigSet) -> Result<libc::c_int> {
58132

0 commit comments

Comments
 (0)