From 370c8e983b49168b670ea4df76293738a833459b Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 14 Feb 2024 11:57:05 -0500 Subject: [PATCH] runtime: use methods for timer Continuing conversion from C to Go, change timer API to use methods. [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: I4cb88a366993a77aa4fad739793a7db7213cc38c Reviewed-on: https://go-review.googlesource.com/c/go/+/564131 Reviewed-by: Ian Lance Taylor LUCI-TryBot-Result: Go LUCI --- src/runtime/netpoll.go | 16 ++++++++-------- src/runtime/time.go | 28 ++++++++++++++-------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/runtime/netpoll.go b/src/runtime/netpoll.go index 9c2e40ce8a..52e7da5741 100644 --- a/src/runtime/netpoll.go +++ b/src/runtime/netpoll.go @@ -399,14 +399,14 @@ func poll_runtime_pollSetDeadline(pd *pollDesc, d int64, mode int) { // if they differ the descriptor was reused or timers were reset. pd.rt.arg = pd.makeArg() pd.rt.seq = pd.rseq - resettimer(&pd.rt, pd.rd) + pd.rt.reset(pd.rd) } } else if pd.rd != rd0 || combo != combo0 { pd.rseq++ // invalidate current timers if pd.rd > 0 { - modtimer(&pd.rt, pd.rd, 0, rtf, pd.makeArg(), pd.rseq) + pd.rt.modify(pd.rd, 0, rtf, pd.makeArg(), pd.rseq) } else { - deltimer(&pd.rt) + pd.rt.stop() pd.rt.f = nil } } @@ -415,14 +415,14 @@ func poll_runtime_pollSetDeadline(pd *pollDesc, d int64, mode int) { pd.wt.f = netpollWriteDeadline pd.wt.arg = pd.makeArg() pd.wt.seq = pd.wseq - resettimer(&pd.wt, pd.wd) + pd.wt.reset(pd.wd) } } else if pd.wd != wd0 || combo != combo0 { pd.wseq++ // invalidate current timers if pd.wd > 0 && !combo { - modtimer(&pd.wt, pd.wd, 0, netpollWriteDeadline, pd.makeArg(), pd.wseq) + pd.wt.modify(pd.wd, 0, netpollWriteDeadline, pd.makeArg(), pd.wseq) } else { - deltimer(&pd.wt) + pd.wt.stop() pd.wt.f = nil } } @@ -461,11 +461,11 @@ func poll_runtime_pollUnblock(pd *pollDesc) { rg = netpollunblock(pd, 'r', false, &delta) wg = netpollunblock(pd, 'w', false, &delta) if pd.rt.f != nil { - deltimer(&pd.rt) + pd.rt.stop() pd.rt.f = nil } if pd.wt.f != nil { - deltimer(&pd.wt) + pd.wt.stop() pd.wt.f = nil } unlock(&pd.lock) diff --git a/src/runtime/time.go b/src/runtime/time.go index 845ba85ac4..4ccf2d98c7 100644 --- a/src/runtime/time.go +++ b/src/runtime/time.go @@ -190,7 +190,7 @@ func timeSleep(ns int64) { // timer function, goroutineReady, before the goroutine has been parked. func resetForSleep(gp *g, ut unsafe.Pointer) bool { t := (*timer)(ut) - resettimer(t, t.nextWhen) + t.reset(t.nextWhen) return true } @@ -204,7 +204,7 @@ func startTimer(t *timer) { if t.state.Load() != 0 { throw("startTimer called with initialized timer") } - resettimer(t, t.when) + t.reset(t.when) } // stopTimer stops a timer. @@ -212,7 +212,7 @@ func startTimer(t *timer) { // //go:linkname stopTimer time.stopTimer func stopTimer(t *timer) bool { - return deltimer(t) + return t.stop() } // resetTimer resets an inactive timer, adding it to the heap. @@ -224,14 +224,14 @@ func resetTimer(t *timer, when int64) bool { if raceenabled { racerelease(unsafe.Pointer(t)) } - return resettimer(t, when) + return t.reset(when) } // modTimer modifies an existing timer. // //go:linkname modTimer time.modTimer func modTimer(t *timer, when, period int64) { - modtimer(t, when, period, t.f, t.arg, t.seq) + t.modify(when, period, t.f, t.arg, t.seq) } // Go runtime. @@ -263,11 +263,11 @@ func doaddtimer(pp *p, t *timer) { pp.numTimers.Add(1) } -// deltimer deletes the timer t. It may be on some other P, so we can't -// actually remove it from the timers heap. We can only mark it as deleted. +// stop deletes the timer t. It may be on some other P, so we can't +// actually remove it from the timers heap. We can only mark it as stopped. // It will be removed in due course by the P whose heap it is on. -// Reports whether the timer was removed before it was run. -func deltimer(t *timer) bool { +// Reports whether the timer was stopped before it was run. +func (t *timer) stop() bool { state, mp := t.lock() if state&timerHeaped != 0 && (state&timerNextWhen == 0 || t.nextWhen != 0) { // Timer pending: stop it. @@ -310,10 +310,10 @@ func dodeltimer0(pp *p) { } } -// modtimer modifies an existing timer. +// modify modifies an existing timer. // This is called by the netpoll code or time.Ticker.Reset or time.Timer.Reset. // Reports whether the timer was modified before it was run. -func modtimer(t *timer, when, period int64, f func(any, uintptr), arg any, seq uintptr) bool { +func (t *timer) modify(when, period int64, f func(any, uintptr), arg any, seq uintptr) bool { if when <= 0 { throw("timer when must be positive") } @@ -377,11 +377,11 @@ func modtimer(t *timer, when, period int64, f func(any, uintptr), arg any, seq u return pending } -// resettimer resets the time when a timer should fire. +// reset resets the time when a timer should fire. // If used for an inactive timer, the timer will become active. // Reports whether the timer was active and was stopped. -func resettimer(t *timer, when int64) bool { - return modtimer(t, when, t.period, t.f, t.arg, t.seq) +func (t *timer) reset(when int64) bool { + return t.modify(when, t.period, t.f, t.arg, t.seq) } // cleantimers cleans up the head of the timer queue. This speeds up -- 2.48.1