1
0
mirror of https://github.com/golang/go synced 2024-11-20 00:24:43 -07:00
go/src/sync/rwmutex.go

141 lines
4.0 KiB
Go
Raw Normal View History

// 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
import (
"internal/race"
"sync/atomic"
"unsafe"
)
// 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.
//
// An RWMutex must not be copied after first use.
//
// If a goroutine holds a RWMutex for reading, it must not expect this or any
// other goroutine to be able to also take the read lock until the first read
// lock is released. In particular, this prohibits recursive read locking.
// This is to ensure that the lock eventually becomes available;
// a blocked Lock call excludes new readers from acquiring the lock.
type RWMutex struct {
sync: improve RWMutex performance The new implementation features wait-free fast path for readers which significantly improves performance/scalability on read-mostly workloads. Benchmark results on HP Z600 (2 x Xeon E5620, 8 HT cores, 2.40GHz) are as follows: benchmark old ns/op new ns/op delta BenchmarkRWMutexUncontended 179.00 96.60 -46.03% BenchmarkRWMutexUncontended-2 89.10 49.10 -44.89% BenchmarkRWMutexUncontended-4 44.70 24.70 -44.74% BenchmarkRWMutexUncontended-8 23.30 12.90 -44.64% BenchmarkRWMutexUncontended-16 16.80 8.75 -47.92% BenchmarkRWMutexWrite100 79.60 26.80 -66.33% BenchmarkRWMutexWrite100-2 305.00 33.00 -89.18% BenchmarkRWMutexWrite100-4 245.00 113.00 -53.88% BenchmarkRWMutexWrite100-8 330.00 147.00 -55.45% BenchmarkRWMutexWrite100-16 371.00 152.00 -59.03% BenchmarkRWMutexWrite10 78.30 29.80 -61.94% BenchmarkRWMutexWrite10-2 348.00 165.00 -52.59% BenchmarkRWMutexWrite10-4 447.00 199.00 -55.48% BenchmarkRWMutexWrite10-8 564.00 180.00 -68.09% BenchmarkRWMutexWrite10-16 492.00 192.00 -60.98% BenchmarkRWMutexWorkWrite100 1077.00 1037.00 -3.71% BenchmarkRWMutexWorkWrite100-2 659.00 596.00 -9.56% BenchmarkRWMutexWorkWrite100-4 509.00 361.00 -29.08% BenchmarkRWMutexWorkWrite100-8 603.00 351.00 -41.79% BenchmarkRWMutexWorkWrite100-16 750.00 607.00 -19.07% BenchmarkRWMutexWorkWrite10 990.00 951.00 -3.94% BenchmarkRWMutexWorkWrite10-2 1119.00 1070.00 -4.38% BenchmarkRWMutexWorkWrite10-4 1300.00 1199.00 -7.77% BenchmarkRWMutexWorkWrite10-8 1424.00 1291.00 -9.34% BenchmarkRWMutexWorkWrite10-16 1981.00 1786.00 -9.84% R=rsc CC=golang-dev https://golang.org/cl/4671051
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
}
sync: improve RWMutex performance The new implementation features wait-free fast path for readers which significantly improves performance/scalability on read-mostly workloads. Benchmark results on HP Z600 (2 x Xeon E5620, 8 HT cores, 2.40GHz) are as follows: benchmark old ns/op new ns/op delta BenchmarkRWMutexUncontended 179.00 96.60 -46.03% BenchmarkRWMutexUncontended-2 89.10 49.10 -44.89% BenchmarkRWMutexUncontended-4 44.70 24.70 -44.74% BenchmarkRWMutexUncontended-8 23.30 12.90 -44.64% BenchmarkRWMutexUncontended-16 16.80 8.75 -47.92% BenchmarkRWMutexWrite100 79.60 26.80 -66.33% BenchmarkRWMutexWrite100-2 305.00 33.00 -89.18% BenchmarkRWMutexWrite100-4 245.00 113.00 -53.88% BenchmarkRWMutexWrite100-8 330.00 147.00 -55.45% BenchmarkRWMutexWrite100-16 371.00 152.00 -59.03% BenchmarkRWMutexWrite10 78.30 29.80 -61.94% BenchmarkRWMutexWrite10-2 348.00 165.00 -52.59% BenchmarkRWMutexWrite10-4 447.00 199.00 -55.48% BenchmarkRWMutexWrite10-8 564.00 180.00 -68.09% BenchmarkRWMutexWrite10-16 492.00 192.00 -60.98% BenchmarkRWMutexWorkWrite100 1077.00 1037.00 -3.71% BenchmarkRWMutexWorkWrite100-2 659.00 596.00 -9.56% BenchmarkRWMutexWorkWrite100-4 509.00 361.00 -29.08% BenchmarkRWMutexWorkWrite100-8 603.00 351.00 -41.79% BenchmarkRWMutexWorkWrite100-16 750.00 607.00 -19.07% BenchmarkRWMutexWorkWrite10 990.00 951.00 -3.94% BenchmarkRWMutexWorkWrite10-2 1119.00 1070.00 -4.38% BenchmarkRWMutexWorkWrite10-4 1300.00 1199.00 -7.77% BenchmarkRWMutexWorkWrite10-8 1424.00 1291.00 -9.34% BenchmarkRWMutexWorkWrite10-16 1981.00 1786.00 -9.84% R=rsc CC=golang-dev https://golang.org/cl/4671051
2011-07-12 10:24:21 -06:00
const rwmutexMaxReaders = 1 << 30
// RLock locks rw for reading.
func (rw *RWMutex) RLock() {
if race.Enabled {
_ = rw.w.state
race.Disable()
}
sync: improve RWMutex performance The new implementation features wait-free fast path for readers which significantly improves performance/scalability on read-mostly workloads. Benchmark results on HP Z600 (2 x Xeon E5620, 8 HT cores, 2.40GHz) are as follows: benchmark old ns/op new ns/op delta BenchmarkRWMutexUncontended 179.00 96.60 -46.03% BenchmarkRWMutexUncontended-2 89.10 49.10 -44.89% BenchmarkRWMutexUncontended-4 44.70 24.70 -44.74% BenchmarkRWMutexUncontended-8 23.30 12.90 -44.64% BenchmarkRWMutexUncontended-16 16.80 8.75 -47.92% BenchmarkRWMutexWrite100 79.60 26.80 -66.33% BenchmarkRWMutexWrite100-2 305.00 33.00 -89.18% BenchmarkRWMutexWrite100-4 245.00 113.00 -53.88% BenchmarkRWMutexWrite100-8 330.00 147.00 -55.45% BenchmarkRWMutexWrite100-16 371.00 152.00 -59.03% BenchmarkRWMutexWrite10 78.30 29.80 -61.94% BenchmarkRWMutexWrite10-2 348.00 165.00 -52.59% BenchmarkRWMutexWrite10-4 447.00 199.00 -55.48% BenchmarkRWMutexWrite10-8 564.00 180.00 -68.09% BenchmarkRWMutexWrite10-16 492.00 192.00 -60.98% BenchmarkRWMutexWorkWrite100 1077.00 1037.00 -3.71% BenchmarkRWMutexWorkWrite100-2 659.00 596.00 -9.56% BenchmarkRWMutexWorkWrite100-4 509.00 361.00 -29.08% BenchmarkRWMutexWorkWrite100-8 603.00 351.00 -41.79% BenchmarkRWMutexWorkWrite100-16 750.00 607.00 -19.07% BenchmarkRWMutexWorkWrite10 990.00 951.00 -3.94% BenchmarkRWMutexWorkWrite10-2 1119.00 1070.00 -4.38% BenchmarkRWMutexWorkWrite10-4 1300.00 1199.00 -7.77% BenchmarkRWMutexWorkWrite10-8 1424.00 1291.00 -9.34% BenchmarkRWMutexWorkWrite10-16 1981.00 1786.00 -9.84% R=rsc CC=golang-dev https://golang.org/cl/4671051
2011-07-12 10:24:21 -06:00
if atomic.AddInt32(&rw.readerCount, 1) < 0 {
// A writer is pending, wait for it.
runtime_Semacquire(&rw.readerSem)
}
if race.Enabled {
race.Enable()
race.Acquire(unsafe.Pointer(&rw.readerSem))
}
}
// 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() {
if race.Enabled {
_ = rw.w.state
race.ReleaseMerge(unsafe.Pointer(&rw.writerSem))
race.Disable()
}
if r := atomic.AddInt32(&rw.readerCount, -1); r < 0 {
if r+1 == 0 || r+1 == -rwmutexMaxReaders {
race.Enable()
panic("sync: RUnlock of unlocked RWMutex")
}
sync: improve RWMutex performance The new implementation features wait-free fast path for readers which significantly improves performance/scalability on read-mostly workloads. Benchmark results on HP Z600 (2 x Xeon E5620, 8 HT cores, 2.40GHz) are as follows: benchmark old ns/op new ns/op delta BenchmarkRWMutexUncontended 179.00 96.60 -46.03% BenchmarkRWMutexUncontended-2 89.10 49.10 -44.89% BenchmarkRWMutexUncontended-4 44.70 24.70 -44.74% BenchmarkRWMutexUncontended-8 23.30 12.90 -44.64% BenchmarkRWMutexUncontended-16 16.80 8.75 -47.92% BenchmarkRWMutexWrite100 79.60 26.80 -66.33% BenchmarkRWMutexWrite100-2 305.00 33.00 -89.18% BenchmarkRWMutexWrite100-4 245.00 113.00 -53.88% BenchmarkRWMutexWrite100-8 330.00 147.00 -55.45% BenchmarkRWMutexWrite100-16 371.00 152.00 -59.03% BenchmarkRWMutexWrite10 78.30 29.80 -61.94% BenchmarkRWMutexWrite10-2 348.00 165.00 -52.59% BenchmarkRWMutexWrite10-4 447.00 199.00 -55.48% BenchmarkRWMutexWrite10-8 564.00 180.00 -68.09% BenchmarkRWMutexWrite10-16 492.00 192.00 -60.98% BenchmarkRWMutexWorkWrite100 1077.00 1037.00 -3.71% BenchmarkRWMutexWorkWrite100-2 659.00 596.00 -9.56% BenchmarkRWMutexWorkWrite100-4 509.00 361.00 -29.08% BenchmarkRWMutexWorkWrite100-8 603.00 351.00 -41.79% BenchmarkRWMutexWorkWrite100-16 750.00 607.00 -19.07% BenchmarkRWMutexWorkWrite10 990.00 951.00 -3.94% BenchmarkRWMutexWorkWrite10-2 1119.00 1070.00 -4.38% BenchmarkRWMutexWorkWrite10-4 1300.00 1199.00 -7.77% BenchmarkRWMutexWorkWrite10-8 1424.00 1291.00 -9.34% BenchmarkRWMutexWorkWrite10-16 1981.00 1786.00 -9.84% R=rsc CC=golang-dev https://golang.org/cl/4671051
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.
runtime_Semrelease(&rw.writerSem)
sync: improve RWMutex performance The new implementation features wait-free fast path for readers which significantly improves performance/scalability on read-mostly workloads. Benchmark results on HP Z600 (2 x Xeon E5620, 8 HT cores, 2.40GHz) are as follows: benchmark old ns/op new ns/op delta BenchmarkRWMutexUncontended 179.00 96.60 -46.03% BenchmarkRWMutexUncontended-2 89.10 49.10 -44.89% BenchmarkRWMutexUncontended-4 44.70 24.70 -44.74% BenchmarkRWMutexUncontended-8 23.30 12.90 -44.64% BenchmarkRWMutexUncontended-16 16.80 8.75 -47.92% BenchmarkRWMutexWrite100 79.60 26.80 -66.33% BenchmarkRWMutexWrite100-2 305.00 33.00 -89.18% BenchmarkRWMutexWrite100-4 245.00 113.00 -53.88% BenchmarkRWMutexWrite100-8 330.00 147.00 -55.45% BenchmarkRWMutexWrite100-16 371.00 152.00 -59.03% BenchmarkRWMutexWrite10 78.30 29.80 -61.94% BenchmarkRWMutexWrite10-2 348.00 165.00 -52.59% BenchmarkRWMutexWrite10-4 447.00 199.00 -55.48% BenchmarkRWMutexWrite10-8 564.00 180.00 -68.09% BenchmarkRWMutexWrite10-16 492.00 192.00 -60.98% BenchmarkRWMutexWorkWrite100 1077.00 1037.00 -3.71% BenchmarkRWMutexWorkWrite100-2 659.00 596.00 -9.56% BenchmarkRWMutexWorkWrite100-4 509.00 361.00 -29.08% BenchmarkRWMutexWorkWrite100-8 603.00 351.00 -41.79% BenchmarkRWMutexWorkWrite100-16 750.00 607.00 -19.07% BenchmarkRWMutexWorkWrite10 990.00 951.00 -3.94% BenchmarkRWMutexWorkWrite10-2 1119.00 1070.00 -4.38% BenchmarkRWMutexWorkWrite10-4 1300.00 1199.00 -7.77% BenchmarkRWMutexWorkWrite10-8 1424.00 1291.00 -9.34% BenchmarkRWMutexWorkWrite10-16 1981.00 1786.00 -9.84% R=rsc CC=golang-dev https://golang.org/cl/4671051
2011-07-12 10:24:21 -06:00
}
}
if race.Enabled {
race.Enable()
}
}
// Lock locks rw for writing.
// If the lock is already locked for reading or writing,
// Lock blocks until the lock is available.
func (rw *RWMutex) Lock() {
if race.Enabled {
_ = rw.w.state
race.Disable()
}
sync: improve RWMutex performance The new implementation features wait-free fast path for readers which significantly improves performance/scalability on read-mostly workloads. Benchmark results on HP Z600 (2 x Xeon E5620, 8 HT cores, 2.40GHz) are as follows: benchmark old ns/op new ns/op delta BenchmarkRWMutexUncontended 179.00 96.60 -46.03% BenchmarkRWMutexUncontended-2 89.10 49.10 -44.89% BenchmarkRWMutexUncontended-4 44.70 24.70 -44.74% BenchmarkRWMutexUncontended-8 23.30 12.90 -44.64% BenchmarkRWMutexUncontended-16 16.80 8.75 -47.92% BenchmarkRWMutexWrite100 79.60 26.80 -66.33% BenchmarkRWMutexWrite100-2 305.00 33.00 -89.18% BenchmarkRWMutexWrite100-4 245.00 113.00 -53.88% BenchmarkRWMutexWrite100-8 330.00 147.00 -55.45% BenchmarkRWMutexWrite100-16 371.00 152.00 -59.03% BenchmarkRWMutexWrite10 78.30 29.80 -61.94% BenchmarkRWMutexWrite10-2 348.00 165.00 -52.59% BenchmarkRWMutexWrite10-4 447.00 199.00 -55.48% BenchmarkRWMutexWrite10-8 564.00 180.00 -68.09% BenchmarkRWMutexWrite10-16 492.00 192.00 -60.98% BenchmarkRWMutexWorkWrite100 1077.00 1037.00 -3.71% BenchmarkRWMutexWorkWrite100-2 659.00 596.00 -9.56% BenchmarkRWMutexWorkWrite100-4 509.00 361.00 -29.08% BenchmarkRWMutexWorkWrite100-8 603.00 351.00 -41.79% BenchmarkRWMutexWorkWrite100-16 750.00 607.00 -19.07% BenchmarkRWMutexWorkWrite10 990.00 951.00 -3.94% BenchmarkRWMutexWorkWrite10-2 1119.00 1070.00 -4.38% BenchmarkRWMutexWorkWrite10-4 1300.00 1199.00 -7.77% BenchmarkRWMutexWorkWrite10-8 1424.00 1291.00 -9.34% BenchmarkRWMutexWorkWrite10-16 1981.00 1786.00 -9.84% R=rsc CC=golang-dev https://golang.org/cl/4671051
2011-07-12 10:24:21 -06:00
// First, resolve competition with other writers.
rw.w.Lock()
sync: improve RWMutex performance The new implementation features wait-free fast path for readers which significantly improves performance/scalability on read-mostly workloads. Benchmark results on HP Z600 (2 x Xeon E5620, 8 HT cores, 2.40GHz) are as follows: benchmark old ns/op new ns/op delta BenchmarkRWMutexUncontended 179.00 96.60 -46.03% BenchmarkRWMutexUncontended-2 89.10 49.10 -44.89% BenchmarkRWMutexUncontended-4 44.70 24.70 -44.74% BenchmarkRWMutexUncontended-8 23.30 12.90 -44.64% BenchmarkRWMutexUncontended-16 16.80 8.75 -47.92% BenchmarkRWMutexWrite100 79.60 26.80 -66.33% BenchmarkRWMutexWrite100-2 305.00 33.00 -89.18% BenchmarkRWMutexWrite100-4 245.00 113.00 -53.88% BenchmarkRWMutexWrite100-8 330.00 147.00 -55.45% BenchmarkRWMutexWrite100-16 371.00 152.00 -59.03% BenchmarkRWMutexWrite10 78.30 29.80 -61.94% BenchmarkRWMutexWrite10-2 348.00 165.00 -52.59% BenchmarkRWMutexWrite10-4 447.00 199.00 -55.48% BenchmarkRWMutexWrite10-8 564.00 180.00 -68.09% BenchmarkRWMutexWrite10-16 492.00 192.00 -60.98% BenchmarkRWMutexWorkWrite100 1077.00 1037.00 -3.71% BenchmarkRWMutexWorkWrite100-2 659.00 596.00 -9.56% BenchmarkRWMutexWorkWrite100-4 509.00 361.00 -29.08% BenchmarkRWMutexWorkWrite100-8 603.00 351.00 -41.79% BenchmarkRWMutexWorkWrite100-16 750.00 607.00 -19.07% BenchmarkRWMutexWorkWrite10 990.00 951.00 -3.94% BenchmarkRWMutexWorkWrite10-2 1119.00 1070.00 -4.38% BenchmarkRWMutexWorkWrite10-4 1300.00 1199.00 -7.77% BenchmarkRWMutexWorkWrite10-8 1424.00 1291.00 -9.34% BenchmarkRWMutexWorkWrite10-16 1981.00 1786.00 -9.84% R=rsc CC=golang-dev https://golang.org/cl/4671051
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 {
runtime_Semacquire(&rw.writerSem)
sync: improve RWMutex performance The new implementation features wait-free fast path for readers which significantly improves performance/scalability on read-mostly workloads. Benchmark results on HP Z600 (2 x Xeon E5620, 8 HT cores, 2.40GHz) are as follows: benchmark old ns/op new ns/op delta BenchmarkRWMutexUncontended 179.00 96.60 -46.03% BenchmarkRWMutexUncontended-2 89.10 49.10 -44.89% BenchmarkRWMutexUncontended-4 44.70 24.70 -44.74% BenchmarkRWMutexUncontended-8 23.30 12.90 -44.64% BenchmarkRWMutexUncontended-16 16.80 8.75 -47.92% BenchmarkRWMutexWrite100 79.60 26.80 -66.33% BenchmarkRWMutexWrite100-2 305.00 33.00 -89.18% BenchmarkRWMutexWrite100-4 245.00 113.00 -53.88% BenchmarkRWMutexWrite100-8 330.00 147.00 -55.45% BenchmarkRWMutexWrite100-16 371.00 152.00 -59.03% BenchmarkRWMutexWrite10 78.30 29.80 -61.94% BenchmarkRWMutexWrite10-2 348.00 165.00 -52.59% BenchmarkRWMutexWrite10-4 447.00 199.00 -55.48% BenchmarkRWMutexWrite10-8 564.00 180.00 -68.09% BenchmarkRWMutexWrite10-16 492.00 192.00 -60.98% BenchmarkRWMutexWorkWrite100 1077.00 1037.00 -3.71% BenchmarkRWMutexWorkWrite100-2 659.00 596.00 -9.56% BenchmarkRWMutexWorkWrite100-4 509.00 361.00 -29.08% BenchmarkRWMutexWorkWrite100-8 603.00 351.00 -41.79% BenchmarkRWMutexWorkWrite100-16 750.00 607.00 -19.07% BenchmarkRWMutexWorkWrite10 990.00 951.00 -3.94% BenchmarkRWMutexWorkWrite10-2 1119.00 1070.00 -4.38% BenchmarkRWMutexWorkWrite10-4 1300.00 1199.00 -7.77% BenchmarkRWMutexWorkWrite10-8 1424.00 1291.00 -9.34% BenchmarkRWMutexWorkWrite10-16 1981.00 1786.00 -9.84% R=rsc CC=golang-dev https://golang.org/cl/4671051
2011-07-12 10:24:21 -06:00
}
if race.Enabled {
race.Enable()
race.Acquire(unsafe.Pointer(&rw.readerSem))
race.Acquire(unsafe.Pointer(&rw.writerSem))
}
}
// Unlock unlocks rw for writing. It is a run-time error if rw is
// not locked for writing on entry to Unlock.
//
// As with Mutexes, a locked RWMutex is not associated with a particular
// goroutine. One goroutine may RLock (Lock) an RWMutex and then
// arrange for another goroutine to RUnlock (Unlock) it.
sync: improve RWMutex performance The new implementation features wait-free fast path for readers which significantly improves performance/scalability on read-mostly workloads. Benchmark results on HP Z600 (2 x Xeon E5620, 8 HT cores, 2.40GHz) are as follows: benchmark old ns/op new ns/op delta BenchmarkRWMutexUncontended 179.00 96.60 -46.03% BenchmarkRWMutexUncontended-2 89.10 49.10 -44.89% BenchmarkRWMutexUncontended-4 44.70 24.70 -44.74% BenchmarkRWMutexUncontended-8 23.30 12.90 -44.64% BenchmarkRWMutexUncontended-16 16.80 8.75 -47.92% BenchmarkRWMutexWrite100 79.60 26.80 -66.33% BenchmarkRWMutexWrite100-2 305.00 33.00 -89.18% BenchmarkRWMutexWrite100-4 245.00 113.00 -53.88% BenchmarkRWMutexWrite100-8 330.00 147.00 -55.45% BenchmarkRWMutexWrite100-16 371.00 152.00 -59.03% BenchmarkRWMutexWrite10 78.30 29.80 -61.94% BenchmarkRWMutexWrite10-2 348.00 165.00 -52.59% BenchmarkRWMutexWrite10-4 447.00 199.00 -55.48% BenchmarkRWMutexWrite10-8 564.00 180.00 -68.09% BenchmarkRWMutexWrite10-16 492.00 192.00 -60.98% BenchmarkRWMutexWorkWrite100 1077.00 1037.00 -3.71% BenchmarkRWMutexWorkWrite100-2 659.00 596.00 -9.56% BenchmarkRWMutexWorkWrite100-4 509.00 361.00 -29.08% BenchmarkRWMutexWorkWrite100-8 603.00 351.00 -41.79% BenchmarkRWMutexWorkWrite100-16 750.00 607.00 -19.07% BenchmarkRWMutexWorkWrite10 990.00 951.00 -3.94% BenchmarkRWMutexWorkWrite10-2 1119.00 1070.00 -4.38% BenchmarkRWMutexWorkWrite10-4 1300.00 1199.00 -7.77% BenchmarkRWMutexWorkWrite10-8 1424.00 1291.00 -9.34% BenchmarkRWMutexWorkWrite10-16 1981.00 1786.00 -9.84% R=rsc CC=golang-dev https://golang.org/cl/4671051
2011-07-12 10:24:21 -06:00
func (rw *RWMutex) Unlock() {
if race.Enabled {
_ = rw.w.state
race.Release(unsafe.Pointer(&rw.readerSem))
race.Release(unsafe.Pointer(&rw.writerSem))
race.Disable()
}
sync: improve RWMutex performance The new implementation features wait-free fast path for readers which significantly improves performance/scalability on read-mostly workloads. Benchmark results on HP Z600 (2 x Xeon E5620, 8 HT cores, 2.40GHz) are as follows: benchmark old ns/op new ns/op delta BenchmarkRWMutexUncontended 179.00 96.60 -46.03% BenchmarkRWMutexUncontended-2 89.10 49.10 -44.89% BenchmarkRWMutexUncontended-4 44.70 24.70 -44.74% BenchmarkRWMutexUncontended-8 23.30 12.90 -44.64% BenchmarkRWMutexUncontended-16 16.80 8.75 -47.92% BenchmarkRWMutexWrite100 79.60 26.80 -66.33% BenchmarkRWMutexWrite100-2 305.00 33.00 -89.18% BenchmarkRWMutexWrite100-4 245.00 113.00 -53.88% BenchmarkRWMutexWrite100-8 330.00 147.00 -55.45% BenchmarkRWMutexWrite100-16 371.00 152.00 -59.03% BenchmarkRWMutexWrite10 78.30 29.80 -61.94% BenchmarkRWMutexWrite10-2 348.00 165.00 -52.59% BenchmarkRWMutexWrite10-4 447.00 199.00 -55.48% BenchmarkRWMutexWrite10-8 564.00 180.00 -68.09% BenchmarkRWMutexWrite10-16 492.00 192.00 -60.98% BenchmarkRWMutexWorkWrite100 1077.00 1037.00 -3.71% BenchmarkRWMutexWorkWrite100-2 659.00 596.00 -9.56% BenchmarkRWMutexWorkWrite100-4 509.00 361.00 -29.08% BenchmarkRWMutexWorkWrite100-8 603.00 351.00 -41.79% BenchmarkRWMutexWorkWrite100-16 750.00 607.00 -19.07% BenchmarkRWMutexWorkWrite10 990.00 951.00 -3.94% BenchmarkRWMutexWorkWrite10-2 1119.00 1070.00 -4.38% BenchmarkRWMutexWorkWrite10-4 1300.00 1199.00 -7.77% BenchmarkRWMutexWorkWrite10-8 1424.00 1291.00 -9.34% BenchmarkRWMutexWorkWrite10-16 1981.00 1786.00 -9.84% R=rsc CC=golang-dev https://golang.org/cl/4671051
2011-07-12 10:24:21 -06:00
// Announce to readers there is no active writer.
r := atomic.AddInt32(&rw.readerCount, rwmutexMaxReaders)
if r >= rwmutexMaxReaders {
race.Enable()
panic("sync: Unlock of unlocked RWMutex")
}
sync: improve RWMutex performance The new implementation features wait-free fast path for readers which significantly improves performance/scalability on read-mostly workloads. Benchmark results on HP Z600 (2 x Xeon E5620, 8 HT cores, 2.40GHz) are as follows: benchmark old ns/op new ns/op delta BenchmarkRWMutexUncontended 179.00 96.60 -46.03% BenchmarkRWMutexUncontended-2 89.10 49.10 -44.89% BenchmarkRWMutexUncontended-4 44.70 24.70 -44.74% BenchmarkRWMutexUncontended-8 23.30 12.90 -44.64% BenchmarkRWMutexUncontended-16 16.80 8.75 -47.92% BenchmarkRWMutexWrite100 79.60 26.80 -66.33% BenchmarkRWMutexWrite100-2 305.00 33.00 -89.18% BenchmarkRWMutexWrite100-4 245.00 113.00 -53.88% BenchmarkRWMutexWrite100-8 330.00 147.00 -55.45% BenchmarkRWMutexWrite100-16 371.00 152.00 -59.03% BenchmarkRWMutexWrite10 78.30 29.80 -61.94% BenchmarkRWMutexWrite10-2 348.00 165.00 -52.59% BenchmarkRWMutexWrite10-4 447.00 199.00 -55.48% BenchmarkRWMutexWrite10-8 564.00 180.00 -68.09% BenchmarkRWMutexWrite10-16 492.00 192.00 -60.98% BenchmarkRWMutexWorkWrite100 1077.00 1037.00 -3.71% BenchmarkRWMutexWorkWrite100-2 659.00 596.00 -9.56% BenchmarkRWMutexWorkWrite100-4 509.00 361.00 -29.08% BenchmarkRWMutexWorkWrite100-8 603.00 351.00 -41.79% BenchmarkRWMutexWorkWrite100-16 750.00 607.00 -19.07% BenchmarkRWMutexWorkWrite10 990.00 951.00 -3.94% BenchmarkRWMutexWorkWrite10-2 1119.00 1070.00 -4.38% BenchmarkRWMutexWorkWrite10-4 1300.00 1199.00 -7.77% BenchmarkRWMutexWorkWrite10-8 1424.00 1291.00 -9.34% BenchmarkRWMutexWorkWrite10-16 1981.00 1786.00 -9.84% R=rsc CC=golang-dev https://golang.org/cl/4671051
2011-07-12 10:24:21 -06:00
// Unblock blocked readers, if any.
for i := 0; i < int(r); i++ {
runtime_Semrelease(&rw.readerSem)
sync: improve RWMutex performance The new implementation features wait-free fast path for readers which significantly improves performance/scalability on read-mostly workloads. Benchmark results on HP Z600 (2 x Xeon E5620, 8 HT cores, 2.40GHz) are as follows: benchmark old ns/op new ns/op delta BenchmarkRWMutexUncontended 179.00 96.60 -46.03% BenchmarkRWMutexUncontended-2 89.10 49.10 -44.89% BenchmarkRWMutexUncontended-4 44.70 24.70 -44.74% BenchmarkRWMutexUncontended-8 23.30 12.90 -44.64% BenchmarkRWMutexUncontended-16 16.80 8.75 -47.92% BenchmarkRWMutexWrite100 79.60 26.80 -66.33% BenchmarkRWMutexWrite100-2 305.00 33.00 -89.18% BenchmarkRWMutexWrite100-4 245.00 113.00 -53.88% BenchmarkRWMutexWrite100-8 330.00 147.00 -55.45% BenchmarkRWMutexWrite100-16 371.00 152.00 -59.03% BenchmarkRWMutexWrite10 78.30 29.80 -61.94% BenchmarkRWMutexWrite10-2 348.00 165.00 -52.59% BenchmarkRWMutexWrite10-4 447.00 199.00 -55.48% BenchmarkRWMutexWrite10-8 564.00 180.00 -68.09% BenchmarkRWMutexWrite10-16 492.00 192.00 -60.98% BenchmarkRWMutexWorkWrite100 1077.00 1037.00 -3.71% BenchmarkRWMutexWorkWrite100-2 659.00 596.00 -9.56% BenchmarkRWMutexWorkWrite100-4 509.00 361.00 -29.08% BenchmarkRWMutexWorkWrite100-8 603.00 351.00 -41.79% BenchmarkRWMutexWorkWrite100-16 750.00 607.00 -19.07% BenchmarkRWMutexWorkWrite10 990.00 951.00 -3.94% BenchmarkRWMutexWorkWrite10-2 1119.00 1070.00 -4.38% BenchmarkRWMutexWorkWrite10-4 1300.00 1199.00 -7.77% BenchmarkRWMutexWorkWrite10-8 1424.00 1291.00 -9.34% BenchmarkRWMutexWorkWrite10-16 1981.00 1786.00 -9.84% R=rsc CC=golang-dev https://golang.org/cl/4671051
2011-07-12 10:24:21 -06:00
}
// Allow other writers to proceed.
rw.w.Unlock()
if race.Enabled {
race.Enable()
}
sync: improve RWMutex performance The new implementation features wait-free fast path for readers which significantly improves performance/scalability on read-mostly workloads. Benchmark results on HP Z600 (2 x Xeon E5620, 8 HT cores, 2.40GHz) are as follows: benchmark old ns/op new ns/op delta BenchmarkRWMutexUncontended 179.00 96.60 -46.03% BenchmarkRWMutexUncontended-2 89.10 49.10 -44.89% BenchmarkRWMutexUncontended-4 44.70 24.70 -44.74% BenchmarkRWMutexUncontended-8 23.30 12.90 -44.64% BenchmarkRWMutexUncontended-16 16.80 8.75 -47.92% BenchmarkRWMutexWrite100 79.60 26.80 -66.33% BenchmarkRWMutexWrite100-2 305.00 33.00 -89.18% BenchmarkRWMutexWrite100-4 245.00 113.00 -53.88% BenchmarkRWMutexWrite100-8 330.00 147.00 -55.45% BenchmarkRWMutexWrite100-16 371.00 152.00 -59.03% BenchmarkRWMutexWrite10 78.30 29.80 -61.94% BenchmarkRWMutexWrite10-2 348.00 165.00 -52.59% BenchmarkRWMutexWrite10-4 447.00 199.00 -55.48% BenchmarkRWMutexWrite10-8 564.00 180.00 -68.09% BenchmarkRWMutexWrite10-16 492.00 192.00 -60.98% BenchmarkRWMutexWorkWrite100 1077.00 1037.00 -3.71% BenchmarkRWMutexWorkWrite100-2 659.00 596.00 -9.56% BenchmarkRWMutexWorkWrite100-4 509.00 361.00 -29.08% BenchmarkRWMutexWorkWrite100-8 603.00 351.00 -41.79% BenchmarkRWMutexWorkWrite100-16 750.00 607.00 -19.07% BenchmarkRWMutexWorkWrite10 990.00 951.00 -3.94% BenchmarkRWMutexWorkWrite10-2 1119.00 1070.00 -4.38% BenchmarkRWMutexWorkWrite10-4 1300.00 1199.00 -7.77% BenchmarkRWMutexWorkWrite10-8 1424.00 1291.00 -9.34% BenchmarkRWMutexWorkWrite10-16 1981.00 1786.00 -9.84% R=rsc CC=golang-dev https://golang.org/cl/4671051
2011-07-12 10:24:21 -06: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() }