Skip to content

Commit

Permalink
Again less truncating for timeout values in Poll::poll
Browse files Browse the repository at this point in the history
Round up submillisecond wait times to the next millisecond on platforms
where submillisecond sleep times aren't supported. This makes the
behavior actually match the documentation.

See also #420, where this was implemented for the first time.
  • Loading branch information
tbu- authored and Thomasdezeeuw committed Dec 27, 2022
1 parent aaf712e commit 533c040
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
11 changes: 5 additions & 6 deletions src/sys/unix/selector/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,11 @@ impl Selector {

let timeout = timeout
.map(|to| {
let to_ms = to.as_millis();
// `Duration::as_millis` truncates, so round up to 1 ms. This
// avoids turning sub-millisecond timeouts into a zero timeout,
// unless the caller explicitly requests that by specifying a
// zero timeout.
let to_ms = to_ms + u128::from(to_ms == 0 && to.subsec_nanos() != 0);
// `Duration::as_millis` truncates, so round up. This avoids
// turning sub-millisecond timeouts into a zero timeout, unless
// the caller explicitly requests that by specifying a zero
// timeout.
let to_ms = (to + Duration::from_nanos(999_999)).as_millis();
cmp::min(MAX_SAFE_TIMEOUT, to_ms) as libc::c_int
})
.unwrap_or(-1);
Expand Down
17 changes: 6 additions & 11 deletions src/sys/windows/iocp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,12 @@ impl CompletionStatus {
#[inline]
fn duration_millis(dur: Option<Duration>) -> u32 {
if let Some(dur) = dur {
let dur_ms = dur.as_millis();
// as_millis() truncates, so round nonzero <1ms timeouts up to 1ms. This avoids turning
// submillisecond timeouts into immediate reutrns unless the caller explictly requests that
// by specifiying a zero timeout.
let dur_ms = dur_ms
+ if dur_ms == 0 && dur.subsec_nanos() != 0 {
1
} else {
0
};
std::cmp::min(dur_ms, u32::MAX as u128) as u32
// `Duration::as_millis` truncates, so round up. This avoids
// turning sub-millisecond timeouts into a zero timeout, unless
// the caller explicitly requests that by specifying a zero
// timeout.
let dur_ms = (dur + Duration::from_nanos(999_999)).as_millis();
cmp::min(dur_ms, u32::MAX as u128) as u32
} else {
u32::MAX
}
Expand Down

0 comments on commit 533c040

Please sign in to comment.