2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-04-14 15:10:48 -04:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
//! This module helps deno implement timers and performance APIs.
|
2021-04-14 15:10:48 -04:00
|
|
|
|
2023-05-17 15:59:55 -04:00
|
|
|
use crate::hr_timer_lock::hr_timer_lock;
|
2021-04-14 15:10:48 -04:00
|
|
|
use deno_core::error::AnyError;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::op;
|
2021-12-07 07:39:58 -05:00
|
|
|
use deno_core::CancelFuture;
|
|
|
|
use deno_core::CancelHandle;
|
2021-04-14 15:10:48 -04:00
|
|
|
use deno_core::OpState;
|
2021-12-07 07:39:58 -05:00
|
|
|
use deno_core::Resource;
|
|
|
|
use deno_core::ResourceId;
|
|
|
|
use std::borrow::Cow;
|
2021-04-14 15:10:48 -04:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use std::time::Duration;
|
|
|
|
use std::time::Instant;
|
|
|
|
|
|
|
|
pub trait TimersPermission {
|
|
|
|
fn allow_hrtime(&mut self) -> bool;
|
|
|
|
fn check_unstable(&self, state: &OpState, api_name: &'static str);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type StartTime = Instant;
|
|
|
|
|
|
|
|
// Returns a milliseconds and nanoseconds subsec
|
|
|
|
// since the start time of the deno runtime.
|
|
|
|
// If the High precision flag is not set, the
|
|
|
|
// nanoseconds are rounded on 2ms.
|
2022-08-28 02:51:49 -04:00
|
|
|
#[op(fast)]
|
2022-09-07 06:51:47 -04:00
|
|
|
pub fn op_now<TP>(state: &mut OpState, buf: &mut [u8])
|
2021-04-14 15:10:48 -04:00
|
|
|
where
|
|
|
|
TP: TimersPermission + 'static,
|
|
|
|
{
|
|
|
|
let start_time = state.borrow::<StartTime>();
|
2022-08-16 11:30:16 -04:00
|
|
|
let elapsed = start_time.elapsed();
|
|
|
|
let seconds = elapsed.as_secs();
|
2022-08-28 02:51:49 -04:00
|
|
|
let mut subsec_nanos = elapsed.subsec_nanos();
|
2021-04-14 15:10:48 -04:00
|
|
|
|
|
|
|
// If the permission is not enabled
|
|
|
|
// Round the nano result on 2 milliseconds
|
|
|
|
// see: https://developer.mozilla.org/en-US/docs/Web/API/DOMHighResTimeStamp#Reduced_time_precision
|
|
|
|
if !state.borrow_mut::<TP>().allow_hrtime() {
|
2022-08-28 02:51:49 -04:00
|
|
|
let reduced_time_precision = 2_000_000; // 2ms in nanoseconds
|
2021-04-14 15:10:48 -04:00
|
|
|
subsec_nanos -= subsec_nanos % reduced_time_precision;
|
|
|
|
}
|
2022-09-07 06:51:47 -04:00
|
|
|
if buf.len() < 8 {
|
|
|
|
return;
|
2022-08-28 02:51:49 -04:00
|
|
|
}
|
2022-09-07 06:51:47 -04:00
|
|
|
let buf: &mut [u32] =
|
|
|
|
// SAFETY: buffer is at least 8 bytes long.
|
|
|
|
unsafe { std::slice::from_raw_parts_mut(buf.as_mut_ptr() as _, 2) };
|
|
|
|
buf[0] = seconds as u32;
|
2022-12-17 17:20:15 -05:00
|
|
|
buf[1] = subsec_nanos;
|
2021-04-14 15:10:48 -04:00
|
|
|
}
|
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
pub struct TimerHandle(Rc<CancelHandle>);
|
|
|
|
|
|
|
|
impl Resource for TimerHandle {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"timer".into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close(self: Rc<Self>) {
|
|
|
|
self.0.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a [`TimerHandle`] resource that can be used to cancel invocations of
|
|
|
|
/// [`op_sleep`].
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-05-13 04:36:31 -04:00
|
|
|
pub fn op_timer_handle(state: &mut OpState) -> ResourceId {
|
|
|
|
state
|
2021-12-07 07:39:58 -05:00
|
|
|
.resource_table
|
2022-05-13 04:36:31 -04:00
|
|
|
.add(TimerHandle(CancelHandle::new_rc()))
|
2021-12-07 07:39:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Waits asynchronously until either `millis` milliseconds have passed or the
|
|
|
|
/// [`TimerHandle`] resource given by `rid` has been canceled.
|
2022-10-17 07:29:16 -04:00
|
|
|
///
|
|
|
|
/// If the timer is canceled, this returns `false`. Otherwise, it returns `true`.
|
2022-09-06 13:38:37 -04:00
|
|
|
#[op(deferred)]
|
2021-12-07 07:39:58 -05:00
|
|
|
pub async fn op_sleep(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
millis: u64,
|
|
|
|
rid: ResourceId,
|
2022-10-17 07:29:16 -04:00
|
|
|
) -> Result<bool, AnyError> {
|
2021-12-07 07:39:58 -05:00
|
|
|
let handle = state.borrow().resource_table.get::<TimerHandle>(rid)?;
|
2023-05-17 15:59:55 -04:00
|
|
|
|
|
|
|
// If a timer is requested with <=100ms resolution, request the high-res timer. Since the default
|
|
|
|
// Windows timer period is 15ms, this means a 100ms timer could fire at 115ms (15% late). We assume that
|
|
|
|
// timers longer than 100ms are a reasonable cutoff here.
|
|
|
|
|
2023-06-26 09:10:27 -04:00
|
|
|
// The high-res timers on Windows are still limited. Unfortunately this means that our shortest duration 4ms timers
|
2023-05-17 15:59:55 -04:00
|
|
|
// can still be 25% late, but without a more complex timer system or spinning on the clock itself, we're somewhat
|
|
|
|
// bounded by the OS' scheduler itself.
|
|
|
|
let _hr_timer_lock = if millis <= 100 {
|
|
|
|
Some(hr_timer_lock())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2022-10-17 07:29:16 -04:00
|
|
|
let res = tokio::time::sleep(Duration::from_millis(millis))
|
2021-12-07 07:39:58 -05:00
|
|
|
.or_cancel(handle.0.clone())
|
2022-10-17 07:29:16 -04:00
|
|
|
.await;
|
2023-05-17 15:59:55 -04:00
|
|
|
|
|
|
|
// We release the high-res timer lock here, either by being cancelled or resolving.
|
2022-10-17 07:29:16 -04:00
|
|
|
Ok(res.is_ok())
|
2021-12-07 07:39:58 -05:00
|
|
|
}
|