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 <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Russ Cox 2024-02-14 11:57:03 -05:00
parent d1e8dc25ff
commit 1df6db8e4f
1 changed files with 9 additions and 22 deletions

View 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 {