| 56 | type Output = (); |
| 57 | |
| 58 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 59 | // Look at the shared state to see if the timer has already completed. |
| 60 | let mut shared_state = self.shared_state.lock().unwrap(); |
| 61 | |
| 62 | if shared_state.completed { |
| 63 | Poll::Ready(()) |
| 64 | } else { |
| 65 | // Set waker so that the thread can wake up the current task |
| 66 | // when the timer has completed, ensuring that the future is polled |
| 67 | // again and sees that `completed = true`. |
| 68 | // |
| 69 | // It's tempting to do this once rather than repeatedly cloning |
| 70 | // the waker each time. However, the `TimerFuture` can move between |
| 71 | // tasks on the executor, which could cause a stale waker pointing |
| 72 | // to the wrong task, preventing `TimerFuture` from waking up |
| 73 | // correctly. |
| 74 | // |
| 75 | // N.B. it's possible to check for this using the `Waker::will_wake` |
| 76 | // function, but we omit that here to keep things simple. |
| 77 | shared_state.waker = Some(cx.waker().clone()); |
| 78 | Poll::Pending |
| 79 | } |
| 80 | } |
| 81 | } |