Skip to content

Commit

Permalink
switch to instant+bool combo
Browse files Browse the repository at this point in the history
  • Loading branch information
conradludgate committed Feb 6, 2023
1 parent 475b266 commit d31290b
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions tokio/src/runtime/time/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,19 +298,15 @@ pub(crate) struct TimerEntry {
/// This is manipulated only under the inner mutex. TODO: Can we use loom
/// cells for this?
inner: StdUnsafeCell<TimerShared>,
/// 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.
deadline: Deadline,
deadline: Instant,
/// Whether the deadline has been registered.
registered: bool,
/// Ensure the type is !Unpin
_m: std::marker::PhantomPinned,
}

#[derive(Debug)]
enum Deadline {
Unregistered(Instant),
Registered(Instant),
}

unsafe impl Send for TimerEntry {}
unsafe impl Sync for TimerEntry {}

Expand Down Expand Up @@ -510,7 +506,8 @@ impl TimerEntry {
Self {
driver,
inner: StdUnsafeCell::new(TimerShared::new()),
deadline: Deadline::Unregistered(deadline),
deadline,
registered: false,
_m: std::marker::PhantomPinned,
}
}
Expand All @@ -520,15 +517,11 @@ impl TimerEntry {
}

pub(crate) fn deadline(&self) -> Instant {
match self.deadline {
Deadline::Unregistered(x) => x,
Deadline::Registered(x) => x,
}
self.deadline
}

pub(crate) fn is_elapsed(&self) -> bool {
!self.inner().state.might_be_registered()
&& matches!(self.deadline, Deadline::Registered(_))
!self.inner().state.might_be_registered() && self.registered
}

/// Cancels and deregisters the timer. This operation is irreversible.
Expand Down Expand Up @@ -559,7 +552,8 @@ impl TimerEntry {
}

pub(crate) fn reset(mut self: Pin<&mut Self>, new_time: Instant) {
unsafe { self.as_mut().get_unchecked_mut() }.deadline = Deadline::Registered(new_time);
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);

Expand All @@ -581,7 +575,8 @@ impl TimerEntry {
panic!("{}", crate::util::error::RUNTIME_SHUTTING_DOWN_ERROR);
}

if let Deadline::Unregistered(deadline) = self.deadline {
if !self.registered {
let deadline = self.deadline;
self.as_mut().reset(deadline);
}

Expand Down

0 comments on commit d31290b

Please sign in to comment.