tokio/runtime/time/wheel/
level.rs1use crate::runtime::time::{EntryList, TimerHandle, TimerShared};
2
3use std::{array, fmt, ptr::NonNull};
4
5pub(crate) struct Level {
7 level: usize,
8
9 occupied: u64,
17
18 slot: [EntryList; LEVEL_MULT],
20}
21
22#[derive(Debug)]
24pub(crate) struct Expiration {
25 pub(crate) level: usize,
27
28 pub(crate) slot: usize,
30
31 pub(crate) deadline: u64,
33}
34
35const LEVEL_MULT: usize = 64;
39
40impl Level {
41 pub(crate) fn new(level: usize) -> Level {
42 Level {
43 level,
44 occupied: 0,
45 slot: array::from_fn(|_| EntryList::default()),
46 }
47 }
48
49 pub(crate) fn next_expiration(&self, now: u64) -> Option<Expiration> {
52 let slot = self.next_occupied_slot(now)?;
55
56 let level_range = level_range(self.level);
60 let slot_range = slot_range(self.level);
61
62 let level_start = now & !(level_range - 1);
65 let mut deadline = level_start + slot as u64 * slot_range;
66
67 if deadline <= now {
68 debug_assert_eq!(self.level, super::NUM_LEVELS - 1);
85
86 deadline += level_range;
87 }
88
89 debug_assert!(
90 deadline >= now,
91 "deadline={:016X}; now={:016X}; level={}; lr={:016X}, sr={:016X}, slot={}; occupied={:b}",
92 deadline,
93 now,
94 self.level,
95 level_range,
96 slot_range,
97 slot,
98 self.occupied
99 );
100
101 Some(Expiration {
102 level: self.level,
103 slot,
104 deadline,
105 })
106 }
107
108 fn next_occupied_slot(&self, now: u64) -> Option<usize> {
109 if self.occupied == 0 {
110 return None;
111 }
112
113 let now_slot = (now / slot_range(self.level)) as usize;
115 let occupied = self.occupied.rotate_right(now_slot as u32);
116 let zeros = occupied.trailing_zeros() as usize;
117 let slot = (zeros + now_slot) % LEVEL_MULT;
118
119 Some(slot)
120 }
121
122 pub(crate) unsafe fn add_entry(&mut self, item: TimerHandle) {
123 let slot = slot_for(item.cached_when(), self.level);
124
125 self.slot[slot].push_front(item);
126
127 self.occupied |= occupied_bit(slot);
128 }
129
130 pub(crate) unsafe fn remove_entry(&mut self, item: NonNull<TimerShared>) {
131 let slot = slot_for(unsafe { item.as_ref().cached_when() }, self.level);
132
133 unsafe { self.slot[slot].remove(item) };
134 if self.slot[slot].is_empty() {
135 debug_assert!(self.occupied & occupied_bit(slot) != 0);
137
138 self.occupied ^= occupied_bit(slot);
140 }
141 }
142
143 pub(crate) fn take_slot(&mut self, slot: usize) -> EntryList {
144 self.occupied &= !occupied_bit(slot);
145
146 std::mem::take(&mut self.slot[slot])
147 }
148}
149
150impl fmt::Debug for Level {
151 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
152 fmt.debug_struct("Level")
153 .field("occupied", &self.occupied)
154 .finish()
155 }
156}
157
158fn occupied_bit(slot: usize) -> u64 {
159 1 << slot
160}
161
162fn slot_range(level: usize) -> u64 {
163 LEVEL_MULT.pow(level as u32) as u64
164}
165
166fn level_range(level: usize) -> u64 {
167 LEVEL_MULT as u64 * slot_range(level)
168}
169
170fn slot_for(duration: u64, level: usize) -> usize {
172 ((duration >> (level * 6)) % LEVEL_MULT as u64) as usize
173}
174
175#[cfg(all(test, not(loom)))]
176mod test {
177 use super::*;
178
179 #[test]
180 fn test_slot_for() {
181 for pos in 0..64 {
182 assert_eq!(pos as usize, slot_for(pos, 0));
183 }
184
185 for level in 1..5 {
186 for pos in level..64 {
187 let a = pos * 64_usize.pow(level as u32);
188 assert_eq!(pos, slot_for(a as u64, level));
189 }
190 }
191 }
192}