github.com/coyove/common@v0.0.0-20240403014525-f70e643f9de8/rand/counter_windows.go (about)

     1  package rand
     2  
     3  import (
     4  	"syscall"
     5  	"time"
     6  	"unsafe"
     7  )
     8  
     9  var qpc uintptr
    10  
    11  func init() {
    12  	k32, _ := syscall.LoadLibrary("kernel32.dll")
    13  	qpc, _ = syscall.GetProcAddress(k32, "QueryPerformanceCounter")
    14  }
    15  
    16  func GetCounter() (k int64) {
    17  	syscall.Syscall(qpc, 1, uintptr(unsafe.Pointer(&k)), 0, 0)
    18  
    19  	if k == 0 {
    20  		k = time.Now().UnixNano()
    21  	}
    22  
    23  	return
    24  }