wasi: Fix sleeping for Duration::MAX
This commit fixes an assert in the WASI-specific implementation of thread sleep to ensure that sleeping for a very large period of time blocks instead of panicking. This can come up when testing programs that sleep "forever", for example.
This commit is contained in:
parent
6cf068db56
commit
c824c1ada7
1 changed files with 29 additions and 28 deletions
|
@ -136,17 +136,17 @@ impl Thread {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sleep(dur: Duration) {
|
pub fn sleep(dur: Duration) {
|
||||||
let nanos = dur.as_nanos();
|
let mut nanos = dur.as_nanos();
|
||||||
assert!(nanos <= u64::MAX as u128);
|
while nanos > 0 {
|
||||||
|
|
||||||
const USERDATA: wasi::Userdata = 0x0123_45678;
|
const USERDATA: wasi::Userdata = 0x0123_45678;
|
||||||
|
|
||||||
let clock = wasi::SubscriptionClock {
|
let clock = wasi::SubscriptionClock {
|
||||||
id: wasi::CLOCKID_MONOTONIC,
|
id: wasi::CLOCKID_MONOTONIC,
|
||||||
timeout: nanos as u64,
|
timeout: u64::try_from(nanos).unwrap_or(u64::MAX),
|
||||||
precision: 0,
|
precision: 0,
|
||||||
flags: 0,
|
flags: 0,
|
||||||
};
|
};
|
||||||
|
nanos -= u128::from(clock.timeout);
|
||||||
|
|
||||||
let in_ = wasi::Subscription {
|
let in_ = wasi::Subscription {
|
||||||
userdata: USERDATA,
|
userdata: USERDATA,
|
||||||
|
@ -169,6 +169,7 @@ impl Thread {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn join(self) {
|
pub fn join(self) {
|
||||||
cfg_if::cfg_if! {
|
cfg_if::cfg_if! {
|
||||||
|
|
Loading…
Add table
Reference in a new issue