Browse Source

Allow sleep form of poll_oneoff for wasmtime-wasi (#8429)

* Allow sleep form of poll_oneoff for wasmtime-wasi

Allow sleep form of poll_oneoff for wasmtime-wasi crate, which just
sleeps for relative time using sleep from the std::thread.
This patch derives from the commit "Support sleep forms of poll_oneoff"
6b40724d18. Applying this patch would
help improve the real-time performance of wasmtime.

Closes #8428

Signed-off-by: Daichi Fukui <daichi1.fukui@toshiba.co.jp>

* Run cargo fmt

---------

Signed-off-by: Daichi Fukui <daichi1.fukui@toshiba.co.jp>
Co-authored-by: Alex Crichton <alex@alexcrichton.com>
pull/8476/head
daichifukui 7 months ago
committed by GitHub
parent
commit
09b522101d
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 29
      crates/wasi/src/preview1.rs

29
crates/wasi/src/preview1.rs

@ -2321,6 +2321,35 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiP1Ctx {
// Indefinite sleeping is not supported in preview1.
return Err(types::Errno::Inval.into());
}
// This is a special case where `poll_oneoff` is just sleeping
// on a single relative timer event. This special case was added
// after experimental observations showed that std::thread::sleep
// results in more consistent sleep times. This design ensures that
// wasmtime can handle real-time requirements more accurately.
if nsubscriptions == 1 {
let sub = subs.read()?;
if let types::SubscriptionU::Clock(clocksub) = sub.u {
if !clocksub
.flags
.contains(types::Subclockflags::SUBSCRIPTION_CLOCK_ABSTIME)
&& self.ctx().allow_blocking_current_thread
{
std::thread::sleep(std::time::Duration::from_nanos(clocksub.timeout));
events.write(types::Event {
userdata: sub.userdata,
error: types::Errno::Success,
type_: types::Eventtype::Clock,
fd_readwrite: types::EventFdReadwrite {
flags: types::Eventrwflags::empty(),
nbytes: 1,
},
})?;
return Ok(1);
}
}
}
let subs = subs.as_array(nsubscriptions);
let events = events.as_array(nsubscriptions);

Loading…
Cancel
Save