Skip to content

unlock notifies _all_ waiting tasks #56182

Open

Description

unlock(rl::ReentrantLock) does a notify(cond) which wakes all tasks waiting on the lock. As this is a lock, only one of these tasks can succeed, so it is unnecessary to schedule all of them. When there are a large number of tasks, the current approach wastes CPU time and can cause a quadratic amount of allocations for waitlist nodes.

It isn't straightforward to trivially fix this by changing the notify call to set all=false -- unlock will only call notify if ReentrantLock.havelock is 0x02 (see here) and that only happens after the first notify if all waiting tasks are woken up to race for the lock again (see here).

Broken out from #50425 as this is a separate issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    multithreadingBase.Threads and related functionalityperformanceMust go faster

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions