2009-11-30 13:10:56 -07: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
|
|
|
|
|
2012-10-07 12:07:03 -06:00
|
|
|
import (
|
|
|
|
"sync/atomic"
|
|
|
|
"unsafe"
|
|
|
|
)
|
2011-02-25 12:29:47 -07:00
|
|
|
|
2009-11-30 13:10:56 -07:00
|
|
|
// An RWMutex is a reader/writer mutual exclusion lock.
|
|
|
|
// The lock can be held by an arbitrary number of readers
|
|
|
|
// or a single writer.
|
|
|
|
// RWMutexes can be created as part of other
|
|
|
|
// structures; the zero value for a RWMutex is
|
|
|
|
// an unlocked mutex.
|
|
|
|
type RWMutex struct {
|
2011-07-12 10:24:21 -06:00
|
|
|
w Mutex // held if there are pending writers
|
|
|
|
writerSem uint32 // semaphore for writers to wait for completing readers
|
|
|
|
readerSem uint32 // semaphore for readers to wait for completing writers
|
|
|
|
readerCount int32 // number of pending readers
|
|
|
|
readerWait int32 // number of departing readers
|
2009-11-30 13:10:56 -07:00
|
|
|
}
|
|
|
|
|
2011-07-12 10:24:21 -06:00
|
|
|
const rwmutexMaxReaders = 1 << 30
|
|
|
|
|
2009-11-30 13:10:56 -07:00
|
|
|
// RLock locks rw for reading.
|
|
|
|
func (rw *RWMutex) RLock() {
|
2012-10-07 12:07:03 -06:00
|
|
|
if raceenabled {
|
2013-04-08 15:46:54 -06:00
|
|
|
_ = rw.w.state
|
2012-10-07 12:07:03 -06:00
|
|
|
raceDisable()
|
|
|
|
}
|
2011-07-12 10:24:21 -06:00
|
|
|
if atomic.AddInt32(&rw.readerCount, 1) < 0 {
|
|
|
|
// A writer is pending, wait for it.
|
2012-02-18 22:11:44 -07:00
|
|
|
runtime_Semacquire(&rw.readerSem)
|
2009-11-30 13:10:56 -07:00
|
|
|
}
|
2012-10-07 12:07:03 -06:00
|
|
|
if raceenabled {
|
|
|
|
raceEnable()
|
|
|
|
raceAcquire(unsafe.Pointer(&rw.readerSem))
|
|
|
|
}
|
2009-11-30 13:10:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// RUnlock undoes a single RLock call;
|
|
|
|
// it does not affect other simultaneous readers.
|
|
|
|
// It is a run-time error if rw is not locked for reading
|
|
|
|
// on entry to RUnlock.
|
|
|
|
func (rw *RWMutex) RUnlock() {
|
2012-10-07 12:07:03 -06:00
|
|
|
if raceenabled {
|
2013-04-08 15:46:54 -06:00
|
|
|
_ = rw.w.state
|
2012-10-07 12:07:03 -06:00
|
|
|
raceReleaseMerge(unsafe.Pointer(&rw.writerSem))
|
|
|
|
raceDisable()
|
|
|
|
}
|
2014-06-19 23:19:56 -06:00
|
|
|
if r := atomic.AddInt32(&rw.readerCount, -1); r < 0 {
|
|
|
|
if r+1 == 0 || r+1 == -rwmutexMaxReaders {
|
|
|
|
raceEnable()
|
|
|
|
panic("sync: RUnlock of unlocked RWMutex")
|
|
|
|
}
|
2011-07-12 10:24:21 -06:00
|
|
|
// A writer is pending.
|
|
|
|
if atomic.AddInt32(&rw.readerWait, -1) == 0 {
|
|
|
|
// The last reader unblocks the writer.
|
2012-02-18 22:11:44 -07:00
|
|
|
runtime_Semrelease(&rw.writerSem)
|
2011-07-12 10:24:21 -06:00
|
|
|
}
|
2009-11-30 13:10:56 -07:00
|
|
|
}
|
2012-10-07 12:07:03 -06:00
|
|
|
if raceenabled {
|
|
|
|
raceEnable()
|
|
|
|
}
|
2009-11-30 13:10:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lock locks rw for writing.
|
|
|
|
// If the lock is already locked for reading or writing,
|
|
|
|
// Lock blocks until the lock is available.
|
|
|
|
// To ensure that the lock eventually becomes available,
|
|
|
|
// a blocked Lock call excludes new readers from acquiring
|
|
|
|
// the lock.
|
|
|
|
func (rw *RWMutex) Lock() {
|
2012-10-07 12:07:03 -06:00
|
|
|
if raceenabled {
|
2013-04-08 15:46:54 -06:00
|
|
|
_ = rw.w.state
|
2012-10-07 12:07:03 -06:00
|
|
|
raceDisable()
|
|
|
|
}
|
2011-07-12 10:24:21 -06:00
|
|
|
// First, resolve competition with other writers.
|
2009-12-15 16:40:16 -07:00
|
|
|
rw.w.Lock()
|
2011-07-12 10:24:21 -06:00
|
|
|
// Announce to readers there is a pending writer.
|
|
|
|
r := atomic.AddInt32(&rw.readerCount, -rwmutexMaxReaders) + rwmutexMaxReaders
|
|
|
|
// Wait for active readers.
|
|
|
|
if r != 0 && atomic.AddInt32(&rw.readerWait, r) != 0 {
|
2012-02-18 22:11:44 -07:00
|
|
|
runtime_Semacquire(&rw.writerSem)
|
2011-07-12 10:24:21 -06:00
|
|
|
}
|
2012-10-07 12:07:03 -06:00
|
|
|
if raceenabled {
|
|
|
|
raceEnable()
|
|
|
|
raceAcquire(unsafe.Pointer(&rw.readerSem))
|
|
|
|
raceAcquire(unsafe.Pointer(&rw.writerSem))
|
|
|
|
}
|
2009-11-30 13:10:56 -07:00
|
|
|
}
|
|
|
|
|
2011-02-01 22:29:46 -07:00
|
|
|
// Unlock unlocks rw for writing. It is a run-time error if rw is
|
|
|
|
// not locked for writing on entry to Unlock.
|
2009-11-30 13:10:56 -07:00
|
|
|
//
|
2011-02-01 22:29:46 -07:00
|
|
|
// As with Mutexes, a locked RWMutex is not associated with a particular
|
|
|
|
// goroutine. One goroutine may RLock (Lock) an RWMutex and then
|
2009-11-30 13:10:56 -07:00
|
|
|
// arrange for another goroutine to RUnlock (Unlock) it.
|
2011-07-12 10:24:21 -06:00
|
|
|
func (rw *RWMutex) Unlock() {
|
2012-10-07 12:07:03 -06:00
|
|
|
if raceenabled {
|
2013-04-08 15:46:54 -06:00
|
|
|
_ = rw.w.state
|
2012-10-07 12:07:03 -06:00
|
|
|
raceRelease(unsafe.Pointer(&rw.readerSem))
|
|
|
|
raceRelease(unsafe.Pointer(&rw.writerSem))
|
|
|
|
raceDisable()
|
|
|
|
}
|
|
|
|
|
2011-07-12 10:24:21 -06:00
|
|
|
// Announce to readers there is no active writer.
|
|
|
|
r := atomic.AddInt32(&rw.readerCount, rwmutexMaxReaders)
|
2014-06-19 23:19:56 -06:00
|
|
|
if r >= rwmutexMaxReaders {
|
|
|
|
raceEnable()
|
|
|
|
panic("sync: Unlock of unlocked RWMutex")
|
|
|
|
}
|
2011-07-12 10:24:21 -06:00
|
|
|
// Unblock blocked readers, if any.
|
|
|
|
for i := 0; i < int(r); i++ {
|
2012-02-18 22:11:44 -07:00
|
|
|
runtime_Semrelease(&rw.readerSem)
|
2011-07-12 10:24:21 -06:00
|
|
|
}
|
|
|
|
// Allow other writers to proceed.
|
|
|
|
rw.w.Unlock()
|
2012-10-07 12:07:03 -06:00
|
|
|
if raceenabled {
|
|
|
|
raceEnable()
|
|
|
|
}
|
2011-07-12 10:24:21 -06:00
|
|
|
}
|
2011-02-16 12:11:07 -07:00
|
|
|
|
|
|
|
// RLocker returns a Locker interface that implements
|
|
|
|
// the Lock and Unlock methods by calling rw.RLock and rw.RUnlock.
|
|
|
|
func (rw *RWMutex) RLocker() Locker {
|
|
|
|
return (*rlocker)(rw)
|
|
|
|
}
|
|
|
|
|
|
|
|
type rlocker RWMutex
|
|
|
|
|
|
|
|
func (r *rlocker) Lock() { (*RWMutex)(r).RLock() }
|
|
|
|
func (r *rlocker) Unlock() { (*RWMutex)(r).RUnlock() }
|