Skip to content

Commit

Permalink
runtime: implement time.Sleep for new timers
Browse files Browse the repository at this point in the history
Updates #27707

Change-Id: I51da8a04ec12ba1efa435e86e3a15d4d13c96c45
Reviewed-on: https://go-review.googlesource.com/c/go/+/171879
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
  • Loading branch information
ianlancetaylor committed Oct 22, 2019
1 parent 432ca0e commit 7d84245
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/runtime/time.go
Expand Up @@ -229,7 +229,31 @@ func timeSleep(ns int64) {
timeSleepOld(ns)
return
}
throw("new timeSleep not yet implemented")

if ns <= 0 {
return
}

gp := getg()
t := gp.timer
if t == nil {
t = new(timer)
gp.timer = t
}
t.f = goroutineReady
t.arg = gp
t.nextwhen = nanotime() + ns
gopark(resetForSleep, unsafe.Pointer(t), waitReasonSleep, traceEvGoSleep, 1)
}

// resetForSleep is called after the goroutine is parked for timeSleep.
// We can't call resettimer in timeSleep itself because if this is a short
// sleep and there are many goroutines then the P can wind up running the
// timer function, goroutineReady, before the goroutine has been parked.
func resetForSleep(gp *g, ut unsafe.Pointer) bool {
t := (*timer)(ut)
resettimer(t, t.nextwhen)
return true
}

func timeSleepOld(ns int64) {
Expand Down

0 comments on commit 7d84245

Please sign in to comment.