-
Notifications
You must be signed in to change notification settings - Fork 23
/
timesrc_win.go
105 lines (88 loc) · 2.25 KB
/
timesrc_win.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// +build windows
package irtt
import (
"fmt"
"os"
"time"
"unsafe"
"golang.org/x/sys/windows"
)
// WindowsTimeSource uses GetSystemTimePreciseAsFileTime for the wall clock and
// QueryPerformanceFrequency/Counter for the monotonic clock.
type WindowsTimeSource struct {
period int64
qpc *windows.LazyProc
}
// Now returns a Time containing the current time.
func (w *WindowsTimeSource) Now(clock Clock) Time {
switch clock {
case Wall:
return Time{w.systemTimePreciseNs(), time.Duration(0)}
case Monotonic:
return Time{0, w.queryPerformanceCounterNs()}
case BothClocks:
return Time{w.systemTimePreciseNs(), w.queryPerformanceCounterNs()}
default:
panic(fmt.Sprintf("unknown clock %s", clock))
}
}
func (w *WindowsTimeSource) String() string {
return "windows"
}
func (w *WindowsTimeSource) systemTimePreciseNs() int64 {
var t windows.Filetime
windows.GetSystemTimePreciseAsFileTime(&t)
return t.Nanoseconds()
}
func (w *WindowsTimeSource) queryPerformanceCounterNs() time.Duration {
var ctr int64
ret, _, err := w.qpc.Call(uintptr(unsafe.Pointer(&ctr)))
if ret == 0 {
panic(err)
}
return time.Duration(ctr * w.period)
}
// NewWindowsTimeSource returns a new WindowsTimeSource.
func NewWindowsTimeSource() (ts *WindowsTimeSource, err error) {
k := windows.NewLazySystemDLL("kernel32.dll")
if err = k.Load(); err != nil {
return
}
qpf := k.NewProc("QueryPerformanceFrequency")
if err = qpf.Find(); err != nil {
return
}
qpc := k.NewProc("QueryPerformanceCounter")
if err = qpc.Find(); err != nil {
return
}
var freq int64
var ret uintptr
if ret, _, err = qpf.Call(uintptr(unsafe.Pointer(&freq))); ret == 0 {
return
}
err = nil
ts = &WindowsTimeSource{1000000000 / freq, qpc}
return
}
// NewDefaultTimeSource returns a WindowsTimeSource for Windows and GoTimeSource
// for everything else.
func NewDefaultTimeSource() TimeSource {
wts, err := NewWindowsTimeSource()
if err != nil {
fmt.Fprintf(os.Stderr, "falling back to Go time source: %s\n", err)
return NewGoTimeSource()
}
return wts
}
func init() {
RegisterTimeSource(
func(s string) (t TimeSource, err error) {
if s == "windows" {
t, err = NewWindowsTimeSource()
}
return
},
"windows: GetSystemTimePreciseAsFileTime/QueryPerformanceCounter",
)
}