github.com/ScaleFT/monotime@v0.0.0-20180209174256-2da272f3dad0/clock_windows_qpf.go (about)

     1  // +build windows
     2  // +build !go1.9
     3  
     4  package monotime
     5  
     6  import (
     7  	"time"
     8  	"unsafe"
     9  
    10  	"golang.org/x/sys/windows"
    11  )
    12  
    13  var (
    14  	modKernel32 = windows.NewLazySystemDLL("kernel32.dll")
    15  )
    16  
    17  var (
    18  	procQueryPerformanceCounter   = modKernel32.NewProc("QueryPerformanceCounter")
    19  	procQueryPerformanceFrequency = modKernel32.NewProc("QueryPerformanceFrequency")
    20  )
    21  
    22  var counterFreq float64
    23  
    24  func init() {
    25  	var freq int64
    26  	ret, _, err := procQueryPerformanceFrequency.Call(uintptr(unsafe.Pointer(&freq)))
    27  	if ret == 0 {
    28  		panic(err.Error())
    29  	}
    30  
    31  	counterFreq = float64(freq) / 1e9
    32  }
    33  
    34  func monotime() Monotime {
    35  	var t uint64
    36  
    37  	ret, _, err := procQueryPerformanceCounter.Call(uintptr(unsafe.Pointer(&t)))
    38  	if ret == 0 {
    39  		panic(err.Error())
    40  	}
    41  
    42  	return t
    43  }
    44  
    45  func duration(start Monotime, end Monotime) time.Duration {
    46  	return time.Duration(float64((end - start)) / counterFreq)
    47  }