Skip to content

Commit 9be38a1

Browse files
committed
[release-branch.go1.9] 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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Austin Clements <[email protected]> Reviewed-on: https://go-review.googlesource.com/73491 TryBot-Result: Russ Cox <[email protected]>
1 parent 8bb333a commit 9be38a1

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/runtime/proc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ func main() {
142142
}
143143

144144
runtime_init() // must be before defer
145+
if nanotime() == 0 {
146+
throw("nanotime returning zero")
147+
}
145148

146149
// Defer unlock so that runtime.Goexit during init does the unlock too.
147150
needUnlock := true

src/runtime/time.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,10 @@ func time_runtimeNano() int64 {
309309
return nanotime()
310310
}
311311

312-
var startNano int64 = nanotime()
312+
// Monotonic times are reported as offsets from startNano.
313+
// We initialize startNano to nanotime() - 1 so that on systems where
314+
// monotonic time resolution is fairly low (e.g. Windows 2008
315+
// which appears to have a default resolution of 15ms),
316+
// we avoid ever reporting a nanotime of 0.
317+
// (Callers may want to use 0 as "time not set".)
318+
var startNano int64 = nanotime() - 1

0 commit comments

Comments
 (0)