tokio/runtime/time/mod.rs
1// Currently, rust warns when an unsafe fn contains an unsafe {} block. However,
2// in the future, this will change to the reverse. For now, suppress this
3// warning and generally stick with being explicit about unsafety.
4#![allow(unused_unsafe)]
5#![cfg_attr(not(feature = "rt"), allow(dead_code))]
6
7//! Time driver.
8
9mod entry;
10pub(crate) use entry::TimerEntry;
11use entry::{EntryList, TimerHandle, TimerShared, MAX_SAFE_MILLIS_DURATION};
12
13mod handle;
14pub(crate) use self::handle::Handle;
15use self::wheel::Wheel;
16
17mod source;
18pub(crate) use source::TimeSource;
19
20mod wheel;
21
22use crate::loom::sync::atomic::{AtomicBool, Ordering};
23use crate::loom::sync::Mutex;
24use crate::runtime::driver::{self, IoHandle, IoStack};
25use crate::time::error::Error;
26use crate::time::{Clock, Duration};
27use crate::util::WakeList;
28
29use crate::loom::sync::atomic::AtomicU64;
30use std::fmt;
31use std::{num::NonZeroU64, ptr::NonNull};
32
33struct AtomicOptionNonZeroU64(AtomicU64);
34
35// A helper type to store the `next_wake`.
36impl AtomicOptionNonZeroU64 {
37 fn new(val: Option<NonZeroU64>) -> Self {
38 Self(AtomicU64::new(val.map_or(0, NonZeroU64::get)))
39 }
40
41 fn store(&self, val: Option<NonZeroU64>) {
42 self.0
43 .store(val.map_or(0, NonZeroU64::get), Ordering::Relaxed);
44 }
45
46 fn load(&self) -> Option<NonZeroU64> {
47 NonZeroU64::new(self.0.load(Ordering::Relaxed))
48 }
49}
50
51/// Time implementation that drives [`Sleep`][sleep], [`Interval`][interval], and [`Timeout`][timeout].
52///
53/// A `Driver` instance tracks the state necessary for managing time and
54/// notifying the [`Sleep`][sleep] instances once their deadlines are reached.
55///
56/// It is expected that a single instance manages many individual [`Sleep`][sleep]
57/// instances. The `Driver` implementation is thread-safe and, as such, is able
58/// to handle callers from across threads.
59///
60/// After creating the `Driver` instance, the caller must repeatedly call `park`
61/// or `park_timeout`. The time driver will perform no work unless `park` or
62/// `park_timeout` is called repeatedly.
63///
64/// The driver has a resolution of one millisecond. Any unit of time that falls
65/// between milliseconds are rounded up to the next millisecond.
66///
67/// When an instance is dropped, any outstanding [`Sleep`][sleep] instance that has not
68/// elapsed will be notified with an error. At this point, calling `poll` on the
69/// [`Sleep`][sleep] instance will result in panic.
70///
71/// # Implementation
72///
73/// The time driver is based on the [paper by Varghese and Lauck][paper].
74///
75/// A hashed timing wheel is a vector of slots, where each slot handles a time
76/// slice. As time progresses, the timer walks over the slot for the current
77/// instant, and processes each entry for that slot. When the timer reaches the
78/// end of the wheel, it starts again at the beginning.
79///
80/// The implementation maintains six wheels arranged in a set of levels. As the
81/// levels go up, the slots of the associated wheel represent larger intervals
82/// of time. At each level, the wheel has 64 slots. Each slot covers a range of
83/// time equal to the wheel at the lower level. At level zero, each slot
84/// represents one millisecond of time.
85///
86/// The wheels are:
87///
88/// * Level 0: 64 x 1 millisecond slots.
89/// * Level 1: 64 x 64 millisecond slots.
90/// * Level 2: 64 x ~4 second slots.
91/// * Level 3: 64 x ~4 minute slots.
92/// * Level 4: 64 x ~4 hour slots.
93/// * Level 5: 64 x ~12 day slots.
94///
95/// When the timer processes entries at level zero, it will notify all the
96/// `Sleep` instances as their deadlines have been reached. For all higher
97/// levels, all entries will be redistributed across the wheel at the next level
98/// down. Eventually, as time progresses, entries with [`Sleep`][sleep] instances will
99/// either be canceled (dropped) or their associated entries will reach level
100/// zero and be notified.
101///
102/// [paper]: http://www.cs.columbia.edu/~nahum/w6998/papers/ton97-timing-wheels.pdf
103/// [sleep]: crate::time::Sleep
104/// [timeout]: crate::time::Timeout
105/// [interval]: crate::time::Interval
106#[derive(Debug)]
107pub(crate) struct Driver {
108 /// Parker to delegate to.
109 park: IoStack,
110}
111
112/// Timer state shared between `Driver`, `Handle`, and `Registration`.
113struct Inner {
114 /// The earliest time at which we promise to wake up without unparking.
115 next_wake: AtomicOptionNonZeroU64,
116
117 /// Sharded Timer wheels.
118 wheels: Box<[Mutex<wheel::Wheel>]>,
119
120 /// True if the driver is being shutdown.
121 pub(super) is_shutdown: AtomicBool,
122
123 // When `true`, a call to `park_timeout` should immediately return and time
124 // should not advance. One reason for this to be `true` is if the task
125 // passed to `Runtime::block_on` called `task::yield_now()`.
126 //
127 // While it may look racy, it only has any effect when the clock is paused
128 // and pausing the clock is restricted to a single-threaded runtime.
129 #[cfg(feature = "test-util")]
130 did_wake: AtomicBool,
131}
132
133// ===== impl Driver =====
134
135impl Driver {
136 /// Creates a new `Driver` instance that uses `park` to block the current
137 /// thread and `time_source` to get the current time and convert to ticks.
138 ///
139 /// Specifying the source of time is useful when testing.
140 pub(crate) fn new(park: IoStack, clock: &Clock, shards: u32) -> (Driver, Handle) {
141 assert!(shards > 0);
142
143 let time_source = TimeSource::new(clock);
144 let wheels: Vec<_> = (0..shards)
145 .map(|_| Mutex::new(wheel::Wheel::new()))
146 .collect();
147
148 let handle = Handle {
149 time_source,
150 inner: Inner {
151 next_wake: AtomicOptionNonZeroU64::new(None),
152 wheels: wheels.into_boxed_slice(),
153 is_shutdown: AtomicBool::new(false),
154 #[cfg(feature = "test-util")]
155 did_wake: AtomicBool::new(false),
156 },
157 };
158
159 let driver = Driver { park };
160
161 (driver, handle)
162 }
163
164 pub(crate) fn park(&mut self, handle: &driver::Handle) {
165 self.park_internal(handle, None);
166 }
167
168 pub(crate) fn park_timeout(&mut self, handle: &driver::Handle, duration: Duration) {
169 self.park_internal(handle, Some(duration));
170 }
171
172 pub(crate) fn shutdown(&mut self, rt_handle: &driver::Handle) {
173 let handle = rt_handle.time();
174
175 if handle.is_shutdown() {
176 return;
177 }
178
179 handle.inner.is_shutdown.store(true, Ordering::SeqCst);
180
181 // Advance time forward to the end of time.
182
183 handle.process_at_time(0, u64::MAX);
184
185 self.park.shutdown(rt_handle);
186 }
187
188 fn park_internal(&mut self, rt_handle: &driver::Handle, limit: Option<Duration>) {
189 let handle = rt_handle.time();
190 assert!(!handle.is_shutdown());
191
192 // Finds out the min expiration time to park.
193 let locks = (0..rt_handle.time().inner.get_shard_size())
194 .map(|id| rt_handle.time().inner.lock_sharded_wheel(id))
195 .collect::<Vec<_>>();
196
197 let expiration_time = locks
198 .iter()
199 .filter_map(|lock| lock.next_expiration_time())
200 .min();
201
202 rt_handle
203 .time()
204 .inner
205 .next_wake
206 .store(next_wake_time(expiration_time));
207
208 // Safety: After updating the `next_wake`, we drop all the locks.
209 drop(locks);
210
211 match expiration_time {
212 Some(when) => {
213 let now = handle.time_source.now(rt_handle.clock());
214 // Note that we effectively round up to 1ms here - this avoids
215 // very short-duration microsecond-resolution sleeps that the OS
216 // might treat as zero-length.
217 let mut duration = handle
218 .time_source
219 .tick_to_duration(when.saturating_sub(now));
220
221 if duration > Duration::from_millis(0) {
222 if let Some(limit) = limit {
223 duration = std::cmp::min(limit, duration);
224 }
225
226 self.park_thread_timeout(rt_handle, duration);
227 } else {
228 self.park.park_timeout(rt_handle, Duration::from_secs(0));
229 }
230 }
231 None => {
232 if let Some(duration) = limit {
233 self.park_thread_timeout(rt_handle, duration);
234 } else {
235 self.park.park(rt_handle);
236 }
237 }
238 }
239
240 // Process pending timers after waking up
241 handle.process(rt_handle.clock());
242 }
243
244 cfg_test_util! {
245 fn park_thread_timeout(&mut self, rt_handle: &driver::Handle, duration: Duration) {
246 let handle = rt_handle.time();
247 let clock = rt_handle.clock();
248
249 if clock.can_auto_advance() {
250 self.park.park_timeout(rt_handle, Duration::from_secs(0));
251
252 // If the time driver was woken, then the park completed
253 // before the "duration" elapsed (usually caused by a
254 // yield in `Runtime::block_on`). In this case, we don't
255 // advance the clock.
256 if !handle.did_wake() {
257 // Simulate advancing time
258 if let Err(msg) = clock.advance(duration) {
259 panic!("{}", msg);
260 }
261 }
262 } else {
263 self.park.park_timeout(rt_handle, duration);
264 }
265 }
266 }
267
268 cfg_not_test_util! {
269 fn park_thread_timeout(&mut self, rt_handle: &driver::Handle, duration: Duration) {
270 self.park.park_timeout(rt_handle, duration);
271 }
272 }
273}
274
275// Helper function to turn expiration_time into next_wake_time.
276// Since the `park_timeout` will round up to 1ms for avoiding very
277// short-duration microsecond-resolution sleeps, we do the same here.
278// The conversion is as follows
279// None => None
280// Some(0) => Some(1)
281// Some(i) => Some(i)
282fn next_wake_time(expiration_time: Option<u64>) -> Option<NonZeroU64> {
283 expiration_time.and_then(|v| {
284 if v == 0 {
285 NonZeroU64::new(1)
286 } else {
287 NonZeroU64::new(v)
288 }
289 })
290}
291
292impl Handle {
293 /// Runs timer related logic, and returns the next wakeup time
294 pub(self) fn process(&self, clock: &Clock) {
295 let now = self.time_source().now(clock);
296 // For fairness, randomly select one to start.
297 let shards = self.inner.get_shard_size();
298 let start = crate::runtime::context::thread_rng_n(shards);
299 self.process_at_time(start, now);
300 }
301
302 pub(self) fn process_at_time(&self, start: u32, now: u64) {
303 let shards = self.inner.get_shard_size();
304
305 let expiration_time = (start..shards + start)
306 .filter_map(|i| self.process_at_sharded_time(i, now))
307 .min();
308
309 self.inner.next_wake.store(next_wake_time(expiration_time));
310 }
311
312 // Returns the next wakeup time of this shard.
313 pub(self) fn process_at_sharded_time(&self, id: u32, mut now: u64) -> Option<u64> {
314 let mut waker_list = WakeList::new();
315 let mut lock = self.inner.lock_sharded_wheel(id);
316
317 if now < lock.elapsed() {
318 // Time went backwards! This normally shouldn't happen as the Rust language
319 // guarantees that an Instant is monotonic, but can happen when running
320 // Linux in a VM on a Windows host due to std incorrectly trusting the
321 // hardware clock to be monotonic.
322 //
323 // See <https://github.com/tokio-rs/tokio/issues/3619> for more information.
324 now = lock.elapsed();
325 }
326
327 while let Some(entry) = lock.poll(now) {
328 debug_assert!(unsafe { entry.is_pending() });
329
330 // SAFETY: We hold the driver lock, and just removed the entry from any linked lists.
331 if let Some(waker) = unsafe { entry.fire(Ok(())) } {
332 waker_list.push(waker);
333
334 if !waker_list.can_push() {
335 // Wake a batch of wakers. To avoid deadlock, we must do this with the lock temporarily dropped.
336 drop(lock);
337
338 waker_list.wake_all();
339
340 lock = self.inner.lock_sharded_wheel(id);
341 }
342 }
343 }
344 let next_wake_up = lock.poll_at();
345 drop(lock);
346
347 waker_list.wake_all();
348 next_wake_up
349 }
350
351 /// Removes a registered timer from the driver.
352 ///
353 /// The timer will be moved to the cancelled state. Wakers will _not_ be
354 /// invoked. If the timer is already completed, this function is a no-op.
355 ///
356 /// This function always acquires the driver lock, even if the entry does
357 /// not appear to be registered.
358 ///
359 /// SAFETY: The timer must not be registered with some other driver, and
360 /// `add_entry` must not be called concurrently.
361 pub(self) unsafe fn clear_entry(&self, entry: NonNull<TimerShared>) {
362 unsafe {
363 let mut lock = self.inner.lock_sharded_wheel(entry.as_ref().shard_id());
364
365 if entry.as_ref().might_be_registered() {
366 lock.remove(entry);
367 }
368
369 entry.as_ref().handle().fire(Ok(()));
370 }
371 }
372
373 /// Removes and re-adds an entry to the driver.
374 ///
375 /// SAFETY: The timer must be either unregistered, or registered with this
376 /// driver. No other threads are allowed to concurrently manipulate the
377 /// timer at all (the current thread should hold an exclusive reference to
378 /// the `TimerEntry`)
379 pub(self) unsafe fn reregister(
380 &self,
381 unpark: &IoHandle,
382 new_tick: u64,
383 entry: NonNull<TimerShared>,
384 ) {
385 let waker = unsafe {
386 let mut lock = self.inner.lock_sharded_wheel(entry.as_ref().shard_id());
387
388 // We may have raced with a firing/deregistration, so check before
389 // deregistering.
390 if unsafe { entry.as_ref().might_be_registered() } {
391 lock.remove(entry);
392 }
393
394 // Now that we have exclusive control of this entry, mint a handle to reinsert it.
395 let entry = entry.as_ref().handle();
396
397 if self.is_shutdown() {
398 unsafe { entry.fire(Err(crate::time::error::Error::shutdown())) }
399 } else {
400 entry.set_expiration(new_tick);
401
402 // Note: We don't have to worry about racing with some other resetting
403 // thread, because add_entry and reregister require exclusive control of
404 // the timer entry.
405 match unsafe { lock.insert(entry) } {
406 Ok(when) => {
407 if self
408 .inner
409 .next_wake
410 .load()
411 .map(|next_wake| when < next_wake.get())
412 .unwrap_or(true)
413 {
414 unpark.unpark();
415 }
416
417 None
418 }
419 Err((entry, crate::time::error::InsertError::Elapsed)) => unsafe {
420 entry.fire(Ok(()))
421 },
422 }
423 }
424
425 // Must release lock before invoking waker to avoid the risk of deadlock.
426 };
427
428 // The timer was fired synchronously as a result of the reregistration.
429 // Wake the waker; this is needed because we might reset _after_ a poll,
430 // and otherwise the task won't be awoken to poll again.
431 if let Some(waker) = waker {
432 waker.wake();
433 }
434 }
435
436 cfg_test_util! {
437 fn did_wake(&self) -> bool {
438 self.inner.did_wake.swap(false, Ordering::SeqCst)
439 }
440 }
441}
442
443// ===== impl Inner =====
444
445impl Inner {
446 /// Locks the driver's sharded wheel structure.
447 pub(super) fn lock_sharded_wheel(
448 &self,
449 shard_id: u32,
450 ) -> crate::loom::sync::MutexGuard<'_, Wheel> {
451 let index = shard_id % (self.wheels.len() as u32);
452 // Safety: This modulo operation ensures that the index is not out of bounds.
453 unsafe { self.wheels.get_unchecked(index as usize).lock() }
454 }
455
456 // Check whether the driver has been shutdown
457 pub(super) fn is_shutdown(&self) -> bool {
458 self.is_shutdown.load(Ordering::SeqCst)
459 }
460
461 // Gets the number of shards.
462 fn get_shard_size(&self) -> u32 {
463 self.wheels.len() as u32
464 }
465}
466
467impl fmt::Debug for Inner {
468 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
469 fmt.debug_struct("Inner").finish()
470 }
471}
472
473#[cfg(test)]
474mod tests;