fix(timer): ensure the runtime is working for timers

master
dignifiedquire 5 years ago
parent 093d640ad7
commit 1c1c168e1b

@ -5,7 +5,7 @@ use std::time::Duration;
use pin_project_lite::pin_project; use pin_project_lite::pin_project;
use crate::task::{Context, Poll}; use crate::task::{Context, Poll};
use crate::utils::Timer; use crate::utils::{timer_after, Timer};
pin_project! { pin_project! {
#[doc(hidden)] #[doc(hidden)]
@ -20,7 +20,7 @@ pin_project! {
impl<F> DelayFuture<F> { impl<F> DelayFuture<F> {
pub fn new(future: F, dur: Duration) -> DelayFuture<F> { pub fn new(future: F, dur: Duration) -> DelayFuture<F> {
let delay = Timer::after(dur); let delay = timer_after(dur);
DelayFuture { future, delay } DelayFuture { future, delay }
} }

@ -7,7 +7,7 @@ use std::time::Duration;
use pin_project_lite::pin_project; use pin_project_lite::pin_project;
use crate::task::{Context, Poll}; use crate::task::{Context, Poll};
use crate::utils::Timer; use crate::utils::{timer_after, Timer};
/// Awaits a future or times out after a duration of time. /// Awaits a future or times out after a duration of time.
/// ///
@ -51,7 +51,7 @@ impl<F> TimeoutFuture<F> {
pub(super) fn new(future: F, dur: Duration) -> TimeoutFuture<F> { pub(super) fn new(future: F, dur: Duration) -> TimeoutFuture<F> {
TimeoutFuture { TimeoutFuture {
future, future,
delay: Timer::after(dur), delay: timer_after(dur),
} }
} }
} }

@ -6,7 +6,7 @@ use std::time::Duration;
use pin_project_lite::pin_project; use pin_project_lite::pin_project;
use crate::io; use crate::io;
use crate::utils::Timer; use crate::utils::{timer_after, Timer};
/// Awaits an I/O future or times out after a duration of time. /// Awaits an I/O future or times out after a duration of time.
/// ///
@ -37,7 +37,7 @@ where
F: Future<Output = io::Result<T>>, F: Future<Output = io::Result<T>>,
{ {
Timeout { Timeout {
timeout: Timer::after(dur), timeout: timer_after(dur),
future: f, future: f,
} }
.await .await

@ -4,7 +4,7 @@ use std::task::{Context, Poll};
use std::time::Duration; use std::time::Duration;
use crate::stream::Stream; use crate::stream::Stream;
use crate::utils::Timer; use crate::utils::{timer_after, Timer};
/// Creates a new stream that yields at a set interval. /// Creates a new stream that yields at a set interval.
/// ///
@ -45,7 +45,7 @@ use crate::utils::Timer;
#[cfg_attr(feature = "docs", doc(cfg(unstable)))] #[cfg_attr(feature = "docs", doc(cfg(unstable)))]
pub fn interval(dur: Duration) -> Interval { pub fn interval(dur: Duration) -> Interval {
Interval { Interval {
delay: Timer::after(dur), delay: timer_after(dur),
interval: dur, interval: dur,
} }
} }
@ -72,7 +72,7 @@ impl Stream for Interval {
return Poll::Pending; return Poll::Pending;
} }
let interval = self.interval; let interval = self.interval;
let _ = std::mem::replace(&mut self.delay, Timer::after(interval)); let _ = std::mem::replace(&mut self.delay, timer_after(interval));
Poll::Ready(Some(())) Poll::Ready(Some(()))
} }
} }

@ -6,7 +6,7 @@ use pin_project_lite::pin_project;
use crate::stream::Stream; use crate::stream::Stream;
use crate::task::{Context, Poll}; use crate::task::{Context, Poll};
use crate::utils::Timer; use crate::utils::{timer_after, Timer};
pin_project! { pin_project! {
#[doc(hidden)] #[doc(hidden)]
@ -24,7 +24,7 @@ impl<S> Delay<S> {
pub(super) fn new(stream: S, dur: Duration) -> Self { pub(super) fn new(stream: S, dur: Duration) -> Self {
Delay { Delay {
stream, stream,
delay: Timer::after(dur), delay: timer_after(dur),
delay_done: false, delay_done: false,
} }
} }

@ -6,7 +6,7 @@ use pin_project_lite::pin_project;
use crate::stream::Stream; use crate::stream::Stream;
use crate::task::{Context, Poll}; use crate::task::{Context, Poll};
use crate::utils::Timer; use crate::utils::{timer_after, Timer};
pin_project! { pin_project! {
/// A stream that only yields one element once every `duration`. /// A stream that only yields one element once every `duration`.
@ -35,7 +35,7 @@ impl<S: Stream> Throttle<S> {
stream, stream,
duration, duration,
blocked: false, blocked: false,
delay: Timer::after(Duration::default()), delay: timer_after(Duration::default()),
} }
} }
} }
@ -59,7 +59,7 @@ impl<S: Stream> Stream for Throttle<S> {
Poll::Ready(None) => Poll::Ready(None), Poll::Ready(None) => Poll::Ready(None),
Poll::Ready(Some(v)) => { Poll::Ready(Some(v)) => {
*this.blocked = true; *this.blocked = true;
let _ = std::mem::replace(&mut *this.delay, Timer::after(*this.duration)); let _ = std::mem::replace(&mut *this.delay, timer_after(*this.duration));
Poll::Ready(Some(v)) Poll::Ready(Some(v))
} }
} }

@ -8,7 +8,7 @@ use pin_project_lite::pin_project;
use crate::stream::Stream; use crate::stream::Stream;
use crate::task::{Context, Poll}; use crate::task::{Context, Poll};
use crate::utils::Timer; use crate::utils::{timer_after, Timer};
pin_project! { pin_project! {
/// A stream with timeout time set /// A stream with timeout time set
@ -23,7 +23,7 @@ pin_project! {
impl<S: Stream> Timeout<S> { impl<S: Stream> Timeout<S> {
pub(crate) fn new(stream: S, dur: Duration) -> Self { pub(crate) fn new(stream: S, dur: Duration) -> Self {
let delay = Timer::after(dur); let delay = timer_after(dur);
Self { stream, delay } Self { stream, delay }
} }

@ -64,6 +64,13 @@ mod timer {
pub type Timer = smol::Timer; pub type Timer = smol::Timer;
} }
pub(crate) fn timer_after(dur: std::time::Duration) -> timer::Timer {
#[cfg(not(target_os = "unknown"))]
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
Timer::after(dur)
}
#[cfg(any( #[cfg(any(
all(target_arch = "wasm32", feature = "default"), all(target_arch = "wasm32", feature = "default"),
all(feature = "unstable", not(feature = "default")) all(feature = "unstable", not(feature = "default"))

Loading…
Cancel
Save