Repository: tokio-rs/tokio
File: tokio/src/runtime/time/mod.rs (Rust)

Code before:
    }

    pub(crate) fn shutdown(&mut self, rt_handle: &driver::Handle) {
        let handle = rt_handle.time();

        if handle.is_shutdown() {
            return;
        }

        handle.inner.is_shutdown.store(true, Ordering::SeqCst);

        // Advance time forward to the end of time.

        handle.process_at_time(u64::MAX);

        self.park.shutdown(rt_handle);
    }

    fn park_internal(&mut self, rt_handle: &driver::Handle, limit: Option<Duration>) {
        let handle = rt_handle.time();
        assert!(!handle.is_shutdown());

        // Finds out the min expiration time to park.
        let mut next_wake: Option<u64> = None;
        for id in 0..rt_handle.time().inner.get_shard_size() {
            let lock = unsafe { rt_handle.time().inner.lock_sharded_wheel(id) };

            if let Some(expiration_time) = lock.next_expiration_time() {
                next_wake = Some(match next_wake {
                    Some(t) => t.min(expiration_time),
                    None => expiration_time,
                });
            }
        }

        rt_handle.time().inner.set_next_wake(
            next_wake.map(|t| NonZeroU64::new(t).unwrap_or_else(|| NonZeroU64::new(1).unwrap())),
        );

        match next_wake {
            Some(when) => {
                let now = handle.time_source.now(rt_handle.clock());
                // Note that we effectively round up to 1ms here - this avoids
                // very short-duration microsecond-resolution sleeps that the OS
                // might treat as zero-length.
                let mut duration = handle
                    .time_source
                    .tick_to_duration(when.saturating_sub(now));

                if duration > Duration::from_millis(0) {
                    if let Some(limit) = limit {

Review comment (suggestion):
Sorry, not sure why `unsafe` was used here. But consider this.
```rust
        let next_wake = (0..handle.inner.get_shard_size())
            .filter_map(|id| handle.inner.lock_sharded_wheel(id).next_expiration_time())
            .fold(None::<u64>, |acc, expiration_time| {
                Some(match acc {
                    Some(t) => t.min(expiration_time),
                    None => expiration_time,
                })
            });
```

Code after:
                wheels: wheels.into_boxed_slice(),
                is_shutdown: AtomicBool::new(false),
                #[cfg(feature = "test-util")]
                did_wake: AtomicBool::new(false),
            },
        };

        let driver = Driver { park };

        (driver, handle)
    }

    pub(crate) fn park(&mut self, handle: &driver::Handle) {
        self.park_internal(handle, None);
    }

    pub(crate) fn park_timeout(&mut self, handle: &driver::Handle, duration: Duration) {
        self.park_internal(handle, Some(duration));
    }

    pub(crate) fn shutdown(&mut self, rt_handle: &driver::Handle) {
        let handle = rt_handle.time();

        if handle.is_shutdown() {
            return;
        }

        handle.inner.is_shutdown.store(true, Ordering::SeqCst);

        // Advance time forward to the end of time.

        handle.process_at_time(0, u64::MAX);

        self.park.shutdown(rt_handle);
    }

    fn park_internal(&mut self, rt_handle: &driver::Handle, limit: Option<Duration>) {
        let handle = rt_handle.time();
        assert!(!handle.is_shutdown());

        // Finds out the min expiration time to park.
        let expiration_time = (0..rt_handle.time().inner.get_shard_size())
            .filter_map(|id| {
                let lock = rt_handle.time().inner.lock_sharded_wheel(id);
                lock.next_expiration_time()
            })
            .min();

        rt_handle
            .time()
            .inner