Skip to content

Commit

Permalink
[release-branch.go1.9] runtime: avoid monotonic time zero on systems …
Browse files Browse the repository at this point in the history
…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 <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-on: https://go-review.googlesource.com/73491
TryBot-Result: Russ Cox <rsc@golang.org>
  • Loading branch information
rsc committed Oct 25, 2017
1 parent 8bb333a commit 9be38a1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/runtime/proc.go
Expand Up @@ -142,6 +142,9 @@ func main() {
} }


runtime_init() // must be before defer 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. // Defer unlock so that runtime.Goexit during init does the unlock too.
needUnlock := true needUnlock := true
Expand Down
8 changes: 7 additions & 1 deletion src/runtime/time.go
Expand Up @@ -309,4 +309,10 @@ func time_runtimeNano() int64 {
return nanotime() 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

0 comments on commit 9be38a1

Please sign in to comment.