From 986582126a371ff927182ba6e61e364c1341171a Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 25 Oct 2017 11:13:23 -0400 Subject: [PATCH] runtime: avoid monotonic time zero on systems with low-res timers Otherwise low-res timers cause problems at call sites that expect to be able to use 0 as meaning "no time set" and therefore expect that nanotime never returns 0 itself. For example, sched.lastpoll == 0 means no last poll. Fixes #22394. Change-Id: Iea28acfddfff6f46bc90f041ec173e0fea591285 Reviewed-on: https://go-review.googlesource.com/73410 Run-TryBot: Russ Cox TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor Reviewed-by: Austin Clements --- src/runtime/proc.go | 3 +++ src/runtime/time.go | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/runtime/proc.go b/src/runtime/proc.go index 48ce7d6248..af9b33886c 100644 --- a/src/runtime/proc.go +++ b/src/runtime/proc.go @@ -142,6 +142,9 @@ func main() { } runtime_init() // must be before defer + if nanotime() == 0 { + throw("nanotime returning zero") + } // Defer unlock so that runtime.Goexit during init does the unlock too. needUnlock := true diff --git a/src/runtime/time.go b/src/runtime/time.go index 0e1763e0cd..6c349c8461 100644 --- a/src/runtime/time.go +++ b/src/runtime/time.go @@ -395,4 +395,10 @@ func time_runtimeNano() int64 { return nanotime() } -var startNano int64 = nanotime() +// Monotonic times are reported as offsets from startNano. +// We initialize startNano to nanotime() - 1 so that on systems where +// monotonic time resolution is fairly low (e.g. Windows 2008 +// which appears to have a default resolution of 15ms), +// we avoid ever reporting a nanotime of 0. +// (Callers may want to use 0 as "time not set".) +var startNano int64 = nanotime() - 1 -- 2.48.1