From 7cb8137fae716f78abad14706d9cb464628a45d8 Mon Sep 17 00:00:00 2001 From: Marcin Mielniczuk Date: Wed, 11 Dec 2019 19:24:25 +0100 Subject: [PATCH] Avoid issuing syscalls if we're requested to return immediately --- .../src/sys/windows/hostcalls_impl/misc.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/crates/wasi-common/src/sys/windows/hostcalls_impl/misc.rs b/crates/wasi-common/src/sys/windows/hostcalls_impl/misc.rs index 3276045a4b..4df00f4ea0 100644 --- a/crates/wasi-common/src/sys/windows/hostcalls_impl/misc.rs +++ b/crates/wasi-common/src/sys/windows/hostcalls_impl/misc.rs @@ -188,10 +188,9 @@ pub(crate) fn poll_oneoff( let new_event = make_read_event(&event, size); events.push(new_event) } - } + } else if !stdin_events.is_empty() { + // There are some stdin poll requests and there's no data available immediately - // There are some stdin poll requests and there's no data available immediately - if !stdin_events.is_empty() { // We are busy-polling the stdin with delay, unfortunately. // // We'd like to do the following: @@ -213,6 +212,15 @@ pub(crate) fn poll_oneoff( // // However, polling stdin is a relatively infrequent use case, so this hopefully won't be // a major issue. + let timeout_duration = timeout + .map(|t| t.delay.try_into().map(Duration::from_nanos)) + .transpose()?; + + // avoid issuing more syscalls if we're requested to return immediately + if timeout_duration == Some(Duration::from_nanos(0)) { + return Ok(()); + } + let poll_interval = Duration::from_millis(10); let poll_start = Instant::now();