Skip to content

Commit 370c8e9

Browse files
committed
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 <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent a155a2f commit 370c8e9

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

src/runtime/netpoll.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -399,14 +399,14 @@ func poll_runtime_pollSetDeadline(pd *pollDesc, d int64, mode int) {
399399
// if they differ the descriptor was reused or timers were reset.
400400
pd.rt.arg = pd.makeArg()
401401
pd.rt.seq = pd.rseq
402-
resettimer(&pd.rt, pd.rd)
402+
pd.rt.reset(pd.rd)
403403
}
404404
} else if pd.rd != rd0 || combo != combo0 {
405405
pd.rseq++ // invalidate current timers
406406
if pd.rd > 0 {
407-
modtimer(&pd.rt, pd.rd, 0, rtf, pd.makeArg(), pd.rseq)
407+
pd.rt.modify(pd.rd, 0, rtf, pd.makeArg(), pd.rseq)
408408
} else {
409-
deltimer(&pd.rt)
409+
pd.rt.stop()
410410
pd.rt.f = nil
411411
}
412412
}
@@ -415,14 +415,14 @@ func poll_runtime_pollSetDeadline(pd *pollDesc, d int64, mode int) {
415415
pd.wt.f = netpollWriteDeadline
416416
pd.wt.arg = pd.makeArg()
417417
pd.wt.seq = pd.wseq
418-
resettimer(&pd.wt, pd.wd)
418+
pd.wt.reset(pd.wd)
419419
}
420420
} else if pd.wd != wd0 || combo != combo0 {
421421
pd.wseq++ // invalidate current timers
422422
if pd.wd > 0 && !combo {
423-
modtimer(&pd.wt, pd.wd, 0, netpollWriteDeadline, pd.makeArg(), pd.wseq)
423+
pd.wt.modify(pd.wd, 0, netpollWriteDeadline, pd.makeArg(), pd.wseq)
424424
} else {
425-
deltimer(&pd.wt)
425+
pd.wt.stop()
426426
pd.wt.f = nil
427427
}
428428
}
@@ -461,11 +461,11 @@ func poll_runtime_pollUnblock(pd *pollDesc) {
461461
rg = netpollunblock(pd, 'r', false, &delta)
462462
wg = netpollunblock(pd, 'w', false, &delta)
463463
if pd.rt.f != nil {
464-
deltimer(&pd.rt)
464+
pd.rt.stop()
465465
pd.rt.f = nil
466466
}
467467
if pd.wt.f != nil {
468-
deltimer(&pd.wt)
468+
pd.wt.stop()
469469
pd.wt.f = nil
470470
}
471471
unlock(&pd.lock)

src/runtime/time.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func timeSleep(ns int64) {
190190
// timer function, goroutineReady, before the goroutine has been parked.
191191
func resetForSleep(gp *g, ut unsafe.Pointer) bool {
192192
t := (*timer)(ut)
193-
resettimer(t, t.nextWhen)
193+
t.reset(t.nextWhen)
194194
return true
195195
}
196196

@@ -204,15 +204,15 @@ func startTimer(t *timer) {
204204
if t.state.Load() != 0 {
205205
throw("startTimer called with initialized timer")
206206
}
207-
resettimer(t, t.when)
207+
t.reset(t.when)
208208
}
209209

210210
// stopTimer stops a timer.
211211
// It reports whether t was stopped before being run.
212212
//
213213
//go:linkname stopTimer time.stopTimer
214214
func stopTimer(t *timer) bool {
215-
return deltimer(t)
215+
return t.stop()
216216
}
217217

218218
// resetTimer resets an inactive timer, adding it to the heap.
@@ -224,14 +224,14 @@ func resetTimer(t *timer, when int64) bool {
224224
if raceenabled {
225225
racerelease(unsafe.Pointer(t))
226226
}
227-
return resettimer(t, when)
227+
return t.reset(when)
228228
}
229229

230230
// modTimer modifies an existing timer.
231231
//
232232
//go:linkname modTimer time.modTimer
233233
func modTimer(t *timer, when, period int64) {
234-
modtimer(t, when, period, t.f, t.arg, t.seq)
234+
t.modify(when, period, t.f, t.arg, t.seq)
235235
}
236236

237237
// Go runtime.
@@ -263,11 +263,11 @@ func doaddtimer(pp *p, t *timer) {
263263
pp.numTimers.Add(1)
264264
}
265265

266-
// deltimer deletes the timer t. It may be on some other P, so we can't
267-
// actually remove it from the timers heap. We can only mark it as deleted.
266+
// stop deletes the timer t. It may be on some other P, so we can't
267+
// actually remove it from the timers heap. We can only mark it as stopped.
268268
// It will be removed in due course by the P whose heap it is on.
269-
// Reports whether the timer was removed before it was run.
270-
func deltimer(t *timer) bool {
269+
// Reports whether the timer was stopped before it was run.
270+
func (t *timer) stop() bool {
271271
state, mp := t.lock()
272272
if state&timerHeaped != 0 && (state&timerNextWhen == 0 || t.nextWhen != 0) {
273273
// Timer pending: stop it.
@@ -310,10 +310,10 @@ func dodeltimer0(pp *p) {
310310
}
311311
}
312312

313-
// modtimer modifies an existing timer.
313+
// modify modifies an existing timer.
314314
// This is called by the netpoll code or time.Ticker.Reset or time.Timer.Reset.
315315
// Reports whether the timer was modified before it was run.
316-
func modtimer(t *timer, when, period int64, f func(any, uintptr), arg any, seq uintptr) bool {
316+
func (t *timer) modify(when, period int64, f func(any, uintptr), arg any, seq uintptr) bool {
317317
if when <= 0 {
318318
throw("timer when must be positive")
319319
}
@@ -377,11 +377,11 @@ func modtimer(t *timer, when, period int64, f func(any, uintptr), arg any, seq u
377377
return pending
378378
}
379379

380-
// resettimer resets the time when a timer should fire.
380+
// reset resets the time when a timer should fire.
381381
// If used for an inactive timer, the timer will become active.
382382
// Reports whether the timer was active and was stopped.
383-
func resettimer(t *timer, when int64) bool {
384-
return modtimer(t, when, t.period, t.f, t.arg, t.seq)
383+
func (t *timer) reset(when int64) bool {
384+
return t.modify(when, t.period, t.f, t.arg, t.seq)
385385
}
386386

387387
// cleantimers cleans up the head of the timer queue. This speeds up

0 commit comments

Comments
 (0)