Skip to content

Commit

Permalink
runtime: use timer.lock in cleantimers
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
rsc committed Feb 28, 2024
1 parent 2fb5ef8 commit 5891159
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/runtime/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 5891159

Please sign in to comment.