1
0
mirror of https://github.com/golang/go synced 2024-11-16 21:24:53 -07:00

runtime: update timers.len with Store instead of Add

Writes to timers.len are protected by the timers.lock.
There is no need to use an Add instead of a Store,
and the code is clearer (and perhaps slightly faster)
using the Store.

Change-Id: Icc6caef1b7405adec55c9b55b999b71de7d97484
Reviewed-on: https://go-review.googlesource.com/c/go/+/564976
Auto-Submit: Russ Cox <rsc@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Russ Cox 2024-02-16 18:42:08 -05:00 committed by Gopher Robot
parent 2171e628b8
commit 3ad5cd4741

View File

@ -288,7 +288,7 @@ func (ts *timers) add(t *timer) {
if t == ts.heap[0] { if t == ts.heap[0] {
ts.minWhen.Store(t.when) ts.minWhen.Store(t.when)
} }
ts.len.Add(1) ts.len.Store(uint32(len(ts.heap)))
} }
// stop deletes the timer t. It may be on some other P, so we can't // stop deletes the timer t. It may be on some other P, so we can't
@ -330,8 +330,8 @@ func (ts *timers) deleteMin() {
ts.siftDown(0) ts.siftDown(0)
} }
ts.updateMinWhen() ts.updateMinWhen()
n := ts.len.Add(-1) ts.len.Store(uint32(last))
if n == 0 { if last == 0 {
// If there are no timers, then clearly none are modified. // If there are no timers, then clearly none are modified.
ts.minNextWhen.Store(0) ts.minNextWhen.Store(0)
} }
@ -764,11 +764,11 @@ func updateTimerPMask(pp *p) {
return return
} }
// Looks like there are no timers, however another P may transiently // Looks like there are no timers, however another P
// decrement numTimers when handling a timerModified timer in // may be adding one at this very moment.
// checkTimers. We must take timersLock to serialize with these changes. // Take the lock to synchronize.
lock(&pp.timers.lock) lock(&pp.timers.lock)
if pp.timers.len.Load() == 0 { if len(pp.timers.heap) == 0 {
timerpMask.clear(pp.id) timerpMask.clear(pp.id)
} }
unlock(&pp.timers.lock) unlock(&pp.timers.lock)