Skip to main content

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