]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: use timer.lock in cleantimers
authorRuss Cox <rsc@golang.org>
Wed, 14 Feb 2024 16:57:01 +0000 (11:57 -0500)
committerRuss Cox <rsc@golang.org>
Wed, 28 Feb 2024 16:44:09 +0000 (16:44 +0000)
Continue using timer.lock to simplify timer operations.

[This is one CL in a refactoring stack making very small changes
in each step, so that any subtle bugs that we miss can be more
easily pinpointed to a small change.]

Change-Id: Ic12fd2630e8ac23cddd00fa7e3240a1ac19da596
Reviewed-on: https://go-review.googlesource.com/c/go/+/564126
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/runtime/time.go

index 5a8f516ccad2e24ecd55d97bbb14e12b77738423..2b82306812c51bf54e60f61655aced1923556e7f 100644 (file)
@@ -393,31 +393,31 @@ func cleantimers(pp *p) {
                if t.pp.ptr() != pp {
                        throw("cleantimers: bad p")
                }
-               switch s := t.status.Load(); s {
-               case timerModified:
-                       if !t.status.CompareAndSwap(s, timerLocked) {
-                               continue
-                       }
-                       if t.nextwhen == 0 {
-                               dodeltimer0(pp)
-                               pp.deletedTimers.Add(-1)
-                               if !t.status.CompareAndSwap(timerLocked, timerRemoved) {
-                                       badTimer()
-                               }
-                       } else {
-                               // Now we can change the when field.
-                               t.when = t.nextwhen
-                               // Move t to the right position.
-                               dodeltimer0(pp)
-                               doaddtimer(pp, t)
-                               if !t.status.CompareAndSwap(timerLocked, timerWaiting) {
-                                       badTimer()
-                               }
-                       }
-               default:
+
+               status := t.status.Load()
+               if status != timerModified {
+                       // Fast path: head of timers does not need adjustment.
+                       return
+               }
+
+               status, mp := t.lock()
+               if status != timerModified {
                        // Head of timers does not need adjustment.
+                       t.unlock(status, mp)
                        return
                }
+               dodeltimer0(pp)
+               if t.nextwhen == 0 {
+                       pp.deletedTimers.Add(-1)
+                       status = timerRemoved
+               } else {
+                       // Now we can change the when field.
+                       t.when = t.nextwhen
+                       // Move t to the right position.
+                       doaddtimer(pp, t)
+                       status = timerWaiting
+               }
+               t.unlock(status, mp)
        }
 }