diff --git a/tokio/src/runtime/time/entry.rs b/tokio/src/runtime/time/entry.rs index 69f93823551..4780cfcad32 100644 --- a/tokio/src/runtime/time/entry.rs +++ b/tokio/src/runtime/time/entry.rs @@ -298,9 +298,11 @@ pub(crate) struct TimerEntry { /// This is manipulated only under the inner mutex. TODO: Can we use loom /// cells for this? inner: StdUnsafeCell, - /// Initial deadline for the timer. This is used to register on the first + /// Deadline for the timer. This is used to register on the first /// poll, as we can't register prior to being pinned. - initial_deadline: Option, + deadline: Instant, + /// Whether the deadline has been registered. + registered: bool, /// Ensure the type is !Unpin _m: std::marker::PhantomPinned, } @@ -504,7 +506,8 @@ impl TimerEntry { Self { driver, inner: StdUnsafeCell::new(TimerShared::new()), - initial_deadline: Some(deadline), + deadline, + registered: false, _m: std::marker::PhantomPinned, } } @@ -513,8 +516,12 @@ impl TimerEntry { unsafe { &*self.inner.get() } } + pub(crate) fn deadline(&self) -> Instant { + self.deadline + } + pub(crate) fn is_elapsed(&self) -> bool { - !self.inner().state.might_be_registered() && self.initial_deadline.is_none() + !self.inner().state.might_be_registered() && self.registered } /// Cancels and deregisters the timer. This operation is irreversible. @@ -545,7 +552,8 @@ impl TimerEntry { } pub(crate) fn reset(mut self: Pin<&mut Self>, new_time: Instant) { - unsafe { self.as_mut().get_unchecked_mut() }.initial_deadline = None; + unsafe { self.as_mut().get_unchecked_mut() }.deadline = new_time; + unsafe { self.as_mut().get_unchecked_mut() }.registered = true; let tick = self.driver().time_source().deadline_to_tick(new_time); @@ -567,7 +575,8 @@ impl TimerEntry { panic!("{}", crate::util::error::RUNTIME_SHUTTING_DOWN_ERROR); } - if let Some(deadline) = self.initial_deadline { + if !self.registered { + let deadline = self.deadline; self.as_mut().reset(deadline); } diff --git a/tokio/src/time/sleep.rs b/tokio/src/time/sleep.rs index ee46a186c01..370a98b9902 100644 --- a/tokio/src/time/sleep.rs +++ b/tokio/src/time/sleep.rs @@ -235,7 +235,6 @@ pin_project! { cfg_trace! { #[derive(Debug)] struct Inner { - deadline: Instant, ctx: trace::AsyncOpTracingCtx, } } @@ -243,7 +242,6 @@ cfg_trace! { cfg_not_trace! { #[derive(Debug)] struct Inner { - deadline: Instant, } } @@ -297,11 +295,11 @@ impl Sleep { resource_span, }; - Inner { deadline, ctx } + Inner { ctx } }; #[cfg(not(all(tokio_unstable, feature = "tracing")))] - let inner = Inner { deadline }; + let inner = Inner {}; Sleep { inner, entry } } @@ -312,7 +310,7 @@ impl Sleep { /// Returns the instant at which the future will complete. pub fn deadline(&self) -> Instant { - self.inner.deadline + self.entry.deadline() } /// Returns `true` if `Sleep` has elapsed. @@ -358,7 +356,6 @@ impl Sleep { fn reset_inner(self: Pin<&mut Self>, deadline: Instant) { let mut me = self.project(); me.entry.as_mut().reset(deadline); - (me.inner).deadline = deadline; #[cfg(all(tokio_unstable, feature = "tracing"))] {