Repository: tokio-rs/tokio
File: tokio/src/runtime/task/list.rs (Rust)

Code before:
    }

    pub(crate) fn active_tasks_count(&self) -> usize {
        self.count.load(Ordering::Relaxed)
    }

    pub(crate) fn remove(&self, task: &Task<S>) -> Option<Task<S>> {
        // If the task's owner ID is `None` then it is not part of any list and
        // doesn't need removing.
        let task_id = task.header().get_owner_id()?;

        assert_eq!(task_id, self.id);

        // safety: We just checked that the provided task is not in some other
        // linked list.
        unsafe { self.remove_inner(task) }
    }

    #[inline]
    unsafe fn remove_inner(&self, task: &Task<S>) -> Option<Task<S>> {
        // Safety: it is safe, because every task has one task_id
        let task_id = unsafe { Header::get_id(task.header_ptr()) };
        let mut lock = self.segment_inner(task_id.0 as usize);
        let task = lock.remove(task.header_ptr())?;
        drop(lock);
        self.count.fetch_sub(1, Ordering::Relaxed);
        Some(task)
    }

    #[inline]
    fn segment_inner(&self, id: usize) -> MutexGuard<'_, ListSement<S>> {
        // Safety: this modulo operation ensures it is safe here.
        unsafe {
            self.lists
                .get_unchecked(id & (self.segment_mask) as usize)
                .lock()
        }
    }

    pub(crate) fn is_empty(&self) -> bool {
        self.count.load(Ordering::Relaxed) == 0
    }
}

cfg_taskdump! {
    impl<S: 'static> OwnedTasks<S> {
        /// Locks the tasks, and calls `f` on an iterator over them.
        pub(crate) fn for_each<F>(&self, mut f: F)
        where
            F: FnMut(&Task<S>),
        {

Review comment (suggestion):
You should only subtract here if `task.is_some()`.

Code after:
    /// Due to the above reasons, we set a maximum value for the shared list size,
    /// denoted as `MAX_SHARED_LIST_SIZE`.
    fn gen_shared_list_size(num_cores: usize) -> usize {
        const MAX_SHARED_LIST_SIZE: usize = 1 << 16;
        usize::min(MAX_SHARED_LIST_SIZE, num_cores.next_power_of_two() * 4)
    }
}

cfg_taskdump! {
    impl<S: 'static> OwnedTasks<S> {
        /// Locks the tasks, and calls `f` on an iterator over them.
        pub(crate) fn for_each<F>(&self, f: F)
        where
            F: FnMut(&Task<S>),
        {
            self.list.for_each(f);
        }
    }
}

impl<S: 'static> LocalOwnedTasks<S> {
    pub(crate) fn new() -> Self {
        Self {
            inner: UnsafeCell::new(OwnedTasksInner {
                list: LinkedList::new(),
                closed: false,
            }),
            id: get_next_id(),
            _not_send_or_sync: PhantomData,
        }
    }

    pub(crate) fn bind<T>(
        &self,
        task: T,
        scheduler: S,
        id: super::Id,
    ) -> (JoinHandle<T::Output>, Option<Notified<S>>)
    where
        S: Schedule,
        T: Future + 'static,
        T::Output: 'static,
    {
        let (task, notified, join) = super::new_task(task, scheduler, id);

        unsafe {
            // safety: We just created the task, so we have exclusive access
            // to the field.
            task.header().set_owner_id(self.id);
        }