| 1 |
747 |
jeremybenn |
// Copyright 2009 The Go Authors. All rights reserved.
|
| 2 |
|
|
// Use of this source code is governed by a BSD-style
|
| 3 |
|
|
// license that can be found in the LICENSE file.
|
| 4 |
|
|
|
| 5 |
|
|
package sync
|
| 6 |
|
|
|
| 7 |
|
|
import (
|
| 8 |
|
|
"runtime"
|
| 9 |
|
|
"sync/atomic"
|
| 10 |
|
|
)
|
| 11 |
|
|
|
| 12 |
|
|
// An RWMutex is a reader/writer mutual exclusion lock.
|
| 13 |
|
|
// The lock can be held by an arbitrary number of readers
|
| 14 |
|
|
// or a single writer.
|
| 15 |
|
|
// RWMutexes can be created as part of other
|
| 16 |
|
|
// structures; the zero value for a RWMutex is
|
| 17 |
|
|
// an unlocked mutex.
|
| 18 |
|
|
type RWMutex struct {
|
| 19 |
|
|
w Mutex // held if there are pending writers
|
| 20 |
|
|
writerSem uint32 // semaphore for writers to wait for completing readers
|
| 21 |
|
|
readerSem uint32 // semaphore for readers to wait for completing writers
|
| 22 |
|
|
readerCount int32 // number of pending readers
|
| 23 |
|
|
readerWait int32 // number of departing readers
|
| 24 |
|
|
}
|
| 25 |
|
|
|
| 26 |
|
|
const rwmutexMaxReaders = 1 << 30
|
| 27 |
|
|
|
| 28 |
|
|
// RLock locks rw for reading.
|
| 29 |
|
|
func (rw *RWMutex) RLock() {
|
| 30 |
|
|
if atomic.AddInt32(&rw.readerCount, 1) < 0 {
|
| 31 |
|
|
// A writer is pending, wait for it.
|
| 32 |
|
|
runtime.Semacquire(&rw.readerSem)
|
| 33 |
|
|
}
|
| 34 |
|
|
}
|
| 35 |
|
|
|
| 36 |
|
|
// RUnlock undoes a single RLock call;
|
| 37 |
|
|
// it does not affect other simultaneous readers.
|
| 38 |
|
|
// It is a run-time error if rw is not locked for reading
|
| 39 |
|
|
// on entry to RUnlock.
|
| 40 |
|
|
func (rw *RWMutex) RUnlock() {
|
| 41 |
|
|
if atomic.AddInt32(&rw.readerCount, -1) < 0 {
|
| 42 |
|
|
// A writer is pending.
|
| 43 |
|
|
if atomic.AddInt32(&rw.readerWait, -1) == 0 {
|
| 44 |
|
|
// The last reader unblocks the writer.
|
| 45 |
|
|
runtime.Semrelease(&rw.writerSem)
|
| 46 |
|
|
}
|
| 47 |
|
|
}
|
| 48 |
|
|
}
|
| 49 |
|
|
|
| 50 |
|
|
// Lock locks rw for writing.
|
| 51 |
|
|
// If the lock is already locked for reading or writing,
|
| 52 |
|
|
// Lock blocks until the lock is available.
|
| 53 |
|
|
// To ensure that the lock eventually becomes available,
|
| 54 |
|
|
// a blocked Lock call excludes new readers from acquiring
|
| 55 |
|
|
// the lock.
|
| 56 |
|
|
func (rw *RWMutex) Lock() {
|
| 57 |
|
|
// First, resolve competition with other writers.
|
| 58 |
|
|
rw.w.Lock()
|
| 59 |
|
|
// Announce to readers there is a pending writer.
|
| 60 |
|
|
r := atomic.AddInt32(&rw.readerCount, -rwmutexMaxReaders) + rwmutexMaxReaders
|
| 61 |
|
|
// Wait for active readers.
|
| 62 |
|
|
if r != 0 && atomic.AddInt32(&rw.readerWait, r) != 0 {
|
| 63 |
|
|
runtime.Semacquire(&rw.writerSem)
|
| 64 |
|
|
}
|
| 65 |
|
|
}
|
| 66 |
|
|
|
| 67 |
|
|
// Unlock unlocks rw for writing. It is a run-time error if rw is
|
| 68 |
|
|
// not locked for writing on entry to Unlock.
|
| 69 |
|
|
//
|
| 70 |
|
|
// As with Mutexes, a locked RWMutex is not associated with a particular
|
| 71 |
|
|
// goroutine. One goroutine may RLock (Lock) an RWMutex and then
|
| 72 |
|
|
// arrange for another goroutine to RUnlock (Unlock) it.
|
| 73 |
|
|
func (rw *RWMutex) Unlock() {
|
| 74 |
|
|
// Announce to readers there is no active writer.
|
| 75 |
|
|
r := atomic.AddInt32(&rw.readerCount, rwmutexMaxReaders)
|
| 76 |
|
|
// Unblock blocked readers, if any.
|
| 77 |
|
|
for i := 0; i < int(r); i++ {
|
| 78 |
|
|
runtime.Semrelease(&rw.readerSem)
|
| 79 |
|
|
}
|
| 80 |
|
|
// Allow other writers to proceed.
|
| 81 |
|
|
rw.w.Unlock()
|
| 82 |
|
|
}
|
| 83 |
|
|
|
| 84 |
|
|
// RLocker returns a Locker interface that implements
|
| 85 |
|
|
// the Lock and Unlock methods by calling rw.RLock and rw.RUnlock.
|
| 86 |
|
|
func (rw *RWMutex) RLocker() Locker {
|
| 87 |
|
|
return (*rlocker)(rw)
|
| 88 |
|
|
}
|
| 89 |
|
|
|
| 90 |
|
|
type rlocker RWMutex
|
| 91 |
|
|
|
| 92 |
|
|
func (r *rlocker) Lock() { (*RWMutex)(r).RLock() }
|
| 93 |
|
|
func (r *rlocker) Unlock() { (*RWMutex)(r).RUnlock() }
|