From: Ian Lance Taylor Date: Thu, 11 Apr 2019 21:29:09 +0000 (-0700) Subject: runtime: implement time.Sleep for new timers X-Git-Tag: go1.14beta1~621 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=7d84245a9cd41489984e36c5a01876fc4da5d5ec;p=gostls13.git runtime: implement time.Sleep for new timers Updates #27707 Change-Id: I51da8a04ec12ba1efa435e86e3a15d4d13c96c45 Reviewed-on: https://go-review.googlesource.com/c/go/+/171879 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Michael Knyszek --- diff --git a/src/runtime/time.go b/src/runtime/time.go index de8cb0835f..3eba66bf07 100644 --- a/src/runtime/time.go +++ b/src/runtime/time.go @@ -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) {