2017-06-15 17:42:08 -06:00
|
|
|
// Copyright 2017 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 runtime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime/internal/atomic"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This is a copy of sync/rwmutex.go rewritten to work in the runtime.
|
|
|
|
|
2018-01-16 15:57:44 -07:00
|
|
|
// A rwmutex is a reader/writer mutual exclusion lock.
|
2017-06-15 17:42:08 -06:00
|
|
|
// The lock can be held by an arbitrary number of readers or a single writer.
|
|
|
|
// This is a variant of sync.RWMutex, for the runtime package.
|
2017-06-28 13:54:46 -06:00
|
|
|
// Like mutex, rwmutex blocks the calling M.
|
|
|
|
// It does not interact with the goroutine scheduler.
|
2017-06-15 17:42:08 -06:00
|
|
|
type rwmutex struct {
|
2017-06-28 13:54:46 -06:00
|
|
|
rLock mutex // protects readers, readerPass, writer
|
|
|
|
readers muintptr // list of pending readers
|
|
|
|
readerPass uint32 // number of pending readers to skip readers list
|
|
|
|
|
|
|
|
wLock mutex // serializes writers
|
|
|
|
writer muintptr // pending writer waiting for completing readers
|
|
|
|
|
2017-06-15 17:42:08 -06:00
|
|
|
readerCount uint32 // number of pending readers
|
|
|
|
readerWait uint32 // number of departing readers
|
|
|
|
}
|
|
|
|
|
|
|
|
const rwmutexMaxReaders = 1 << 30
|
|
|
|
|
|
|
|
// rlock locks rw for reading.
|
|
|
|
func (rw *rwmutex) rlock() {
|
2017-07-06 09:55:39 -06:00
|
|
|
// The reader must not be allowed to lose its P or else other
|
|
|
|
// things blocking on the lock may consume all of the Ps and
|
|
|
|
// deadlock (issue #20903). Alternatively, we could drop the P
|
|
|
|
// while sleeping.
|
|
|
|
acquirem()
|
2017-06-15 17:42:08 -06:00
|
|
|
if int32(atomic.Xadd(&rw.readerCount, 1)) < 0 {
|
2017-06-28 13:54:46 -06:00
|
|
|
// A writer is pending. Park on the reader queue.
|
|
|
|
systemstack(func() {
|
|
|
|
lock(&rw.rLock)
|
|
|
|
if rw.readerPass > 0 {
|
|
|
|
// Writer finished.
|
|
|
|
rw.readerPass -= 1
|
|
|
|
unlock(&rw.rLock)
|
|
|
|
} else {
|
|
|
|
// Queue this reader to be woken by
|
|
|
|
// the writer.
|
|
|
|
m := getg().m
|
|
|
|
m.schedlink = rw.readers
|
|
|
|
rw.readers.set(m)
|
|
|
|
unlock(&rw.rLock)
|
|
|
|
notesleep(&m.park)
|
|
|
|
noteclear(&m.park)
|
|
|
|
}
|
|
|
|
})
|
2017-06-15 17:42:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// runlock undoes a single rlock call on rw.
|
|
|
|
func (rw *rwmutex) runlock() {
|
|
|
|
if r := int32(atomic.Xadd(&rw.readerCount, -1)); r < 0 {
|
|
|
|
if r+1 == 0 || r+1 == -rwmutexMaxReaders {
|
|
|
|
throw("runlock of unlocked rwmutex")
|
|
|
|
}
|
|
|
|
// A writer is pending.
|
|
|
|
if atomic.Xadd(&rw.readerWait, -1) == 0 {
|
|
|
|
// The last reader unblocks the writer.
|
2017-06-28 13:54:46 -06:00
|
|
|
lock(&rw.rLock)
|
|
|
|
w := rw.writer.ptr()
|
|
|
|
if w != nil {
|
|
|
|
notewakeup(&w.park)
|
|
|
|
}
|
|
|
|
unlock(&rw.rLock)
|
2017-06-15 17:42:08 -06:00
|
|
|
}
|
|
|
|
}
|
2017-07-06 09:55:39 -06:00
|
|
|
releasem(getg().m)
|
2017-06-15 17:42:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// lock locks rw for writing.
|
|
|
|
func (rw *rwmutex) lock() {
|
2017-07-06 09:55:39 -06:00
|
|
|
// Resolve competition with other writers and stick to our P.
|
2017-06-28 13:54:46 -06:00
|
|
|
lock(&rw.wLock)
|
|
|
|
m := getg().m
|
2017-06-15 17:42:08 -06:00
|
|
|
// Announce that there is a pending writer.
|
|
|
|
r := int32(atomic.Xadd(&rw.readerCount, -rwmutexMaxReaders)) + rwmutexMaxReaders
|
|
|
|
// Wait for any active readers to complete.
|
2017-06-28 13:54:46 -06:00
|
|
|
lock(&rw.rLock)
|
2017-06-15 17:42:08 -06:00
|
|
|
if r != 0 && atomic.Xadd(&rw.readerWait, r) != 0 {
|
2017-06-28 13:54:46 -06:00
|
|
|
// Wait for reader to wake us up.
|
|
|
|
systemstack(func() {
|
|
|
|
rw.writer.set(m)
|
|
|
|
unlock(&rw.rLock)
|
|
|
|
notesleep(&m.park)
|
|
|
|
noteclear(&m.park)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
unlock(&rw.rLock)
|
2017-06-15 17:42:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// unlock unlocks rw for writing.
|
|
|
|
func (rw *rwmutex) unlock() {
|
|
|
|
// Announce to readers that there is no active writer.
|
|
|
|
r := int32(atomic.Xadd(&rw.readerCount, rwmutexMaxReaders))
|
|
|
|
if r >= rwmutexMaxReaders {
|
|
|
|
throw("unlock of unlocked rwmutex")
|
|
|
|
}
|
2017-06-28 13:54:46 -06:00
|
|
|
// Unblock blocked readers.
|
|
|
|
lock(&rw.rLock)
|
|
|
|
for rw.readers.ptr() != nil {
|
|
|
|
reader := rw.readers.ptr()
|
|
|
|
rw.readers = reader.schedlink
|
|
|
|
reader.schedlink.set(nil)
|
|
|
|
notewakeup(&reader.park)
|
|
|
|
r -= 1
|
2017-06-15 17:42:08 -06:00
|
|
|
}
|
2017-06-28 13:54:46 -06:00
|
|
|
// If r > 0, there are pending readers that aren't on the
|
|
|
|
// queue. Tell them to skip waiting.
|
|
|
|
rw.readerPass += uint32(r)
|
|
|
|
unlock(&rw.rLock)
|
2017-06-15 17:42:08 -06:00
|
|
|
// Allow other writers to proceed.
|
2017-06-28 13:54:46 -06:00
|
|
|
unlock(&rw.wLock)
|
2017-06-15 17:42:08 -06:00
|
|
|
}
|