]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: implement time.Sleep for new timers
authorIan Lance Taylor <iant@golang.org>
Thu, 11 Apr 2019 21:29:09 +0000 (14:29 -0700)
committerIan Lance Taylor <iant@golang.org>
Tue, 22 Oct 2019 22:35:20 +0000 (22:35 +0000)
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>
src/runtime/time.go

index de8cb0835fb01f7968e9acee914d0fde704d60f3..3eba66bf0719e04cd8e70150f972da0e834b39e6 100644 (file)
@@ -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) {