Skip to content

Commit f6e7fe2

Browse files
committed
runtime: move findrunnable timer delay computation closer to use
findrunnable has a couple places where delta is recomputed from a new pollUntil value. This proves to be a pain in refactoring, as it is easy to forget to do properly. Move computation of delta closer to its use, where it is more logical anyways. This CL should have no functional changes. For #43997. For #44313. Change-Id: I89980fd7f40f8a4c56c7540cae03ff99e12e1422 Reviewed-on: https://go-review.googlesource.com/c/go/+/307910 Trust: Michael Pratt <[email protected]> Run-TryBot: Michael Pratt <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Michael Knyszek <[email protected]>
1 parent 9fbcba6 commit f6e7fe2

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

src/runtime/lock_futex.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func notetsleepg(n *note, ns int64) bool {
239239
return ok
240240
}
241241

242-
func beforeIdle(int64) (*g, bool) {
242+
func beforeIdle(int64, int64) (*g, bool) {
243243
return nil, false
244244
}
245245

src/runtime/lock_js.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,12 @@ var idleID int32
176176
// If an event handler returned, we resume it and it will pause the execution.
177177
// beforeIdle either returns the specific goroutine to schedule next or
178178
// indicates with otherReady that some goroutine became ready.
179-
func beforeIdle(delay int64) (gp *g, otherReady bool) {
179+
func beforeIdle(now, pollUntil int64) (gp *g, otherReady bool) {
180+
delay := int64(-1)
181+
if pollUntil != 0 {
182+
delay = pollUntil - now
183+
}
184+
180185
if delay > 0 {
181186
clearIdleID()
182187
if delay < 1e6 {

src/runtime/lock_sema.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ func notetsleepg(n *note, ns int64) bool {
298298
return ok
299299
}
300300

301-
func beforeIdle(int64) (*g, bool) {
301+
func beforeIdle(int64, int64) (*g, bool) {
302302
return nil, false
303303
}
304304

src/runtime/proc.go

+13-18
Original file line numberDiff line numberDiff line change
@@ -2742,17 +2742,11 @@ stop:
27422742
}
27432743
}
27442744

2745-
delta := int64(-1)
2746-
if pollUntil != 0 {
2747-
// checkTimers ensures that polluntil > now.
2748-
delta = pollUntil - now
2749-
}
2750-
27512745
// wasm only:
27522746
// If a callback returned and no other goroutine is awake,
27532747
// then wake event handler goroutine which pauses execution
27542748
// until a callback was triggered.
2755-
gp, otherReady := beforeIdle(delta)
2749+
gp, otherReady := beforeIdle(now, pollUntil)
27562750
if gp != nil {
27572751
casgstatus(gp, _Gwaiting, _Grunnable)
27582752
if trace.enabled {
@@ -2842,15 +2836,6 @@ stop:
28422836
}
28432837
}
28442838
}
2845-
if pollUntil != 0 {
2846-
if now == 0 {
2847-
now = nanotime()
2848-
}
2849-
delta = pollUntil - now
2850-
if delta < 0 {
2851-
delta = 0
2852-
}
2853-
}
28542839

28552840
// Check for idle-priority GC work again.
28562841
//
@@ -2909,11 +2894,21 @@ stop:
29092894
if _g_.m.spinning {
29102895
throw("findrunnable: netpoll with spinning")
29112896
}
2897+
delay := int64(-1)
2898+
if pollUntil != 0 {
2899+
if now == 0 {
2900+
now = nanotime()
2901+
}
2902+
delay = pollUntil - now
2903+
if delay < 0 {
2904+
delay = 0
2905+
}
2906+
}
29122907
if faketime != 0 {
29132908
// When using fake time, just poll.
2914-
delta = 0
2909+
delay = 0
29152910
}
2916-
list := netpoll(delta) // block until new work is available
2911+
list := netpoll(delay) // block until new work is available
29172912
atomic.Store64(&sched.pollUntil, 0)
29182913
atomic.Store64(&sched.lastpoll, uint64(nanotime()))
29192914
if faketime != 0 && list.empty() {

0 commit comments

Comments
 (0)