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

Code before:
        let inner = unsafe { &*self.inner.get() };
        if inner.is_none() {
            let shard_id = self.get_shard_id();
            unsafe {
                *self.inner.get() = Some(TimerShared::new(shard_id));
            }
        }
        return inner.as_ref().unwrap();
    }

    // Gets the shard id. If current thread is a worker thread, we use its worker index as a shard id.
    // Otherwise, we use a random number generator to obtain the shard id.
    cfg_rt! {
        fn get_shard_id(&self) -> u32 {
            let shard_size = self.driver.driver().time().inner.get_shard_size();
            let id = context::with_scheduler(|ctx| match ctx {
                Some(scheduler::Context::CurrentThread(_ctx)) => 0,
                #[cfg(feature = "rt-multi-thread")]
                Some(scheduler::Context::MultiThread(ctx)) => ctx.get_worker_index() as u32,
                #[cfg(all(tokio_unstable, feature = "rt-multi-thread"))]
                Some(scheduler::Context::MultiThreadAlt(ctx)) => ctx.get_worker_index() as u32,
                _ => context::thread_rng_n(shard_size),
            });
            id % shard_size
        }
    }

    cfg_not_rt! {
        fn get_shard_id(&self) -> u32 {
            let shard_size = self.driver.driver().time().inner.get_shard_size();
            let id = context::thread_rng_n(shard_size);
            id % shard_size
        }
    }

    pub(crate) fn deadline(&self) -> Instant {
        self.deadline
    }

    pub(crate) fn is_elapsed(&self) -> bool {
        self.is_inner_init() && !self.inner().state.might_be_registered() && self.registered
    }

    /// Cancels and deregisters the timer. This operation is irreversible.
    pub(crate) fn cancel(self: Pin<&mut Self>) {
        // Avoid calling the `clear_entry` method, because it has not been initialized yet.
        if !self.is_inner_init() {
            return;
        }
        // We need to perform an acq/rel fence with the driver thread, and the
        // simplest way to do so is to grab the driver lock.

Review comment (suggestion):
This sounds like a getter for a value that is already created, but actually it is the logic for figuring out which shard to use for a new timer. Can we rename this, and perhaps move it to a stand-alone function instead of a method on `TimerEntry`. Otherwise I am worried that someone will call it and expect it to return the same value as what the `TimerShared` is using.

Code after:
        let inner = unsafe { &*self.inner.get() };
        if inner.is_none() {
            let shard_size = self.driver.driver().time().inner.get_shard_size();
            let shard_id = generate_shard_id(shard_size);
            unsafe {
                *self.inner.get() = Some(TimerShared::new(shard_id));
            }
        }
        return inner.as_ref().unwrap();
    }

    pub(crate) fn deadline(&self) -> Instant {
        self.deadline
    }

    pub(crate) fn is_elapsed(&self) -> bool {
        self.is_inner_init() && !self.inner().state.might_be_registered() && self.registered
    }

    /// Cancels and deregisters the timer. This operation is irreversible.
    pub(crate) fn cancel(self: Pin<&mut Self>) {
        // Avoid calling the `clear_entry` method, because it has not been initialized yet.
        if !self.is_inner_init() {
            return;
        }
        // We need to perform an acq/rel fence with the driver thread, and the
        // simplest way to do so is to grab the driver lock.
        //
        // Why is this necessary? We're about to release this timer's memory for
        // some other non-timer use. However, we've been doing a bunch of
        // relaxed (or even non-atomic) writes from the driver thread, and we'll
        // be doing more from _this thread_ (as this memory is interpreted as
        // something else).
        //
        // It is critical to ensure that, from the point of view of the driver,
        // those future non-timer writes happen-after the timer is fully fired,
        // and from the purpose of this thread, the driver's writes all
        // happen-before we drop the timer. This in turn requires us to perform
        // an acquire-release barrier in _both_ directions between the driver
        // and dropping thread.
        //
        // The lock acquisition in clear_entry serves this purpose. All of the
        // driver manipulations happen with the lock held, so we can just take
        // the lock and be sure that this drop happens-after everything the
        // driver did so far and happens-before everything the driver does in
        // the future. While we have the lock held, we also go ahead and
        // deregister the entry if necessary.
        unsafe { self.driver().clear_entry(NonNull::from(self.inner())) };
    }

    pub(crate) fn reset(mut self: Pin<&mut Self>, new_time: Instant, reregister: bool) {