]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: use timer.lock in adjusttimers
authorRuss Cox <rsc@golang.org>
Wed, 14 Feb 2024 16:57:03 +0000 (11:57 -0500)
committerRuss Cox <rsc@golang.org>
Wed, 28 Feb 2024 16:44:13 +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: I2298cede902cbf0aea268c54d741190007a733c8
Reviewed-on: https://go-review.googlesource.com/c/go/+/564128
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/runtime/time.go

index f94ad991960f600fa234167987848cc691c200fd..251fe8eb49ed72483b0179fce6767a0b4df9f897 100644 (file)
@@ -521,21 +521,19 @@ func adjusttimers(pp *p, now int64, force bool) {
                if t.pp.ptr() != pp {
                        throw("adjusttimers: bad p")
                }
-               switch s := t.status.Load(); s {
-               case timerModified:
-                       if !t.status.CompareAndSwap(s, timerLocked) {
-                               // TODO(rsc): Try harder to lock.
-                               break
-                       }
+
+               status, mp := t.lock()
+               if status == timerRemoved {
+                       badTimer()
+               }
+               if status == timerModified {
                        if t.nextwhen == 0 {
                                n := len(pp.timers)
                                pp.timers[i] = pp.timers[n-1]
                                pp.timers[n-1] = nil
                                pp.timers = pp.timers[:n-1]
                                t.pp = 0
-                               if !t.status.CompareAndSwap(timerLocked, timerRemoved) {
-                                       badTimer()
-                               }
+                               status = timerRemoved
                                pp.deletedTimers.Add(-1)
                                i--
                                changed = true
@@ -543,21 +541,10 @@ func adjusttimers(pp *p, now int64, force bool) {
                                // Now we can change the when field.
                                t.when = t.nextwhen
                                changed = true
-                               if !t.status.CompareAndSwap(timerLocked, timerWaiting) {
-                                       badTimer()
-                               }
+                               status = timerWaiting
                        }
-               case timerRemoved:
-                       badTimer()
-               case timerWaiting:
-                       // OK, nothing to do.
-               case timerLocked:
-                       // Check again after modification is complete.
-                       osyield()
-                       i--
-               default:
-                       badTimer()
                }
+               t.unlock(status, mp)
        }
 
        if changed {