From 7508fd3596b448497681e4a93fcb616939153a9c Mon Sep 17 00:00:00 2001 From: Hiroki Noda Date: Sun, 10 Jul 2016 02:32:11 +0900 Subject: [PATCH 1/7] Add epoll_create1 and set EPOLL_CLOEXEC flag by default. --- src/sys/epoll.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs index 2090a0df1c..67449e8783 100644 --- a/src/sys/epoll.rs +++ b/src/sys/epoll.rs @@ -8,11 +8,19 @@ mod ffi { extern { pub fn epoll_create(size: c_int) -> c_int; + pub fn epoll_create1(flags: 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!( + flags EpollFdFlag: c_int { + const EPOLL_NONBLOCK = 0x800, + const EPOLL_CLOEXEC = 0x80000 + } +); + bitflags!( #[repr(C)] flags EpollEventKind: u32 { @@ -79,6 +87,13 @@ pub fn epoll_create() -> Result { Errno::result(res) } +#[inline] +pub fn epoll_create1(flags: EpollFdFlag) -> Result { + let res = unsafe { ffi::epoll_create1(flags.bits() | EPOLL_CLOEXEC.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) }; From 26d211710ca4f0ad8b8e8d3e3284d253d953aef5 Mon Sep 17 00:00:00 2001 From: Hiroki Noda Date: Sun, 10 Jul 2016 04:02:37 +0900 Subject: [PATCH 2/7] Not to set EPOLL_CLOEXEC by default. --- src/sys/epoll.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs index 67449e8783..af938bd3a7 100644 --- a/src/sys/epoll.rs +++ b/src/sys/epoll.rs @@ -89,7 +89,7 @@ pub fn epoll_create() -> Result { #[inline] pub fn epoll_create1(flags: EpollFdFlag) -> Result { - let res = unsafe { ffi::epoll_create1(flags.bits() | EPOLL_CLOEXEC.bits) }; + let res = unsafe { ffi::epoll_create1(flags.bits()) }; Errno::result(res) } From dfcf1266769a26004d956aecefa979a0a39a4962 Mon Sep 17 00:00:00 2001 From: Hiroki Noda Date: Sat, 23 Jul 2016 14:33:12 +0900 Subject: [PATCH 3/7] Use libc's declarations --- src/sys/epoll.rs | 65 ++++++------------------------------------------ 1 file changed, 8 insertions(+), 57 deletions(-) diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs index af938bd3a7..9b30f993eb 100644 --- a/src/sys/epoll.rs +++ b/src/sys/epoll.rs @@ -1,26 +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_create1(flags: 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!( - flags EpollFdFlag: c_int { - const EPOLL_NONBLOCK = 0x800, - const EPOLL_CLOEXEC = 0x80000 - } -); - bitflags!( #[repr(C)] flags EpollEventKind: u32 { @@ -50,61 +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::(), 12); -} - -#[cfg(target_arch = "arm")] -#[test] -fn test_epoll_event_size() { - use std::mem::size_of; - assert_eq!(size_of::(), 16); -} - #[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: EpollFdFlag) -> Result { - let res = unsafe { ffi::epoll_create1(flags.bits()) }; +pub fn epoll_create1(flags: c_int) -> Result { + 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 { +pub fn epoll_wait(epfd: RawFd, events: &mut [libc::epoll_event], 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(), events.len() as c_int, timeout_ms as c_int) }; Errno::result(res).map(|r| r as usize) From da36438f5bef8f885fb3ea9269b73ba85df32a07 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Wed, 31 Aug 2016 21:05:35 +0200 Subject: [PATCH 4/7] Readd EpollEvent --- src/sys/epoll.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs index 9b30f993eb..2cdc663859 100644 --- a/src/sys/epoll.rs +++ b/src/sys/epoll.rs @@ -31,6 +31,18 @@ pub enum EpollOp { EpollCtlMod = 3 } +#[derive(Clone, Copy)] +#[repr(C)] +pub struct EpollEvent { + event: libc::epoll_event, +} + +impl EpollEvent { + fn new(events: EpollEventKind, data: u64) -> EpollEvent { + EpollEvent { event: libc::epoll_event { events: events.bits(), u64: data } } + } +} + #[inline] pub fn epoll_create() -> Result { let res = unsafe { libc::epoll_create(1024) }; @@ -46,16 +58,16 @@ pub fn epoll_create1(flags: c_int) -> Result { } #[inline] -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) }; +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 [libc::epoll_event], timeout_ms: isize) -> Result { +pub fn epoll_wait(epfd: RawFd, events: &mut [EpollEvent], timeout_ms: isize) -> Result { let res = unsafe { - libc::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) From 584794d79125f65537d993ae8acd1ea9e981ad0a Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Thu, 8 Sep 2016 21:14:46 +0200 Subject: [PATCH 5/7] Add property readers to EpollEvent --- src/sys/epoll.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs index 2cdc663859..51f789a243 100644 --- a/src/sys/epoll.rs +++ b/src/sys/epoll.rs @@ -38,9 +38,17 @@ pub struct EpollEvent { } impl EpollEvent { - fn new(events: EpollEventKind, data: u64) -> EpollEvent { + pub fn new(events: EpollEventKind, data: u64) -> EpollEvent { EpollEvent { event: libc::epoll_event { events: events.bits(), u64: data } } } + + pub fn events(&self) -> EpollEventKind { + EpollEventKind::from_bits(self.event.events).unwrap() + } + + pub fn data(&self) -> u64 { + self.event.u64 + } } #[inline] From c9edda389b5edba97165c887aea6f4e5d11541a2 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Thu, 15 Sep 2016 22:07:43 +0200 Subject: [PATCH 6/7] Add EpollCrateFlags bitflag type. --- src/sys/epoll.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs index 51f789a243..8e18d8576a 100644 --- a/src/sys/epoll.rs +++ b/src/sys/epoll.rs @@ -31,6 +31,12 @@ pub enum EpollOp { EpollCtlMod = 3 } +libc_bitflags!{ + flags EpollCreateFlags: c_int { + EPOLL_CLOEXEC, + } +} + #[derive(Clone, Copy)] #[repr(C)] pub struct EpollEvent { @@ -59,8 +65,8 @@ pub fn epoll_create() -> Result { } #[inline] -pub fn epoll_create1(flags: c_int) -> Result { - let res = unsafe { libc::epoll_create1(flags) }; +pub fn epoll_create1(flags: EpollCreateFlags) -> Result { + let res = unsafe { libc::epoll_create1(flags.bits()) }; Errno::result(res) } From 2b0c9919acf1aa4abf5f1b1d998c4d7404284fcc Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Thu, 15 Sep 2016 22:08:04 +0200 Subject: [PATCH 7/7] Rename EpollEventKind to EpollFlags, according to convention. --- src/sys/epoll.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs index 8e18d8576a..9774318f46 100644 --- a/src/sys/epoll.rs +++ b/src/sys/epoll.rs @@ -4,7 +4,7 @@ use std::os::unix::io::RawFd; bitflags!( #[repr(C)] - flags EpollEventKind: u32 { + flags EpollFlags: u32 { const EPOLLIN = 0x001, const EPOLLPRI = 0x002, const EPOLLOUT = 0x004, @@ -44,12 +44,12 @@ pub struct EpollEvent { } impl EpollEvent { - pub fn new(events: EpollEventKind, data: u64) -> EpollEvent { + pub fn new(events: EpollFlags, data: u64) -> EpollEvent { EpollEvent { event: libc::epoll_event { events: events.bits(), u64: data } } } - pub fn events(&self) -> EpollEventKind { - EpollEventKind::from_bits(self.event.events).unwrap() + pub fn events(&self) -> EpollFlags { + EpollFlags::from_bits(self.event.events).unwrap() } pub fn data(&self) -> u64 {