Skip to content

Commit

Permalink
calculate the duration when timeout is absolute (#4682)
Browse files Browse the repository at this point in the history
  • Loading branch information
maminrayej committed May 14, 2024
2 parents 2fee36b + aa38882 commit d65b674
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/wasix/src/syscalls/wasi/clock_time_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::syscalls::*;
/// - `Timestamp *time`
/// The value of the clock in nanoseconds
//#[instrument(level = "trace", skip_all, fields(?clock_id, %precision), ret)]
#[instrument(level = "trace", skip_all, ret)]
pub fn clock_time_get<M: MemorySize>(
mut ctx: FunctionEnvMut<'_, WasiEnv>,
clock_id: Snapshot0Clockid,
Expand Down
21 changes: 19 additions & 2 deletions lib/wasix/src/syscalls/wasi/poll_oneoff.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use wasmer_wasix_types::wasi::{SubscriptionClock, Userdata};
use wasmer_wasix_types::wasi::{Subclockflags, SubscriptionClock, Userdata};

use super::*;
use crate::{
Expand Down Expand Up @@ -299,7 +299,24 @@ where
time_to_sleep = Duration::ZERO;
clock_subs.push((clock_info, s.userdata));
} else {
time_to_sleep = Duration::from_nanos(clock_info.timeout);
// if the timeout is specified as an absolute time in the future,
// we should calculate the duration we need to sleep
time_to_sleep = if clock_info
.flags
.contains(Subclockflags::SUBSCRIPTION_CLOCK_ABSTIME)
{
let now = wasi_try_ok!(platform_clock_time_get(
Snapshot0Clockid::Monotonic,
1
)) as u64;

Duration::from_nanos(clock_info.timeout)
- Duration::from_nanos(now as u64)
} else {
// if the timeout is not absolute, just use it as duration
Duration::from_nanos(clock_info.timeout)
};

clock_subs.push((clock_info, s.userdata));
}
continue;
Expand Down

0 comments on commit d65b674

Please sign in to comment.