summaryrefslogtreecommitdiff
path: root/src/poll.rs
blob: 72988d8c3e7d9f237bcce9b05fb936bd29e92034 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use libc;
use {Errno, Result};

#[repr(C)]
#[derive(Clone, Copy)]
pub struct PollFd {
    pollfd: libc::pollfd,
}

impl PollFd {
    pub fn new(fd: libc::c_int, events: EventFlags, revents: EventFlags) -> PollFd {
        PollFd {
            pollfd: libc::pollfd {
                fd: fd,
                events: events.bits(),
                revents: revents.bits(),
            },
        }
    }

    pub fn revents(&self) -> Option<EventFlags> {
        EventFlags::from_bits(self.pollfd.revents)
    }
}

libc_bitflags! {
    pub flags EventFlags: libc::c_short {
        POLLIN,
        POLLPRI,
        POLLOUT,
        POLLRDNORM,
        POLLWRNORM,
        POLLRDBAND,
        POLLWRBAND,
        POLLERR,
        POLLHUP,
        POLLNVAL,
    }
}

pub fn poll(fds: &mut [PollFd], timeout: libc::c_int) -> Result<libc::c_int> {
    let res = unsafe {
        libc::poll(fds.as_mut_ptr() as *mut libc::pollfd,
                   fds.len() as libc::nfds_t,
                   timeout)
    };

    Errno::result(res)
}