summaryrefslogtreecommitdiffstats
path: root/tokio-util
diff options
context:
space:
mode:
Diffstat (limited to 'tokio-util')
-rw-r--r--tokio-util/src/time/delay_queue.rs8
-rw-r--r--tokio-util/src/time/wheel/mod.rs49
2 files changed, 23 insertions, 34 deletions
diff --git a/tokio-util/src/time/delay_queue.rs b/tokio-util/src/time/delay_queue.rs
index b23c24e6..92c922b8 100644
--- a/tokio-util/src/time/delay_queue.rs
+++ b/tokio-util/src/time/delay_queue.rs
@@ -141,7 +141,7 @@ pub struct DelayQueue<T> {
delay: Option<Delay>,
/// Wheel polling state
- poll: wheel::Poll,
+ wheel_now: u64,
/// Instant at which the timer starts
start: Instant,
@@ -251,7 +251,7 @@ impl<T> DelayQueue<T> {
slab: Slab::with_capacity(capacity),
expired: Stack::default(),
delay: None,
- poll: wheel::Poll::new(0),
+ wheel_now: 0,
start: Instant::now(),
}
}
@@ -733,11 +733,11 @@ impl<T> DelayQueue<T> {
let now = crate::time::ms(delay.deadline() - self.start, crate::time::Round::Down);
- self.poll = wheel::Poll::new(now);
+ self.wheel_now = now;
}
// We poll the wheel to get the next value out before finding the next deadline.
- let wheel_idx = self.wheel.poll(&mut self.poll, &mut self.slab);
+ let wheel_idx = self.wheel.poll(self.wheel_now, &mut self.slab);
self.delay = self.next_deadline().map(sleep_until);
diff --git a/tokio-util/src/time/wheel/mod.rs b/tokio-util/src/time/wheel/mod.rs
index a2ef27fc..478037a3 100644
--- a/tokio-util/src/time/wheel/mod.rs
+++ b/tokio-util/src/time/wheel/mod.rs
@@ -51,13 +51,6 @@ pub(crate) enum InsertError {
Invalid,
}
-/// Poll expirations from the wheel
-#[derive(Debug, Default)]
-pub(crate) struct Poll {
- now: u64,
- expiration: Option<Expiration>,
-}
-
impl<T> Wheel<T>
where
T: Stack,
@@ -136,19 +129,18 @@ where
self.next_expiration().map(|expiration| expiration.deadline)
}
- pub(crate) fn poll(&mut self, poll: &mut Poll, store: &mut T::Store) -> Option<T::Owned> {
+ /// Advances the timer up to the instant represented by `now`.
+ pub(crate) fn poll(&mut self, now: u64, store: &mut T::Store) -> Option<T::Owned> {
loop {
- if poll.expiration.is_none() {
- poll.expiration = self.next_expiration().and_then(|expiration| {
- if expiration.deadline > poll.now {
- None
- } else {
- Some(expiration)
- }
- });
- }
+ let expiration = self.next_expiration().and_then(|expiration| {
+ if expiration.deadline > now {
+ None
+ } else {
+ Some(expiration)
+ }
+ });
- match poll.expiration {
+ match expiration {
Some(ref expiration) => {
if let Some(item) = self.poll_expiration(expiration, store) {
return Some(item);
@@ -157,12 +149,14 @@ where
self.set_elapsed(expiration.deadline);
}
None => {
- self.set_elapsed(poll.now);
+ // in this case the poll did not indicate an expiration
+ // _and_ we were not able to find a next expiration in
+ // the current list of timers. advance to the poll's
+ // current time and do nothing else.
+ self.set_elapsed(now);
return None;
}
}
-
- poll.expiration = None;
}
}
@@ -197,6 +191,10 @@ where
res
}
+ /// iteratively find entries that are between the wheel's current
+ /// time and the expiration time. for each in that population either
+ /// return it for notification (in the case of the last level) or tier
+ /// it down to the next level (in all other cases).
pub(crate) fn poll_expiration(
&mut self,
expiration: &Expiration,
@@ -251,15 +249,6 @@ fn level_for(elapsed: u64, when: u64) -> usize {
significant / 6
}
-impl Poll {
- pub(crate) fn new(now: u64) -> Poll {
- Poll {
- now,
- expiration: None,
- }
- }
-}
-
#[cfg(all(test, not(loom)))]
mod test {
use super::*;