Skip to content

Commit

Permalink
runtime: move findrunnable timer delay computation closer to use
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
prattmic committed Apr 16, 2021
1 parent 9fbcba6 commit f6e7fe2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/runtime/lock_futex.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func notetsleepg(n *note, ns int64) bool {
return ok
}

func beforeIdle(int64) (*g, bool) {
func beforeIdle(int64, int64) (*g, bool) {
return nil, false
}

Expand Down
7 changes: 6 additions & 1 deletion src/runtime/lock_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,12 @@ var idleID int32
// If an event handler returned, we resume it and it will pause the execution.
// beforeIdle either returns the specific goroutine to schedule next or
// indicates with otherReady that some goroutine became ready.
func beforeIdle(delay int64) (gp *g, otherReady bool) {
func beforeIdle(now, pollUntil int64) (gp *g, otherReady bool) {
delay := int64(-1)
if pollUntil != 0 {
delay = pollUntil - now
}

if delay > 0 {
clearIdleID()
if delay < 1e6 {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/lock_sema.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func notetsleepg(n *note, ns int64) bool {
return ok
}

func beforeIdle(int64) (*g, bool) {
func beforeIdle(int64, int64) (*g, bool) {
return nil, false
}

Expand Down
31 changes: 13 additions & 18 deletions src/runtime/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2742,17 +2742,11 @@ stop:
}
}

delta := int64(-1)
if pollUntil != 0 {
// checkTimers ensures that polluntil > now.
delta = pollUntil - now
}

// wasm only:
// If a callback returned and no other goroutine is awake,
// then wake event handler goroutine which pauses execution
// until a callback was triggered.
gp, otherReady := beforeIdle(delta)
gp, otherReady := beforeIdle(now, pollUntil)
if gp != nil {
casgstatus(gp, _Gwaiting, _Grunnable)
if trace.enabled {
Expand Down Expand Up @@ -2842,15 +2836,6 @@ stop:
}
}
}
if pollUntil != 0 {
if now == 0 {
now = nanotime()
}
delta = pollUntil - now
if delta < 0 {
delta = 0
}
}

// Check for idle-priority GC work again.
//
Expand Down Expand Up @@ -2909,11 +2894,21 @@ stop:
if _g_.m.spinning {
throw("findrunnable: netpoll with spinning")
}
delay := int64(-1)
if pollUntil != 0 {
if now == 0 {
now = nanotime()
}
delay = pollUntil - now
if delay < 0 {
delay = 0
}
}
if faketime != 0 {
// When using fake time, just poll.
delta = 0
delay = 0
}
list := netpoll(delta) // block until new work is available
list := netpoll(delay) // block until new work is available
atomic.Store64(&sched.pollUntil, 0)
atomic.Store64(&sched.lastpoll, uint64(nanotime()))
if faketime != 0 && list.empty() {
Expand Down

0 comments on commit f6e7fe2

Please sign in to comment.