Skip to content

Commit

Permalink
Implement I/O timeouts that specify the REALTIME clock. (#4777)
Browse files Browse the repository at this point in the history
POSIX specifies that functions like `nanosleep` use the REALTIME clock,
so allow WASI `poll_oneoff` calls to use the REALTIME clock, at least
for non-absolute intervals. POSIX specifies that the timeouts should not
be affected by subsequent `clock_settime` calls, so they behave the same
way as MONOTONIC clock requests, so we can implement them as monotonic
requests.
  • Loading branch information
sunfishcode authored Aug 25, 2022
1 parent 9386409 commit 05ffdc2
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions crates/wasi-common/src/snapshots/preview_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,33 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
sub.userdata.into(),
)
}
types::Clockid::Realtime => {
// POSIX specifies that functions like `nanosleep` and others use the
// `REALTIME` clock. But it also says that `clock_settime` has no effect
// on threads waiting in these functions. MONOTONIC should always have
// resolution at least as good as REALTIME, so we can translate a
// non-absolute `REALTIME` request into a `MONOTONIC` request.
let clock = self.clocks.monotonic.deref();
let precision = Duration::from_nanos(clocksub.precision);
let duration = Duration::from_nanos(clocksub.timeout);
let deadline = if clocksub
.flags
.contains(types::Subclockflags::SUBSCRIPTION_CLOCK_ABSTIME)
{
return Err(Error::not_supported());
} else {
clock
.now(precision)
.checked_add(duration)
.ok_or_else(|| Error::overflow().context("deadline"))?
};
poll.subscribe_monotonic_clock(
clock,
deadline,
precision,
sub.userdata.into(),
)
}
_ => Err(Error::invalid_argument()
.context("timer subscriptions only support monotonic timer"))?,
},
Expand Down

0 comments on commit 05ffdc2

Please sign in to comment.