-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Open
Labels
Milestone
Description
sync.Mutex allows lock/unlock in different goroutines:
http://golang.org/pkg/sync/#Mutex.Unlock
"A locked Mutex is not associated with a particular goroutine. It is allowed for
one goroutine to lock a Mutex and then arrange for another goroutine to unlock it."
And the same for RWMutex:
http://golang.org/pkg/sync/#RWMutex.Unlock
The possibility to unlock the mutex in a different goroutine is very rarely used in real
code. And if you really need something more complex than lock/unlock in a single
goroutine, you can always use channels.
But it creates several problems:
- Deadlock detection becomes impossible, as there is no notion of "critical
sections". - Similarly static lock annotations becomes impossible for the same reason.
- Optimizations like hardware lock elision (see e.g. Intel HLE) become impossible.
- Another potential optimization that becomes impossible is priority boosting inside of critical sections. Namely, if a goroutine is preempted inside of a critical sections, scheduler may give it another tiny time slot to finish the critical section (or perhaps it can then voluntarily switch in Unlock). Solaris did this for a long time.
We should prohibit possibility to unlock in a different goroutine in Go2.
hoenirvili, bcmills, as, CAFxX, theodesp and 12 moretmthrgd and tristanfisher