From 1df6db8e4fe0c8adf72e8533a09b46cad176eb42 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 14 Feb 2024 11:57:03 -0500 Subject: [PATCH] runtime: use timer.lock in adjusttimers 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 LUCI-TryBot-Result: Go LUCI --- src/runtime/time.go | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/src/runtime/time.go b/src/runtime/time.go index f94ad99196..251fe8eb49 100644 --- a/src/runtime/time.go +++ b/src/runtime/time.go @@ -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 { -- 2.48.1