[go] runtime: convert notifyList.wait to atomic type

6 views
Skip to first unread message

Michael Knyszek (Gerrit)

unread,
Aug 26, 2022, 11:36:10 AM8/26/22
to hopehook, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Michael Pratt, Gopher Robot, golang-co...@googlegroups.com

Michael Knyszek submitted this change.

View Change


Approvals: Michael Pratt: Looks good to me, approved Michael Knyszek: Looks good to me, approved hopehook: Run TryBots Gopher Robot: TryBots succeeded
runtime: convert notifyList.wait to atomic type

For #53821

Change-Id: Ib096332fe6111bbcd2f5c4cbb29c2fef7a808e7e
Reviewed-on: https://go-review.googlesource.com/c/go/+/425784
Run-TryBot: hopehook <hope...@qq.com>
TryBot-Result: Gopher Robot <go...@golang.org>
Reviewed-by: Michael Pratt <mpr...@google.com>
Reviewed-by: Michael Knyszek <mkny...@google.com>
---
M src/runtime/sema.go
1 file changed, 22 insertions(+), 6 deletions(-)

diff --git a/src/runtime/sema.go b/src/runtime/sema.go
index 39935f7..4b965ea 100644
--- a/src/runtime/sema.go
+++ b/src/runtime/sema.go
@@ -451,7 +451,7 @@
type notifyList struct {
// wait is the ticket number of the next waiter. It is atomically
// incremented outside the lock.
- wait uint32
+ wait atomic.Uint32

// notify is the ticket number of the next waiter to be notified. It can
// be read outside the lock, but is only written to with lock held.
@@ -482,7 +482,7 @@
func notifyListAdd(l *notifyList) uint32 {
// This may be called concurrently, for example, when called from
// sync.Cond.Wait while holding a RWMutex in read mode.
- return atomic.Xadd(&l.wait, 1) - 1
+ return l.wait.Add(1) - 1
}

// notifyListWait waits for a notification. If one has been sent since
@@ -527,7 +527,7 @@
func notifyListNotifyAll(l *notifyList) {
// Fast-path: if there are no new waiters since the last notification
// we don't need to acquire the lock.
- if atomic.Load(&l.wait) == atomic.Load(&l.notify) {
+ if l.wait.Load() == atomic.Load(&l.notify) {
return
}

@@ -542,7 +542,7 @@
// value of wait because any previous waiters are already in the list
// or will notice that they have already been notified when trying to
// add themselves to the list.
- atomic.Store(&l.notify, atomic.Load(&l.wait))
+ atomic.Store(&l.notify, l.wait.Load())
unlock(&l.lock)

// Go through the local list and ready all waiters.
@@ -560,7 +560,7 @@
func notifyListNotifyOne(l *notifyList) {
// Fast-path: if there are no new waiters since the last notification
// we don't need to acquire the lock at all.
- if atomic.Load(&l.wait) == atomic.Load(&l.notify) {
+ if l.wait.Load() == atomic.Load(&l.notify) {
return
}

@@ -568,7 +568,7 @@

// Re-check under the lock if we need to do anything.
t := l.notify
- if t == atomic.Load(&l.wait) {
+ if t == l.wait.Load() {
unlock(&l.lock)
return
}

To view, visit change 425784. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ib096332fe6111bbcd2f5c4cbb29c2fef7a808e7e
Gerrit-Change-Number: 425784
Gerrit-PatchSet: 4
Gerrit-Owner: hopehook <hope...@qq.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Michael Knyszek <mkny...@google.com>
Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
Gerrit-Reviewer: hopehook <hope...@qq.com>
Gerrit-MessageType: merged
Reply all
Reply to author
Forward
0 new messages