Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add epoll_create1 #410

Merged
merged 7 commits into from
Sep 16, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 30 additions & 38 deletions src/sys/epoll.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -42,54 +31,57 @@ 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::<EpollEvent>(), 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::<EpollEvent>(), 16);
pub fn data(&self) -> u64 {
self.event.u64
}
}

#[inline]
pub fn epoll_create() -> Result<RawFd> {
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<RawFd> {
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)
}

#[inline]
pub fn epoll_wait(epfd: RawFd, events: &mut [EpollEvent], timeout_ms: isize) -> Result<usize> {
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)
Expand Down