Skip to content

Commit

Permalink
Convert POSIX timeouts directly from Duration
Browse files Browse the repository at this point in the history
Both variants provide a direct conversion from Duration, so there is no
need for us to deal with converting the timeout and signedness.
  • Loading branch information
sirhcel committed Apr 9, 2024
1 parent 2d859aa commit bf7050c
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/posix/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ use std::os::fd::AsFd;
use std::slice;
use std::time::Duration;

#[cfg(not(target_os = "linux"))]
use nix::poll::PollTimeout;
use nix::poll::{PollFd, PollFlags};
#[cfg(target_os = "linux")]
use nix::sys::signal::SigSet;
#[cfg(target_os = "linux")]
use nix::sys::time::{TimeSpec, TimeValLike};
use nix::sys::time::TimeSpec;
#[cfg(not(target_os = "linux"))]
use std::io::ErrorKind;

pub fn wait_read_fd<F: AsFd>(fd: F, timeout: Duration) -> io::Result<()> {
wait_fd(fd, PollFlags::POLLIN, timeout)
Expand All @@ -24,19 +28,21 @@ fn wait_fd<F: AsFd>(fd: F, events: PollFlags, timeout: Duration) -> io::Result<(

let mut fd = PollFd::new(fd.as_fd(), events);

let milliseconds =
timeout.as_secs() as i64 * 1000 + i64::from(timeout.subsec_nanos()) / 1_000_000;
#[cfg(target_os = "linux")]
let wait_res = {
let timespec = TimeSpec::milliseconds(milliseconds);
let timespec = TimeSpec::from_duration(timeout);
nix::poll::ppoll(
slice::from_mut(&mut fd),
Some(timespec),
Some(SigSet::empty()),
)
};
#[cfg(not(target_os = "linux"))]
let wait_res = nix::poll::poll(slice::from_mut(&mut fd), milliseconds as nix::libc::c_int);
let wait_res = nix::poll::poll(
slice::from_mut(&mut fd),
PollTimeout::try_from(timeout.as_millis())
.map_err(|e| io::Error::new(ErrorKind::InvalidInput, e))?,
);

let wait = match wait_res {
Ok(r) => r,
Expand Down

0 comments on commit bf7050c

Please sign in to comment.