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. #384

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ preadv_pwritev = []
signalfd = []

[dependencies]
libc = "0.2.13"
libc = { git = "https://github.com/rust-lang/libc" }
bitflags = "0.4"
cfg-if = "0.1.0"
void = "1.0.2"
Expand Down
56 changes: 11 additions & 45 deletions src/sys/epoll.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
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 {
Expand Down Expand Up @@ -42,54 +31,31 @@ 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
}

#[cfg(target_arch = "x86_64")]
#[derive(Clone, Copy)]
#[repr(C, packed)]
pub struct EpollEvent {
pub events: EpollEventKind,
pub data: u64
}

#[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);
}
#[inline]
pub fn epoll_create() -> Result<RawFd> {
let res = unsafe { libc::epoll_create(1024) };

#[cfg(target_arch = "arm")]
#[test]
fn test_epoll_event_size() {
use std::mem::size_of;
assert_eq!(size_of::<EpollEvent>(), 16);
Errno::result(res)
}

#[inline]
pub fn epoll_create() -> Result<RawFd> {
let res = unsafe { ffi::epoll_create(1024) };
pub fn epoll_create1(flags: c_int) -> Result<RawFd> {
let res = unsafe { libc::epoll_create1(flags) };

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 libc::epoll_event) -> Result<()> {
let res = unsafe { libc::epoll_ctl(epfd, op as c_int, fd, event as *mut libc::epoll_event) };

Errno::result(res).map(drop)
}

#[inline]
pub fn epoll_wait(epfd: RawFd, events: &mut [EpollEvent], timeout_ms: isize) -> Result<usize> {
pub fn epoll_wait(epfd: RawFd, events: &mut [libc::epoll_event], 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(), events.len() as c_int, timeout_ms as c_int)
};

Errno::result(res).map(|r| r as usize)
Expand Down