|
| 1 | +use std::fmt; |
| 2 | +use std::future::Future; |
| 3 | +use std::pin::Pin; |
| 4 | +use std::sync::Arc; |
| 5 | +use std::task::{Context, Poll}; |
| 6 | +use std::time::Duration; |
| 7 | + |
| 8 | +use futures_util::task::AtomicWaker; |
| 9 | + |
| 10 | +use crate::{clear_timer, set_timer, TimerId}; |
| 11 | + |
| 12 | +/// A future representing the notification that an elapsed duration has |
| 13 | +/// occurred. |
| 14 | +/// |
| 15 | +/// This is created through the `Delay::new` method indicating when the future should fire. |
| 16 | +/// Note that these futures are not intended for high resolution timers. |
| 17 | +pub struct Delay { |
| 18 | + timer_id: Option<TimerId>, |
| 19 | + waker: Arc<AtomicWaker>, |
| 20 | + at: Duration, |
| 21 | +} |
| 22 | + |
| 23 | +impl Delay { |
| 24 | + /// Creates a new future which will fire at `dur` time into the future. |
| 25 | + pub fn new(dur: Duration) -> Delay { |
| 26 | + let now = duration_since_epoch(); |
| 27 | + |
| 28 | + Delay { |
| 29 | + timer_id: None, |
| 30 | + waker: Arc::new(AtomicWaker::new()), |
| 31 | + at: now + dur, |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /// Resets this timeout to an new timeout which will fire at the time |
| 36 | + /// specified by `at`. |
| 37 | + pub fn reset(&mut self, dur: Duration) { |
| 38 | + let now = duration_since_epoch(); |
| 39 | + self.at = now + dur; |
| 40 | + |
| 41 | + if let Some(id) = self.timer_id.take() { |
| 42 | + clear_timer(id); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl Future for Delay { |
| 48 | + type Output = (); |
| 49 | + |
| 50 | + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 51 | + let now = duration_since_epoch(); |
| 52 | + |
| 53 | + if now >= self.at { |
| 54 | + Poll::Ready(()) |
| 55 | + } else { |
| 56 | + // Register the latest waker |
| 57 | + self.waker.register(cx.waker()); |
| 58 | + |
| 59 | + // Register to global timer |
| 60 | + if self.timer_id.is_none() { |
| 61 | + let waker = self.waker.clone(); |
| 62 | + let id = set_timer(self.at - now, move || waker.wake()); |
| 63 | + self.timer_id = Some(id); |
| 64 | + } |
| 65 | + |
| 66 | + Poll::Pending |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl Drop for Delay { |
| 72 | + fn drop(&mut self) { |
| 73 | + if let Some(id) = self.timer_id.take() { |
| 74 | + clear_timer(id); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl fmt::Debug for Delay { |
| 80 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { |
| 81 | + f.debug_struct("Delay").finish() |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +fn duration_since_epoch() -> Duration { |
| 86 | + Duration::from_nanos(ic_cdk::api::time()) |
| 87 | +} |
0 commit comments