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

Code before:
    }

    /// Shuts down all tasks in the collection. This call also closes the
    /// collection, preventing new items from being added.
    /// The parameter start should be random among different worker threads
    /// to reduce lock conflicts during shutdown.
    /// Initiate shutting down the segment indexed by the start, and reset to 0
    /// once the segment_size is reached, continuing until start - 1, it works like a ring.
    pub(crate) fn close_and_shutdown_all(&self, start: usize)
    where
        S: Schedule,
    {
        self.closed.store(true, Ordering::Release);
        for i in start..self.segment_size as usize + start {
            loop {
                let mut lock = self.segment_inner(i);
                let task = match lock.pop_back() {
                    Some(task) => {
                        drop(lock);
                        self.count.fetch_sub(1, Ordering::Relaxed);
                        task
                    }
                    None => break,
                };
                task.shutdown();
            }
        }
    }

    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>> {
        let mut lock = self.segment_inner(task.task_id() as usize);
        match lock.remove(task.header_ptr()) {
            Some(task) => {
                drop(lock);

Review comment (refactor):
style nit: i think we can simplify this to
```suggestion
            while let Some(task) = self.segment_inner(i).pop_back() {
                self.count.fetch_sub(1, Ordering::Relaxed);
                task.shutdown();
            }
```
since the `lock` is dropped at the end of the expression `self.segment_inner(i).pop_back()`. of course, if you think it improves readability to make the duration of the mutex lock more explicit, we could also write
```suggestion
            loop {
                let mut lock = self.segment_inner(i);
                match lock.pop_back() {
                    Some(task) => {
                        drop(lock);
                        self.count.fetch_sub(1, Ordering::Relaxed);
                        task.shutdown();
                    }
                    None => break,
                };
            }
```
but IMO, the `while` loop is a bit nicer. it's up to you, though.

Code after:
    /// Using different values for each worker thread reduces contention.
    pub(crate) fn close_and_shutdown_all(&self, start: usize)
    where
        S: Schedule,
    {
        self.closed.store(true, Ordering::Release);
        for i in start..self.get_shard_size() + start {
            loop {
                let task = self.list.pop_back(i);
                match task {
                    Some(task) => {
                        task.shutdown();
                    }
                    None => break,
                }
            }
        }
    }

    #[inline]
    pub(crate) fn get_shard_size(&self) -> usize {
        self.list.shard_size()
    }

    pub(crate) fn active_tasks_count(&self) -> usize {
        self.list.len()
    }

    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.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,