From 05ffdc26ec08a7ec543dac6336c8bb4d3f031ba3 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Thu, 25 Aug 2022 10:18:48 -0700 Subject: [PATCH] Implement I/O timeouts that specify the REALTIME clock. (#4777) 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. --- crates/wasi-common/src/snapshots/preview_1.rs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/crates/wasi-common/src/snapshots/preview_1.rs b/crates/wasi-common/src/snapshots/preview_1.rs index 6e2506480c..7907be103b 100644 --- a/crates/wasi-common/src/snapshots/preview_1.rs +++ b/crates/wasi-common/src/snapshots/preview_1.rs @@ -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"))?, },