diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs index 2090a0df1c..9774318f46 100644 --- a/src/sys/epoll.rs +++ b/src/sys/epoll.rs @@ -1,21 +1,10 @@ use {Errno, Result}; -use libc::c_int; +use libc::{self, c_int}; use std::os::unix::io::RawFd; -mod ffi { - use libc::{c_int}; - use super::EpollEvent; - - extern { - pub fn epoll_create(size: c_int) -> c_int; - pub fn epoll_ctl(epfd: c_int, op: c_int, fd: c_int, event: *const EpollEvent) -> c_int; - pub fn epoll_wait(epfd: c_int, events: *mut EpollEvent, max_events: c_int, timeout: c_int) -> c_int; - } -} - bitflags!( #[repr(C)] - flags EpollEventKind: u32 { + flags EpollFlags: u32 { const EPOLLIN = 0x001, const EPOLLPRI = 0x002, const EPOLLOUT = 0x004, @@ -42,46 +31,49 @@ pub enum EpollOp { EpollCtlMod = 3 } -#[cfg(not(target_arch = "x86_64"))] -#[derive(Clone, Copy)] -#[repr(C)] -pub struct EpollEvent { - pub events: EpollEventKind, - pub data: u64 +libc_bitflags!{ + flags EpollCreateFlags: c_int { + EPOLL_CLOEXEC, + } } -#[cfg(target_arch = "x86_64")] #[derive(Clone, Copy)] -#[repr(C, packed)] +#[repr(C)] pub struct EpollEvent { - pub events: EpollEventKind, - pub data: u64 + event: libc::epoll_event, } -#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] -#[test] -fn test_epoll_event_size() { - use std::mem::size_of; - assert_eq!(size_of::(), 12); -} +impl EpollEvent { + pub fn new(events: EpollFlags, data: u64) -> EpollEvent { + EpollEvent { event: libc::epoll_event { events: events.bits(), u64: data } } + } + + pub fn events(&self) -> EpollFlags { + EpollFlags::from_bits(self.event.events).unwrap() + } -#[cfg(target_arch = "arm")] -#[test] -fn test_epoll_event_size() { - use std::mem::size_of; - assert_eq!(size_of::(), 16); + pub fn data(&self) -> u64 { + self.event.u64 + } } #[inline] pub fn epoll_create() -> Result { - let res = unsafe { ffi::epoll_create(1024) }; + let res = unsafe { libc::epoll_create(1024) }; + + Errno::result(res) +} + +#[inline] +pub fn epoll_create1(flags: EpollCreateFlags) -> Result { + let res = unsafe { libc::epoll_create1(flags.bits()) }; Errno::result(res) } #[inline] -pub fn epoll_ctl(epfd: RawFd, op: EpollOp, fd: RawFd, event: &EpollEvent) -> Result<()> { - let res = unsafe { ffi::epoll_ctl(epfd, op as c_int, fd, event as *const EpollEvent) }; +pub fn epoll_ctl(epfd: RawFd, op: EpollOp, fd: RawFd, event: &mut EpollEvent) -> Result<()> { + let res = unsafe { libc::epoll_ctl(epfd, op as c_int, fd, &mut event.event) }; Errno::result(res).map(drop) } @@ -89,7 +81,7 @@ pub fn epoll_ctl(epfd: RawFd, op: EpollOp, fd: RawFd, event: &EpollEvent) -> Res #[inline] pub fn epoll_wait(epfd: RawFd, events: &mut [EpollEvent], timeout_ms: isize) -> Result { let res = unsafe { - ffi::epoll_wait(epfd, events.as_mut_ptr(), events.len() as c_int, timeout_ms as c_int) + libc::epoll_wait(epfd, events.as_mut_ptr() as *mut libc::epoll_event, events.len() as c_int, timeout_ms as c_int) }; Errno::result(res).map(|r| r as usize)