lustre_executor/io/
timer.rs1use crate::TimerData;
2use futures::future::Future;
3use std::sync::{Arc, Mutex};
4use std::task::{Context, Poll as TaskPoll};
5use std::time::Duration;
6
7pub struct TimerFuture {
9 deadline: std::time::Instant,
10 timer_data: Arc<Mutex<TimerData>>,
11 registered: bool,
12}
13
14impl TimerFuture {
15 pub fn new(duration: Duration, timer_data: Arc<Mutex<TimerData>>) -> Self {
17 Self {
18 deadline: std::time::Instant::now() + duration,
19 timer_data,
20 registered: false,
21 }
22 }
23}
24
25impl Future for TimerFuture {
26 type Output = ();
27
28 fn poll(mut self: std::pin::Pin<&mut Self>, cx: &mut Context) -> TaskPoll<Self::Output> {
29 if std::time::Instant::now() >= self.deadline {
30 TaskPoll::Ready(())
31 } else {
32 if !self.registered {
33 self.timer_data
34 .lock()
35 .unwrap()
36 .register_timer(self.deadline, cx.waker().clone());
37 self.registered = true;
38 }
39 TaskPoll::Pending
40 }
41 }
42}