From 3ac1996f9d265a2bc2fa42bb053570ab28fedf8e Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 27 Jan 2019 17:31:30 +0900 Subject: [PATCH] clock: Avoid overflow by using time duration on Windows --- internal/clock/now_windows.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/internal/clock/now_windows.go b/internal/clock/now_windows.go index 5bf11a2ab..5b23cb25f 100644 --- a/internal/clock/now_windows.go +++ b/internal/clock/now_windows.go @@ -30,21 +30,33 @@ var ( procQueryPerformanceCounter = kernel32.NewProc("QueryPerformanceCounter") ) -var freq int64 - -func init() { +func queryPerformanceFrequency() int64 { + var freq int64 + // TODO: Should the returned value be checked? _, _, e := procQueryPerformanceFrequency.Call(uintptr(unsafe.Pointer(&freq))) if e != nil && e.(windows.Errno) != 0 { panic(fmt.Sprintf("clock: QueryPerformanceFrequency failed: errno: %d", e.(windows.Errno))) } + return freq } -func now() int64 { +func queryPerformanceCounter() int64 { var ctr int64 // TODO: Should the returned value be checked? _, _, e := procQueryPerformanceCounter.Call(uintptr(unsafe.Pointer(&ctr))) if e != nil && e.(windows.Errno) != 0 { panic(fmt.Sprintf("clock: QueryPerformanceCounter failed: errno: %d", e.(windows.Errno))) } - return (ctr * 1e9) / freq + return ctr +} + +var ( + freq = queryPerformanceFrequency() + initCtr = queryPerformanceCounter() +) + +func now() int64 { + // Use the time duration instead of the current counter to avoid overflow. + duration := queryPerformanceCounter() - initCtr + return (duration * 1e9) / freq }