1use crate::time::wheel::Stack;
23use std::fmt;
45/// Wheel for a single level in the timer. This wheel contains 64 slots.
6pub(crate) struct Level<T> {
7 level: usize,
89/// Bit field tracking which slots currently contain entries.
10 ///
11 /// Using a bit field to track slots that contain entries allows avoiding a
12 /// scan to find entries. This field is updated when entries are added or
13 /// removed from a slot.
14 ///
15 /// The least-significant bit represents slot zero.
16occupied: u64,
1718/// Slots
19slot: [T; LEVEL_MULT],
20}
2122/// Indicates when a slot must be processed next.
23#[derive(Debug)]
24pub(crate) struct Expiration {
25/// The level containing the slot.
26pub(crate) level: usize,
2728/// The slot index.
29pub(crate) slot: usize,
3031/// The instant at which the slot needs to be processed.
32pub(crate) deadline: u64,
33}
3435/// Level multiplier.
36///
37/// Being a power of 2 is very important.
38const LEVEL_MULT: usize = 64;
3940impl<T: Stack> Level<T> {
41pub(crate) fn new(level: usize) -> Level<T> {
42 Level {
43 level,
44 occupied: 0,
45 slot: std::array::from_fn(|_| T::default()),
46 }
47 }
4849/// Finds the slot that needs to be processed next and returns the slot and
50 /// `Instant` at which this slot must be processed.
51pub(crate) fn next_expiration(&self, now: u64) -> Option<Expiration> {
52// Use the `occupied` bit field to get the index of the next slot that
53 // needs to be processed.
54let slot = match self.next_occupied_slot(now) {
55Some(slot) => slot,
56None => return None,
57 };
5859// From the slot index, calculate the `Instant` at which it needs to be
60 // processed. This value *must* be in the future with respect to `now`.
6162let level_range = level_range(self.level);
63let slot_range = slot_range(self.level);
6465// TODO: This can probably be simplified w/ power of 2 math
66let level_start = now - (now % level_range);
67let mut deadline = level_start + slot as u64 * slot_range;
68if deadline < now {
69// A timer is in a slot "prior" to the current time. This can occur
70 // because we do not have an infinite hierarchy of timer levels, and
71 // eventually a timer scheduled for a very distant time might end up
72 // being placed in a slot that is beyond the end of all of the
73 // arrays.
74 //
75 // To deal with this, we first limit timers to being scheduled no
76 // more than MAX_DURATION ticks in the future; that is, they're at
77 // most one rotation of the top level away. Then, we force timers
78 // that logically would go into the top+1 level, to instead go into
79 // the top level's slots.
80 //
81 // What this means is that the top level's slots act as a
82 // pseudo-ring buffer, and we rotate around them indefinitely. If we
83 // compute a deadline before now, and it's the top level, it
84 // therefore means we're actually looking at a slot in the future.
85debug_assert_eq!(self.level, super::NUM_LEVELS - 1);
8687 deadline += level_range;
88 }
89debug_assert!(
90 deadline >= now,
91"deadline={:016X}; now={:016X}; level={}; slot={}; occupied={:b}",
92 deadline,
93 now,
94self.level,
95 slot,
96self.occupied
97 );
9899Some(Expiration {
100 level: self.level,
101 slot,
102 deadline,
103 })
104 }
105106fn next_occupied_slot(&self, now: u64) -> Option<usize> {
107if self.occupied == 0 {
108return None;
109 }
110111// Get the slot for now using Maths
112let now_slot = (now / slot_range(self.level)) as usize;
113let occupied = self.occupied.rotate_right(now_slot as u32);
114let zeros = occupied.trailing_zeros() as usize;
115let slot = (zeros + now_slot) % 64;
116117Some(slot)
118 }
119120pub(crate) fn add_entry(&mut self, when: u64, item: T::Owned, store: &mut T::Store) {
121let slot = slot_for(when, self.level);
122123self.slot[slot].push(item, store);
124self.occupied |= occupied_bit(slot);
125 }
126127pub(crate) fn remove_entry(&mut self, when: u64, item: &T::Borrowed, store: &mut T::Store) {
128let slot = slot_for(when, self.level);
129130self.slot[slot].remove(item, store);
131132if self.slot[slot].is_empty() {
133// The bit is currently set
134debug_assert!(self.occupied & occupied_bit(slot) != 0);
135136// Unset the bit
137self.occupied ^= occupied_bit(slot);
138 }
139 }
140141pub(crate) fn pop_entry_slot(&mut self, slot: usize, store: &mut T::Store) -> Option<T::Owned> {
142let ret = self.slot[slot].pop(store);
143144if ret.is_some() && self.slot[slot].is_empty() {
145// The bit is currently set
146debug_assert!(self.occupied & occupied_bit(slot) != 0);
147148self.occupied ^= occupied_bit(slot);
149 }
150151 ret
152 }
153154pub(crate) fn peek_entry_slot(&self, slot: usize) -> Option<T::Owned> {
155self.slot[slot].peek()
156 }
157}
158159impl<T> fmt::Debug for Level<T> {
160fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
161 fmt.debug_struct("Level")
162 .field("occupied", &self.occupied)
163 .finish()
164 }
165}
166167fn occupied_bit(slot: usize) -> u64 {
1681 << slot
169}
170171fn slot_range(level: usize) -> u64 {
172 LEVEL_MULT.pow(level as u32) as u64
173}
174175fn level_range(level: usize) -> u64 {
176 LEVEL_MULT as u64 * slot_range(level)
177}
178179/// Convert a duration (milliseconds) and a level to a slot position
180fn slot_for(duration: u64, level: usize) -> usize {
181 ((duration >> (level * 6)) % LEVEL_MULT as u64) as usize
182}
183184#[cfg(all(test, not(loom)))]
185mod test {
186use super::*;
187188#[test]
189fn test_slot_for() {
190for pos in 0..64 {
191assert_eq!(pos as usize, slot_for(pos, 0));
192 }
193194for level in 1..5 {
195for pos in level..64 {
196let a = pos * 64_usize.pow(level as u32);
197assert_eq!(pos, slot_for(a as u64, level));
198 }
199 }
200 }
201}