Skip to content

Commit 4782f42

Browse files
prattmiccherrymui
authored andcommitted
[release-branch.go1.18] runtime: clear timerModifiedEarliest when last timer is deleted
timerModifiedEarliest contains the lowest possible expiration for a modified earlier timer, which may be earlier than timer0When because we haven't yet updated the heap. Note "may", as the modified earlier timer that set timerModifiedEarliest may have since been modified later or deleted. We can clear timerModifiedEarliest when the last timer is deleted because by definition there must not be any modified earlier timers. Why does this matter? checkTimersNoP claims that there is work to do if timerModifiedEarliest has passed, causing findRunnable to loop back around to checkTimers. But the code to clean up timerModifiedEarliest in checkTimers (i.e., the call to adjusttimers) is conditional behind a check that len(pp.timers) > 0. Without clearing timerModifiedEarliest, a spinning M that would otherwise go to sleep will busy loop in findRunnable until some other work is available. Note that changing the condition on the call to adjusttimers would also be a valid fix. I took this approach because it feels a bit cleaner to clean up timerModifiedEarliest as soon as it is known to be irrelevant. For #51654. Fixes #53847. Change-Id: I3f3787c67781cac7ce87939c5706cef8db927dd5 Reviewed-on: https://go-review.googlesource.com/c/go/+/417434 Auto-Submit: Michael Pratt <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Michael Pratt <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> (cherry picked from commit c006b7a) Reviewed-on: https://go-review.googlesource.com/c/go/+/417475
1 parent 12e00f6 commit 4782f42

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/runtime/time.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,11 @@ func dodeltimer(pp *p, i int) int {
391391
if i == 0 {
392392
updateTimer0When(pp)
393393
}
394-
atomic.Xadd(&pp.numTimers, -1)
394+
n := atomic.Xadd(&pp.numTimers, -1)
395+
if n == 0 {
396+
// If there are no timers, then clearly none are modified.
397+
atomic.Store64(&pp.timerModifiedEarliest, 0)
398+
}
395399
return smallestChanged
396400
}
397401

@@ -415,7 +419,11 @@ func dodeltimer0(pp *p) {
415419
siftdownTimer(pp.timers, 0)
416420
}
417421
updateTimer0When(pp)
418-
atomic.Xadd(&pp.numTimers, -1)
422+
n := atomic.Xadd(&pp.numTimers, -1)
423+
if n == 0 {
424+
// If there are no timers, then clearly none are modified.
425+
atomic.Store64(&pp.timerModifiedEarliest, 0)
426+
}
419427
}
420428

421429
// modtimer modifies an existing timer.

0 commit comments

Comments
 (0)