Skip to content
This repository has been archived by the owner on Apr 16, 2021. It is now read-only.

Commit

Permalink
epoll: Add pwait_raw() and wait_raw() methods
Browse files Browse the repository at this point in the history
Both read directly into an &mut [libc::epoll_event]. That struct is
packed, but it can be used safely if values are copied out and not
borrowed (see rust-lang/rust#46043 for
details).
  • Loading branch information
cptpcrd committed May 31, 2020
1 parent a4e1a1c commit 5e8cb5c
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions src/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,27 @@ impl Epoll {
) -> io::Result<Int> {
let maxevents = events.len();

let mut ep_events = Vec::new();
ep_events.resize(maxevents, libc::epoll_event { events: 0, u64: 0 });

self.pwait_raw(&mut ep_events, timeout, sigmask)
.map(|res| {
for i in 0..(res as usize) {
events[i] = Event {
events: Events::from_bits_truncate(ep_events[i].events),
data: ep_events[i].u64,
};
}
res
})
}

pub fn pwait_raw(
&self,
events: &mut [libc::epoll_event],
timeout: Option<time::Duration>,
sigmask: Option<crate::signal::Sigset>,
) -> io::Result<Int> {
let raw_timeout: Int = match timeout {
Some(t) => t.as_millis().try_into().unwrap_or(Int::MAX),
None => -1,
Expand All @@ -136,34 +157,27 @@ impl Epoll {
None => std::ptr::null(),
};

let mut ep_events = Vec::new();

ep_events.resize(maxevents, libc::epoll_event { events: 0, u64: 0 });

crate::error::convert_neg_ret(unsafe {
libc::epoll_pwait(
self.fd,
ep_events.as_mut_ptr(),
maxevents as Int,
events.as_mut_ptr(),
events.len() as Int,
raw_timeout,
raw_sigmask,
)
})
.map(|res| {
for i in 0..(res as usize) {
events[i] = Event {
events: Events::from_bits_truncate(ep_events[i].events),
data: ep_events[i].u64,
};
}
res
})
}

#[inline]
pub fn wait(&self, events: &mut [Event], timeout: Option<time::Duration>) -> io::Result<Int> {
self.pwait(events, timeout, None)
}


#[inline]
pub fn wait_raw(&self, events: &mut [libc::epoll_event], timeout: Option<time::Duration>) -> io::Result<Int> {
self.pwait_raw(events, timeout, None)
}
}

impl AsRawFd for Epoll {
Expand Down

0 comments on commit 5e8cb5c

Please sign in to comment.