Skip to content

Commit

Permalink
Support nullable timeout in ppoll
Browse files Browse the repository at this point in the history
  • Loading branch information
MikailBag committed Jul 5, 2020
1 parent a777389 commit e6e21ab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/poll.rs
Expand Up @@ -133,13 +133,17 @@ pub fn poll(fds: &mut [PollFd], timeout: libc::c_int) -> Result<libc::c_int> {
/// with the `sigmask` argument.
///
#[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "freebsd", target_os = "linux"))]
pub fn ppoll(fds: &mut [PollFd], timeout: TimeSpec, sigmask: SigSet) -> Result<libc::c_int> {

pub fn ppoll(fds: &mut [PollFd], timeout: Option<TimeSpec>, sigmask: SigSet) -> Result<libc::c_int> {
let timeout = &timeout;
let timeout = match timeout {
Some(t) => t.as_ref(),
None => core::ptr::null()
};

let res = unsafe {
libc::ppoll(fds.as_mut_ptr() as *mut libc::pollfd,
fds.len() as libc::nfds_t,
timeout.as_ref(),
timeout,
sigmask.as_ref())
};
Errno::result(res)
Expand Down
4 changes: 2 additions & 2 deletions test/test_poll.rs
Expand Up @@ -37,14 +37,14 @@ fn test_ppoll() {
let mut fds = [PollFd::new(r, PollFlags::POLLIN)];

// Poll an idle pipe. Should timeout
let nfds = ppoll(&mut fds, timeout, SigSet::empty()).unwrap();
let nfds = ppoll(&mut fds, Some(timeout), SigSet::empty()).unwrap();
assert_eq!(nfds, 0);
assert!(!fds[0].revents().unwrap().contains(PollFlags::POLLIN));

write(w, b".").unwrap();

// Poll a readable pipe. Should return an event.
let nfds = ppoll(&mut fds, timeout, SigSet::empty()).unwrap();
let nfds = ppoll(&mut fds, Some(timeout), SigSet::empty()).unwrap();
assert_eq!(nfds, 1);
assert!(fds[0].revents().unwrap().contains(PollFlags::POLLIN));
}

0 comments on commit e6e21ab

Please sign in to comment.