Commit 6fb89cf0c5 for golang
commit 6fb89cf0c57de68949e510471368b5d5ea797867
Author: Liz Fong-Jones <lizf@honeycomb.io>
Date: Sat Nov 28 15:25:26 2026 -0700
sync: hand off the P to the writer woken by RUnlock
When the last reader's RUnlock wakes a pending writer, the writer
already owns the lock: new readers park behind it and other writers
queue on rw.w. But it is readied into runnext on the reader's P, and
if the reader keeps running, the writer waits to be stolen, behind
the 3µs sleep in runqgrab (about 56µs with Linux's default timer
slack). The lock is held, and idle, for that long.
Pass handoff=true so the reader yields its P to the writer, as
Mutex.Unlock does in starvation mode (CL 206180). Unlike a normal-mode
Mutex, nothing can take the lock while the reader yields.
On a 32 vCPU Graviton5 with GOMAXPROCS=22, an open-loop workload that
write-locks an RWMutex in 20% of its 100µs spans stalls above 168k
spans/s (77% CPU); with this change it runs to 210k (96% CPU).
Write-lock wait p99 at 140k spans/s goes from 655µs to 2.6µs.
With Sarama and franz-go producers in front of such a lock on 12
cores, go1.27.3 falls into the convoy at 185k and 151k spans/s, at
73-76% and 58-61% CPU. With this change there is no convoy up to
240k, where the CPU runs out. CPU per span is unchanged.
net/http ClientServerParallel and database/sql Concurrent* are
unchanged (geomean -0.15% and -0.05%). Benchmarks with one goroutine
per P and frequent writes regress: under 1% of their writes contend,
the reader's next RLock was already handing the P over, and the yield
now makes an idle P wake to take the reader.
│ stock │ handoff │
│ sec/op │ sec/op vs base │
RWMutexWrite100-22 35.05n ± 2% 34.29n ± 2% -2.17% (p=0.012 n=10)
RWMutexWrite10-22 20.80n ± 1% 33.89n ± 2% +62.89% (p=0.000 n=10)
RWMutexWorkWrite100-22 161.1n ± 1% 159.5n ± 1% -1.02% (p=0.003 n=10)
RWMutexWorkWrite10-22 166.1n ± 0% 196.3n ± 1% +18.15% (p=0.000 n=10)
Fixes #83472
Change-Id: I524c5b88d538be6ca871f8ecaf1b3e902c278072
Reviewed-on: https://go-review.googlesource.com/c/go/+/868181
Reviewed-by: Liz Fong-Jones <lizf@honeycomb.io>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
diff --git a/src/sync/rwmutex.go b/src/sync/rwmutex.go
index 3fbfd28d7f..1bf0b39ee7 100644
--- a/src/sync/rwmutex.go
+++ b/src/sync/rwmutex.go
@@ -133,8 +133,11 @@ func (rw *RWMutex) rUnlockSlow(r int32) {
}
// A writer is pending.
if rw.readerWait.Add(-1) == 0 {
- // The last reader unblocks the writer.
- runtime_Semrelease(&rw.writerSem, false, 1)
+ // The last reader unblocks the writer. The writer already
+ // holds the lock, and every other reader and writer is
+ // waiting behind it, so hand it this P instead of leaving
+ // it in runnext to be stolen.
+ runtime_Semrelease(&rw.writerSem, true, 1)
}
}
diff --git a/src/sync/rwmutex_test.go b/src/sync/rwmutex_test.go
index dfbdd9bbee..5df07416bd 100644
--- a/src/sync/rwmutex_test.go
+++ b/src/sync/rwmutex_test.go
@@ -153,6 +153,34 @@ func TestRWMutex(t *testing.T) {
HammerRWMutex(10, 5, n)
}
+// TestRWMutexWriterHandoff checks that the RUnlock that releases a waiting
+// writer runs that writer before returning. The writer already owns the lock
+// at that point, and every other reader and writer is queued behind it.
+func TestRWMutexWriterHandoff(t *testing.T) {
+ defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
+ var rw RWMutex
+ var locked atomic.Bool
+ done := make(chan bool)
+ rw.RLock()
+ go func() {
+ rw.Lock()
+ locked.Store(true)
+ rw.Unlock()
+ done <- true
+ }()
+ // Wait for the writer to announce itself, then let it park.
+ for rw.TryRLock() {
+ rw.RUnlock()
+ runtime.Gosched()
+ }
+ runtime.Gosched()
+ rw.RUnlock()
+ if !locked.Load() {
+ t.Error("writer did not run before RUnlock returned")
+ }
+ <-done
+}
+
func TestRLocker(t *testing.T) {
var wl RWMutex
var rl Locker