From 58911599e8dc25898c3017dd6655540fb9b976b1 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 14 Feb 2024 11:57:01 -0500 Subject: [PATCH] runtime: use timer.lock in cleantimers 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 Reviewed-by: Ian Lance Taylor --- src/runtime/time.go | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/runtime/time.go b/src/runtime/time.go index 5a8f516cca..2b82306812 100644 --- a/src/runtime/time.go +++ b/src/runtime/time.go @@ -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) } } -- 2.48.1