tokio/runtime/time/wheel/
mod.rs

1use crate::runtime::time::{TimerHandle, TimerShared};
2use crate::time::error::InsertError;
3
4mod level;
5pub(crate) use self::level::Expiration;
6use self::level::Level;
7
8use std::{array, ptr::NonNull};
9
10use super::entry::STATE_DEREGISTERED;
11use super::EntryList;
12
13/// Timing wheel implementation.
14///
15/// This type provides the hashed timing wheel implementation that backs `Timer`
16/// and `DelayQueue`.
17///
18/// The structure is generic over `T: Stack`. This allows handling timeout data
19/// being stored on the heap or in a slab. In order to support the latter case,
20/// the slab must be passed into each function allowing the implementation to
21/// lookup timer entries.
22///
23/// See `Timer` documentation for some implementation notes.
24#[derive(Debug)]
25pub(crate) struct Wheel {
26    /// The number of milliseconds elapsed since the wheel started.
27    elapsed: u64,
28
29    /// Timer wheel.
30    ///
31    /// Levels:
32    ///
33    /// * 1 ms slots / 64 ms range
34    /// * 64 ms slots / ~ 4 sec range
35    /// * ~ 4 sec slots / ~ 4 min range
36    /// * ~ 4 min slots / ~ 4 hr range
37    /// * ~ 4 hr slots / ~ 12 day range
38    /// * ~ 12 day slots / ~ 2 yr range
39    levels: Box<[Level; NUM_LEVELS]>,
40
41    /// Entries queued for firing
42    pending: EntryList,
43}
44
45/// Number of levels. Each level has 64 slots. By using 6 levels with 64 slots
46/// each, the timer is able to track time up to 2 years into the future with a
47/// precision of 1 millisecond.
48const NUM_LEVELS: usize = 6;
49
50/// The maximum duration of a `Sleep`.
51pub(super) const MAX_DURATION: u64 = (1 << (6 * NUM_LEVELS)) - 1;
52
53impl Wheel {
54    /// Creates a new timing wheel.
55    pub(crate) fn new() -> Wheel {
56        Wheel {
57            elapsed: 0,
58            levels: Box::new(array::from_fn(Level::new)),
59            pending: EntryList::new(),
60        }
61    }
62
63    /// Returns the number of milliseconds that have elapsed since the timing
64    /// wheel's creation.
65    pub(crate) fn elapsed(&self) -> u64 {
66        self.elapsed
67    }
68
69    /// Inserts an entry into the timing wheel.
70    ///
71    /// # Arguments
72    ///
73    /// * `item`: The item to insert into the wheel.
74    ///
75    /// # Return
76    ///
77    /// Returns `Ok` when the item is successfully inserted, `Err` otherwise.
78    ///
79    /// `Err(Elapsed)` indicates that `when` represents an instant that has
80    /// already passed. In this case, the caller should fire the timeout
81    /// immediately.
82    ///
83    /// `Err(Invalid)` indicates an invalid `when` argument as been supplied.
84    ///
85    /// # Safety
86    ///
87    /// This function registers item into an intrusive linked list. The caller
88    /// must ensure that `item` is pinned and will not be dropped without first
89    /// being deregistered.
90    pub(crate) unsafe fn insert(
91        &mut self,
92        item: TimerHandle,
93    ) -> Result<u64, (TimerHandle, InsertError)> {
94        let when = item.sync_when();
95
96        if when <= self.elapsed {
97            return Err((item, InsertError::Elapsed));
98        }
99
100        // Get the level at which the entry should be stored
101        let level = self.level_for(when);
102
103        unsafe {
104            self.levels[level].add_entry(item);
105        }
106
107        debug_assert!({
108            self.levels[level]
109                .next_expiration(self.elapsed)
110                .map(|e| e.deadline >= self.elapsed)
111                .unwrap_or(true)
112        });
113
114        Ok(when)
115    }
116
117    /// Removes `item` from the timing wheel.
118    pub(crate) unsafe fn remove(&mut self, item: NonNull<TimerShared>) {
119        unsafe {
120            let when = item.as_ref().registered_when();
121            if when == STATE_DEREGISTERED {
122                self.pending.remove(item);
123            } else {
124                debug_assert!(
125                    self.elapsed <= when,
126                    "elapsed={}; when={}",
127                    self.elapsed,
128                    when
129                );
130
131                let level = self.level_for(when);
132                self.levels[level].remove_entry(item);
133            }
134        }
135    }
136
137    /// Instant at which to poll.
138    pub(crate) fn poll_at(&self) -> Option<u64> {
139        self.next_expiration().map(|expiration| expiration.deadline)
140    }
141
142    /// Advances the timer up to the instant represented by `now`.
143    pub(crate) fn poll(&mut self, now: u64) -> Option<TimerHandle> {
144        loop {
145            if let Some(handle) = self.pending.pop_back() {
146                return Some(handle);
147            }
148
149            match self.next_expiration() {
150                Some(ref expiration) if expiration.deadline <= now => {
151                    self.process_expiration(expiration);
152
153                    self.set_elapsed(expiration.deadline);
154                }
155                _ => {
156                    // in this case the poll did not indicate an expiration
157                    // _and_ we were not able to find a next expiration in
158                    // the current list of timers.  advance to the poll's
159                    // current time and do nothing else.
160                    self.set_elapsed(now);
161                    break;
162                }
163            }
164        }
165
166        self.pending.pop_back()
167    }
168
169    /// Returns the instant at which the next timeout expires.
170    fn next_expiration(&self) -> Option<Expiration> {
171        if !self.pending.is_empty() {
172            // Expire immediately as we have things pending firing
173            return Some(Expiration {
174                level: 0,
175                slot: 0,
176                deadline: self.elapsed,
177            });
178        }
179
180        // Check all levels
181        for (level_num, level) in self.levels.iter().enumerate() {
182            if let Some(expiration) = level.next_expiration(self.elapsed) {
183                // There cannot be any expirations at a higher level that happen
184                // before this one.
185                debug_assert!(self.no_expirations_before(level_num + 1, expiration.deadline));
186
187                return Some(expiration);
188            }
189        }
190
191        None
192    }
193
194    /// Returns the tick at which this timer wheel next needs to perform some
195    /// processing, or None if there are no timers registered.
196    pub(super) fn next_expiration_time(&self) -> Option<u64> {
197        self.next_expiration().map(|ex| ex.deadline)
198    }
199
200    /// Used for debug assertions
201    fn no_expirations_before(&self, start_level: usize, before: u64) -> bool {
202        let mut res = true;
203
204        for level in &self.levels[start_level..] {
205            if let Some(e2) = level.next_expiration(self.elapsed) {
206                if e2.deadline < before {
207                    res = false;
208                }
209            }
210        }
211
212        res
213    }
214
215    /// iteratively find entries that are between the wheel's current
216    /// time and the expiration time.  for each in that population either
217    /// queue it for notification (in the case of the last level) or tier
218    /// it down to the next level (in all other cases).
219    pub(crate) fn process_expiration(&mut self, expiration: &Expiration) {
220        // Note that we need to take _all_ of the entries off the list before
221        // processing any of them. This is important because it's possible that
222        // those entries might need to be reinserted into the same slot.
223        //
224        // This happens only on the highest level, when an entry is inserted
225        // more than MAX_DURATION into the future. When this happens, we wrap
226        // around, and process some entries a multiple of MAX_DURATION before
227        // they actually need to be dropped down a level. We then reinsert them
228        // back into the same position; we must make sure we don't then process
229        // those entries again or we'll end up in an infinite loop.
230        let mut entries = self.take_entries(expiration);
231
232        while let Some(item) = entries.pop_back() {
233            if expiration.level == 0 {
234                debug_assert_eq!(unsafe { item.registered_when() }, expiration.deadline);
235            }
236
237            // Try to expire the entry; this is cheap (doesn't synchronize) if
238            // the timer is not expired, and updates registered_when.
239            match unsafe { item.mark_pending(expiration.deadline) } {
240                Ok(()) => {
241                    // Item was expired
242                    self.pending.push_front(item);
243                }
244                Err(expiration_tick) => {
245                    let level = level_for(expiration.deadline, expiration_tick);
246                    unsafe {
247                        self.levels[level].add_entry(item);
248                    }
249                }
250            }
251        }
252    }
253
254    fn set_elapsed(&mut self, when: u64) {
255        assert!(
256            self.elapsed <= when,
257            "elapsed={:?}; when={:?}",
258            self.elapsed,
259            when
260        );
261
262        if when > self.elapsed {
263            self.elapsed = when;
264        }
265    }
266
267    /// Obtains the list of entries that need processing for the given expiration.
268    fn take_entries(&mut self, expiration: &Expiration) -> EntryList {
269        self.levels[expiration.level].take_slot(expiration.slot)
270    }
271
272    fn level_for(&self, when: u64) -> usize {
273        level_for(self.elapsed, when)
274    }
275}
276
277fn level_for(elapsed: u64, when: u64) -> usize {
278    const SLOT_MASK: u64 = (1 << 6) - 1;
279
280    // Mask in the trailing bits ignored by the level calculation in order to cap
281    // the possible leading zeros
282    let mut masked = elapsed ^ when | SLOT_MASK;
283
284    if masked >= MAX_DURATION {
285        // Fudge the timer into the top level
286        masked = MAX_DURATION - 1;
287    }
288
289    let leading_zeros = masked.leading_zeros() as usize;
290    let significant = 63 - leading_zeros;
291
292    significant / NUM_LEVELS
293}
294
295#[cfg(all(test, not(loom)))]
296mod test {
297    use super::*;
298
299    #[test]
300    fn test_level_for() {
301        for pos in 0..64 {
302            assert_eq!(0, level_for(0, pos), "level_for({pos}) -- binary = {pos:b}");
303        }
304
305        for level in 1..5 {
306            for pos in level..64 {
307                let a = pos * 64_usize.pow(level as u32);
308                assert_eq!(
309                    level,
310                    level_for(0, a as u64),
311                    "level_for({a}) -- binary = {a:b}"
312                );
313
314                if pos > level {
315                    let a = a - 1;
316                    assert_eq!(
317                        level,
318                        level_for(0, a as u64),
319                        "level_for({a}) -- binary = {a:b}"
320                    );
321                }
322
323                if pos < 64 {
324                    let a = a + 1;
325                    assert_eq!(
326                        level,
327                        level_for(0, a as u64),
328                        "level_for({a}) -- binary = {a:b}"
329                    );
330                }
331            }
332        }
333    }
334}