2012-03-27 23:13:14 +00:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package sync provides basic synchronization primitives such as mutual
|
2017-10-07 00:16:47 +00:00
|
|
|
// exclusion locks. Other than the Once and WaitGroup types, most are intended
|
|
|
|
// for use by low-level library routines. Higher-level synchronization is
|
2012-03-27 23:13:14 +00:00
|
|
|
// better done via channels and communication.
|
|
|
|
//
|
|
|
|
// Values containing the types defined in this package should not be copied.
|
|
|
|
package sync
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
import (
|
2017-04-10 11:32:00 +00:00
|
|
|
"internal/race"
|
2014-09-21 17:33:12 +00:00
|
|
|
"sync/atomic"
|
|
|
|
"unsafe"
|
|
|
|
)
|
2012-03-27 23:13:14 +00:00
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
func throw(string) // provided by runtime
|
|
|
|
|
2012-03-27 23:13:14 +00:00
|
|
|
// A Mutex is a mutual exclusion lock.
|
|
|
|
// Mutexes can be created as part of other structures;
|
|
|
|
// the zero value for a Mutex is an unlocked mutex.
|
2017-10-07 00:16:47 +00:00
|
|
|
//
|
|
|
|
// A Mutex must not be copied after first use.
|
2012-03-27 23:13:14 +00:00
|
|
|
type Mutex struct {
|
|
|
|
state int32
|
|
|
|
sema uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// A Locker represents an object that can be locked and unlocked.
|
|
|
|
type Locker interface {
|
|
|
|
Lock()
|
|
|
|
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() {
|
|
|
|
// Fast path: grab unlocked mutex.
|
|
|
|
if atomic.CompareAndSwapInt32(&m.state, 0, mutexLocked) {
|
2017-04-10 11:32:00 +00:00
|
|
|
if race.Enabled {
|
|
|
|
race.Acquire(unsafe.Pointer(m))
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
awoke := false
|
2017-04-10 11:32:00 +00:00
|
|
|
iter := 0
|
2012-03-27 23:13:14 +00:00
|
|
|
for {
|
|
|
|
old := m.state
|
|
|
|
new := old | mutexLocked
|
|
|
|
if old&mutexLocked != 0 {
|
2017-04-10 11:32:00 +00:00
|
|
|
if runtime_canSpin(iter) {
|
|
|
|
// Active spinning makes sense.
|
|
|
|
// Try to set mutexWoken flag to inform Unlock
|
|
|
|
// to not wake other blocked goroutines.
|
|
|
|
if !awoke && old&mutexWoken == 0 && old>>mutexWaiterShift != 0 &&
|
|
|
|
atomic.CompareAndSwapInt32(&m.state, old, old|mutexWoken) {
|
|
|
|
awoke = true
|
|
|
|
}
|
|
|
|
runtime_doSpin()
|
|
|
|
iter++
|
|
|
|
continue
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
new = old + 1<<mutexWaiterShift
|
|
|
|
}
|
|
|
|
if awoke {
|
|
|
|
// The goroutine has been woken from sleep,
|
|
|
|
// so we need to reset the flag in either case.
|
2017-04-10 11:32:00 +00:00
|
|
|
if new&mutexWoken == 0 {
|
2017-10-07 00:16:47 +00:00
|
|
|
throw("sync: inconsistent mutex state")
|
2017-04-10 11:32:00 +00:00
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
new &^= mutexWoken
|
|
|
|
}
|
|
|
|
if atomic.CompareAndSwapInt32(&m.state, old, new) {
|
|
|
|
if old&mutexLocked == 0 {
|
|
|
|
break
|
|
|
|
}
|
2017-10-07 00:16:47 +00:00
|
|
|
runtime_SemacquireMutex(&m.sema)
|
2012-03-27 23:13:14 +00:00
|
|
|
awoke = true
|
2017-04-10 11:32:00 +00:00
|
|
|
iter = 0
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
if race.Enabled {
|
|
|
|
race.Acquire(unsafe.Pointer(m))
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock unlocks m.
|
|
|
|
// It is a run-time error if m is not locked on entry to 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.
|
|
|
|
func (m *Mutex) Unlock() {
|
2017-04-10 11:32:00 +00:00
|
|
|
if race.Enabled {
|
2014-09-21 17:33:12 +00:00
|
|
|
_ = m.state
|
2017-04-10 11:32:00 +00:00
|
|
|
race.Release(unsafe.Pointer(m))
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
|
2012-03-27 23:13:14 +00:00
|
|
|
// Fast path: drop lock bit.
|
|
|
|
new := atomic.AddInt32(&m.state, -mutexLocked)
|
|
|
|
if (new+mutexLocked)&mutexLocked == 0 {
|
2017-10-07 00:16:47 +00:00
|
|
|
throw("sync: unlock of unlocked mutex")
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
old := new
|
|
|
|
for {
|
|
|
|
// If there are no waiters or a goroutine has already
|
|
|
|
// been woken or grabbed the lock, no need to wake anyone.
|
|
|
|
if old>>mutexWaiterShift == 0 || old&(mutexLocked|mutexWoken) != 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Grab the right to wake someone.
|
|
|
|
new = (old - 1<<mutexWaiterShift) | mutexWoken
|
|
|
|
if atomic.CompareAndSwapInt32(&m.state, old, new) {
|
|
|
|
runtime_Semrelease(&m.sema)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
old = m.state
|
|
|
|
}
|
|
|
|
}
|