2010-12-03 05:34:57 +01: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.
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
// Package sync provides basic synchronization primitives such as mutual
|
|
|
|
// exclusion locks. Other than the Once and WaitGroup types, most are intended
|
|
|
|
// for use by low-level library routines. Higher-level synchronization is
|
|
|
|
// better done via channels and communication.
|
2011-12-13 00:40:51 +01:00
|
|
|
//
|
|
|
|
// Values containing the types defined in this package should not be copied.
|
2010-12-03 05:34:57 +01:00
|
|
|
package sync
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
import (
|
|
|
|
"sync/atomic"
|
|
|
|
"unsafe"
|
|
|
|
)
|
2010-12-03 05:34:57 +01: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.
|
|
|
|
type Mutex struct {
|
2011-09-16 17:47:21 +02:00
|
|
|
state int32
|
|
|
|
sema uint32
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
// A Locker represents an object that can be locked and unlocked.
|
|
|
|
type Locker interface {
|
|
|
|
Lock()
|
|
|
|
Unlock()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
const (
|
|
|
|
mutexLocked = 1 << iota // mutex is locked
|
|
|
|
mutexWoken
|
|
|
|
mutexWaiterShift = iota
|
|
|
|
)
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// Lock locks m.
|
|
|
|
// If the lock is already in use, the calling goroutine
|
|
|
|
// blocks until the mutex is available.
|
|
|
|
func (m *Mutex) Lock() {
|
2011-09-16 17:47:21 +02:00
|
|
|
// Fast path: grab unlocked mutex.
|
|
|
|
if atomic.CompareAndSwapInt32(&m.state, 0, mutexLocked) {
|
2012-10-23 06:31:11 +02:00
|
|
|
if raceenabled {
|
|
|
|
raceAcquire(unsafe.Pointer(m))
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
return
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
|
|
|
|
awoke := false
|
|
|
|
for {
|
|
|
|
old := m.state
|
|
|
|
new := old | mutexLocked
|
|
|
|
if old&mutexLocked != 0 {
|
|
|
|
new = old + 1<<mutexWaiterShift
|
|
|
|
}
|
|
|
|
if awoke {
|
|
|
|
// The goroutine has been woken from sleep,
|
|
|
|
// so we need to reset the flag in either case.
|
|
|
|
new &^= mutexWoken
|
|
|
|
}
|
|
|
|
if atomic.CompareAndSwapInt32(&m.state, old, new) {
|
|
|
|
if old&mutexLocked == 0 {
|
|
|
|
break
|
|
|
|
}
|
2012-03-02 21:01:37 +01:00
|
|
|
runtime_Semacquire(&m.sema)
|
2011-09-16 17:47:21 +02:00
|
|
|
awoke = true
|
|
|
|
}
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
|
|
|
|
if raceenabled {
|
|
|
|
raceAcquire(unsafe.Pointer(m))
|
|
|
|
}
|
2010-12-03 05:34:57 +01: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() {
|
2012-10-23 06:31:11 +02:00
|
|
|
if raceenabled {
|
2013-07-16 08:54:42 +02:00
|
|
|
_ = m.state
|
2012-10-23 06:31:11 +02:00
|
|
|
raceRelease(unsafe.Pointer(m))
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Fast path: drop lock bit.
|
|
|
|
new := atomic.AddInt32(&m.state, -mutexLocked)
|
|
|
|
if (new+mutexLocked)&mutexLocked == 0 {
|
2011-03-17 00:05:44 +01:00
|
|
|
panic("sync: unlock of unlocked mutex")
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-09-16 17:47:21 +02: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) {
|
2012-03-02 21:01:37 +01:00
|
|
|
runtime_Semrelease(&m.sema)
|
2011-09-16 17:47:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
old = m.state
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|