Skip to content

Commit

Permalink
Auto merge of #410 - fiveop:epoll_create1, r=posborne
Browse files Browse the repository at this point in the history
Add epoll_create1

In order to get @kubo39's PR #384 forward, I cleaned up the commit history a bit and added `EpollEvent` back.

Since this module is used by mio, maybe @carllerche could comment on these changes.
  • Loading branch information
homu committed Sep 16, 2016
2 parents 7c0570d + 2b0c991 commit 3c8ff4a
Showing 1 changed file with 30 additions and 38 deletions.
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

0 comments on commit 3c8ff4a

Please sign in to comment.