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

Code before:
        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>> {
        let mut lock = self.segment_inner(task.task_id() as usize);
        match lock.remove(task.header_ptr()) {
            Some(task) => {
                drop(lock);
                self.count.fetch_sub(1, Ordering::Relaxed);
                Some(task)
            }
            None => None,
        }
    }

    #[inline]
    fn segment_inner(&self, id: usize) -> MutexGuard<'_, ListSement<S>> {
        self.lists[id & (self.segment_mask) as usize].lock()
    }

    pub(crate) fn is_closed(&self) -> bool {
        self.closed.load(Ordering::Acquire)
    }

    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>),
        {
            for i in 0 .. self.lists.len()  {
                self.lists[i].lock().for_each(&mut f);

Review comment (refactor):
style nit, take it or leave it: i think we can simplify this code to just
```suggestion
        let task = self.segment_inner(task.task_id() as usize).remove(task.header_ptr())?;
        self.count.fetch_sub(1, Ordering::Relaxed);
        Some(task)
```

Code after:
        // 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.list.remove(task.header_ptr()) }
    }

    pub(crate) fn is_empty(&self) -> bool {
        self.list.is_empty()
    }

    /// Generates the size of the sharded list based on the number of worker threads.
    ///
    /// The sharded lock design can effectively alleviate
    /// lock contention performance problems caused by high concurrency.
    ///
    /// However, as the number of shards increases, the memory continuity between
    /// nodes in the intrusive linked list will diminish. Furthermore,
    /// the construction time of the sharded list will also increase with a higher number of shards.
    ///
    /// 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(),