Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Condvar: Add wait morphing to condvar
This patch adds wait morphing to condvar: 1. condvar->wake*() doesn't wake the thread to take the user mutex. Rather, it attempts to grab the lock for the sleeping thread, and if the lock is already taken, move the sleeping thread to wait on the mutex's queue, without waking the thread up. 2. condvar->wait() now assumes that when it is woken up, it already has the mutex. Wait morphing reduces unnecessary context switches, and therefore improves performance, in two case: 1. The "thundering herd" problem - when there are many threads waiting on the condvar, if condvar->wake_all() wakes all of them, all will race to get the mutex and likely many of them will go back to sleep. 2. The "locked wakeup" problem - when condvar_>wake*() is done with the user mutex locked (as it is very often does), if we wake a waiter to take the lock, it may find the lock already held (by the waker) and go back to sleep.
- Loading branch information