Fix writing timeout events. Check that we only return one timeout event.

This commit is contained in:
Marcin Mielniczuk
2020-01-08 19:44:07 +01:00
parent a2b556f1b0
commit 54a398ad69
2 changed files with 15 additions and 6 deletions

View File

@@ -65,11 +65,17 @@ unsafe fn test_stdin_read() {
r#type: wasi::EVENTTYPE_CLOCK, r#type: wasi::EVENTTYPE_CLOCK,
u: wasi::SubscriptionU { clock }, u: wasi::SubscriptionU { clock },
}, },
// Make sure that timeout is returned only once even if there are multiple read events
wasi::Subscription { wasi::Subscription {
userdata: 1, userdata: 1,
r#type: wasi::EVENTTYPE_FD_READ, r#type: wasi::EVENTTYPE_FD_READ,
u: wasi::SubscriptionU { fd_readwrite }, u: wasi::SubscriptionU { fd_readwrite },
}, },
wasi::Subscription {
userdata: 42,
r#type: wasi::EVENTTYPE_FD_READ,
u: wasi::SubscriptionU { fd_readwrite },
},
]; ];
let out = poll_oneoff_impl(&r#in, 1); let out = poll_oneoff_impl(&r#in, 1);
let event = &out[0]; let event = &out[0];

View File

@@ -95,7 +95,7 @@ fn make_rw_event(event: &FdEventData, nbytes: Result<u64>) -> wasi::__wasi_event
} }
} }
fn make_timeout_event(event: &FdEventData, timeout: &ClockEventData) -> wasi::__wasi_event_t { fn make_timeout_event(timeout: &ClockEventData) -> wasi::__wasi_event_t {
wasi::__wasi_event_t { wasi::__wasi_event_t {
userdata: timeout.userdata, userdata: timeout.userdata,
r#type: wasi::__WASI_EVENTTYPE_CLOCK, r#type: wasi::__WASI_EVENTTYPE_CLOCK,
@@ -125,7 +125,12 @@ pub(crate) fn poll_oneoff(
// With no events to listen, poll_oneoff just becomes a sleep. // With no events to listen, poll_oneoff just becomes a sleep.
if fd_events.is_empty() { if fd_events.is_empty() {
match timeout_duration { match timeout_duration {
Some(t) => thread::sleep(t), Some(t) => {
thread::sleep(t);
let timeout_event = timeout.expect("timeout should be Some");
let new_event = make_timeout_event(&timeout_event);
events.push(new_event);
}
// `poll` invoked with nfds = 0, timeout = -1 appears to be an infinite sleep // `poll` invoked with nfds = 0, timeout = -1 appears to be an infinite sleep
// Even though the thread is not guanteed to remain parked forever, `poll(2)` // Even though the thread is not guanteed to remain parked forever, `poll(2)`
// mentions that spurious readiness notifications may occur, so it's probably fine // mentions that spurious readiness notifications may occur, so it's probably fine
@@ -236,11 +241,9 @@ pub(crate) fn poll_oneoff(
match timeout_occurred { match timeout_occurred {
Some(timeout_info) => { Some(timeout_info) => {
for event in stdin_events { let new_event = make_timeout_event(&timeout_info);
let new_event = make_timeout_event(&event, &timeout_info);
events.push(new_event); events.push(new_event);
} }
}
None => { None => {
// stdin became ready for reading // stdin became ready for reading
for event in stdin_events { for event in stdin_events {