aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/time.go
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2019-04-11 14:29:09 -0700
committerIan Lance Taylor <iant@golang.org>2019-10-22 22:35:20 +0000
commit7d84245a9cd41489984e36c5a01876fc4da5d5ec (patch)
treece808082b36da14f999cb92b9a2b8c51ee6a1343 /src/runtime/time.go
parent432ca0ea83d12519004c6f7f7c1728410923987f (diff)
downloadgo-7d84245a9cd41489984e36c5a01876fc4da5d5ec.tar.gz
go-7d84245a9cd41489984e36c5a01876fc4da5d5ec.zip
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 <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
Diffstat (limited to 'src/runtime/time.go')
-rw-r--r--src/runtime/time.go26
1 files changed, 25 insertions, 1 deletions
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) {