Skip to content

Commit 3c8ff4a

Browse files
committed
Auto merge of #410 - fiveop:epoll_create1, r=posborne
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.
2 parents 7c0570d + 2b0c991 commit 3c8ff4a

File tree

1 file changed

+30
-38
lines changed

1 file changed

+30
-38
lines changed

src/sys/epoll.rs

+30-38
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
use {Errno, Result};
2-
use libc::c_int;
2+
use libc::{self, c_int};
33
use std::os::unix::io::RawFd;
44

5-
mod ffi {
6-
use libc::{c_int};
7-
use super::EpollEvent;
8-
9-
extern {
10-
pub fn epoll_create(size: c_int) -> c_int;
11-
pub fn epoll_ctl(epfd: c_int, op: c_int, fd: c_int, event: *const EpollEvent) -> c_int;
12-
pub fn epoll_wait(epfd: c_int, events: *mut EpollEvent, max_events: c_int, timeout: c_int) -> c_int;
13-
}
14-
}
15-
165
bitflags!(
176
#[repr(C)]
18-
flags EpollEventKind: u32 {
7+
flags EpollFlags: u32 {
198
const EPOLLIN = 0x001,
209
const EPOLLPRI = 0x002,
2110
const EPOLLOUT = 0x004,
@@ -42,54 +31,57 @@ pub enum EpollOp {
4231
EpollCtlMod = 3
4332
}
4433

45-
#[cfg(not(target_arch = "x86_64"))]
46-
#[derive(Clone, Copy)]
47-
#[repr(C)]
48-
pub struct EpollEvent {
49-
pub events: EpollEventKind,
50-
pub data: u64
34+
libc_bitflags!{
35+
flags EpollCreateFlags: c_int {
36+
EPOLL_CLOEXEC,
37+
}
5138
}
5239

53-
#[cfg(target_arch = "x86_64")]
5440
#[derive(Clone, Copy)]
55-
#[repr(C, packed)]
41+
#[repr(C)]
5642
pub struct EpollEvent {
57-
pub events: EpollEventKind,
58-
pub data: u64
43+
event: libc::epoll_event,
5944
}
6045

61-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
62-
#[test]
63-
fn test_epoll_event_size() {
64-
use std::mem::size_of;
65-
assert_eq!(size_of::<EpollEvent>(), 12);
66-
}
46+
impl EpollEvent {
47+
pub fn new(events: EpollFlags, data: u64) -> EpollEvent {
48+
EpollEvent { event: libc::epoll_event { events: events.bits(), u64: data } }
49+
}
50+
51+
pub fn events(&self) -> EpollFlags {
52+
EpollFlags::from_bits(self.event.events).unwrap()
53+
}
6754

68-
#[cfg(target_arch = "arm")]
69-
#[test]
70-
fn test_epoll_event_size() {
71-
use std::mem::size_of;
72-
assert_eq!(size_of::<EpollEvent>(), 16);
55+
pub fn data(&self) -> u64 {
56+
self.event.u64
57+
}
7358
}
7459

7560
#[inline]
7661
pub fn epoll_create() -> Result<RawFd> {
77-
let res = unsafe { ffi::epoll_create(1024) };
62+
let res = unsafe { libc::epoll_create(1024) };
63+
64+
Errno::result(res)
65+
}
66+
67+
#[inline]
68+
pub fn epoll_create1(flags: EpollCreateFlags) -> Result<RawFd> {
69+
let res = unsafe { libc::epoll_create1(flags.bits()) };
7870

7971
Errno::result(res)
8072
}
8173

8274
#[inline]
83-
pub fn epoll_ctl(epfd: RawFd, op: EpollOp, fd: RawFd, event: &EpollEvent) -> Result<()> {
84-
let res = unsafe { ffi::epoll_ctl(epfd, op as c_int, fd, event as *const EpollEvent) };
75+
pub fn epoll_ctl(epfd: RawFd, op: EpollOp, fd: RawFd, event: &mut EpollEvent) -> Result<()> {
76+
let res = unsafe { libc::epoll_ctl(epfd, op as c_int, fd, &mut event.event) };
8577

8678
Errno::result(res).map(drop)
8779
}
8880

8981
#[inline]
9082
pub fn epoll_wait(epfd: RawFd, events: &mut [EpollEvent], timeout_ms: isize) -> Result<usize> {
9183
let res = unsafe {
92-
ffi::epoll_wait(epfd, events.as_mut_ptr(), events.len() as c_int, timeout_ms as c_int)
84+
libc::epoll_wait(epfd, events.as_mut_ptr() as *mut libc::epoll_event, events.len() as c_int, timeout_ms as c_int)
9385
};
9486

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

0 commit comments

Comments
 (0)