aboutsummaryrefslogtreecommitdiff
path: root/src/sync
diff options
context:
space:
mode:
authorCarlo Alberto Ferraris <cafxx@strayorange.com>2019-10-02 19:15:53 +0900
committerIan Lance Taylor <iant@golang.org>2019-11-07 05:59:33 +0000
commita8f57f4adad2122b42ea05024b61e93442788289 (patch)
treee67255dc5bf1aebc30f7ffdaccf1c80980474ee8 /src/sync
parent3eabdd291d3f53c88fdd01aca30158d0c06420b6 (diff)
downloadgo-a8f57f4adad2122b42ea05024b61e93442788289.tar.gz
go-a8f57f4adad2122b42ea05024b61e93442788289.zip
sync: yield to the waiter when unlocking a starving mutex
When we have already assigned the semaphore ticket to a specific waiter, we want to get the waiter running as fast as possible since no other G waiting on the semaphore can acquire it optimistically. The net effect is that, when a sync.Mutex is contented, the code in the critical section guarded by the Mutex gets a priority boost. Fixes #33747 Change-Id: I9967f0f763c25504010651bdd7f944ee0189cd45 Reviewed-on: https://go-review.googlesource.com/c/go/+/200577 Reviewed-by: Rhys Hiltner <rhys@justin.tv> Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/sync')
-rw-r--r--src/sync/mutex.go3
1 files changed, 2 insertions, 1 deletions
diff --git a/src/sync/mutex.go b/src/sync/mutex.go
index 11ad20c975..3028552f74 100644
--- a/src/sync/mutex.go
+++ b/src/sync/mutex.go
@@ -216,7 +216,8 @@ func (m *Mutex) unlockSlow(new int32) {
old = m.state
}
} else {
- // Starving mode: handoff mutex ownership to the next waiter.
+ // Starving mode: handoff mutex ownership to the next waiter, and yield
+ // our time slice so that the next waiter can start to run immediately.
// Note: mutexLocked is not set, the waiter will set it after wakeup.
// But mutex is still considered locked if mutexStarving is set,
// so new coming goroutines won't acquire it.