|
@ -36,26 +36,20 @@ impl Future for TimerFuture { |
|
|
type Output = ();
|
|
|
type Output = ();
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
let mut state = self.state.lock().unwrap();
|
|
|
let mut state = self.state.lock().unwrap();
|
|
|
if matches!(*state, State::Completed) {
|
|
|
|
|
|
return Poll::Ready(());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if let State::NotStarted(duration) = *state {
|
|
|
|
|
|
let thread_state = self.state.clone();
|
|
|
|
|
|
thread::spawn(move || {
|
|
|
|
|
|
thread::sleep(duration);
|
|
|
|
|
|
let mut state = thread_state.lock().unwrap();
|
|
|
|
|
|
let waker = if let State::Running(waker) = &*state {
|
|
|
|
|
|
Some(waker.clone())
|
|
|
|
|
|
} else {
|
|
|
|
|
|
None
|
|
|
|
|
|
};
|
|
|
|
|
|
*state = State::Completed;
|
|
|
|
|
|
if let Some(w) = waker {
|
|
|
|
|
|
w.wake();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
match *state {
|
|
|
|
|
|
State::Completed => return Poll::Ready(()),
|
|
|
|
|
|
State::NotStarted(duration) => {
|
|
|
|
|
|
let thread_state = self.state.clone();
|
|
|
|
|
|
thread::spawn(move || {
|
|
|
|
|
|
thread::sleep(duration);
|
|
|
|
|
|
let mut state = thread_state.lock().unwrap();
|
|
|
|
|
|
if let State::Running(waker) = std::mem::replace(&mut *state, State::Completed) {
|
|
|
|
|
|
waker.wake();
|
|
|
|
|
|
};
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
State::Running(_) => {}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
*state = State::Running(cx.waker().clone());
|
|
|
*state = State::Running(cx.waker().clone());
|
|
|
Poll::Pending
|
|
|
Poll::Pending
|
|
|