diff --git a/src/pkg/sync/mutex.go b/src/pkg/sync/mutex.go index 13f03cad39..2d46c89948 100644 --- a/src/pkg/sync/mutex.go +++ b/src/pkg/sync/mutex.go @@ -17,8 +17,8 @@ import ( // Mutexes can be created as part of other structures; // the zero value for a Mutex is an unlocked mutex. type Mutex struct { - key int32 - sema uint32 + state int32 + sema uint32 } // A Locker represents an object that can be locked and unlocked. @@ -27,15 +27,41 @@ type Locker interface { Unlock() } +const ( + mutexLocked = 1 << iota // mutex is locked + mutexWoken + mutexWaiterShift = iota +) + // Lock locks m. // If the lock is already in use, the calling goroutine // blocks until the mutex is available. func (m *Mutex) Lock() { - if atomic.AddInt32(&m.key, 1) == 1 { - // changed from 0 to 1; we hold lock + // Fast path: grab unlocked mutex. + if atomic.CompareAndSwapInt32(&m.state, 0, mutexLocked) { return } - runtime.Semacquire(&m.sema) + + awoke := false + for { + old := m.state + new := old | mutexLocked + if old&mutexLocked != 0 { + new = old + 1<>mutexWaiterShift == 0 || old&(mutexLocked|mutexWoken) != 0 { + return + } + // Grab the right to wake someone. + new = (old - 1<