From 5da285871e9c6a1c3acade75bea3282d33f55ebd Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Thu, 6 Jun 2019 15:35:02 +0200 Subject: [PATCH] windows: add accessor for duration since boot In order to deal with boot time race conditions, sometimes it is useful to determine the time since boot. Change-Id: Ibc907b49a9b072b3ef3b6c94eec7e2e6428943ba Reviewed-on: https://go-review.googlesource.com/c/sys/+/180899 Run-TryBot: Jason Donenfeld TryBot-Result: Gobot Gobot Reviewed-by: Matt Layher --- windows/syscall_windows.go | 6 ++++++ windows/zsyscall_windows.go | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/windows/syscall_windows.go b/windows/syscall_windows.go index c698da330..bea993b2d 100644 --- a/windows/syscall_windows.go +++ b/windows/syscall_windows.go @@ -10,6 +10,7 @@ import ( errorspkg "errors" "sync" "syscall" + "time" "unicode/utf16" "unsafe" ) @@ -194,6 +195,7 @@ func NewCallbackCDecl(fn interface{}) uintptr { //sys SetEnvironmentVariable(name *uint16, value *uint16) (err error) = kernel32.SetEnvironmentVariableW //sys CreateEnvironmentBlock(block **uint16, token Token, inheritExisting bool) (err error) = userenv.CreateEnvironmentBlock //sys DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock +//sys getTickCount64() (ms uint64) = kernel32.GetTickCount64 //sys SetFileTime(handle Handle, ctime *Filetime, atime *Filetime, wtime *Filetime) (err error) //sys GetFileAttributes(name *uint16) (attrs uint32, err error) [failretval==INVALID_FILE_ATTRIBUTES] = kernel32.GetFileAttributesW //sys SetFileAttributes(name *uint16, attrs uint32) (err error) = kernel32.SetFileAttributesW @@ -498,6 +500,10 @@ func ComputerName() (name string, err error) { return string(utf16.Decode(b[0:n])), nil } +func DurationSinceBoot() time.Duration { + return time.Duration(getTickCount64()) * time.Millisecond +} + func Ftruncate(fd Handle, length int64) (err error) { curoffset, e := Seek(fd, 0, 1) if e != nil { diff --git a/windows/zsyscall_windows.go b/windows/zsyscall_windows.go index dcd10e069..a7663b5fc 100644 --- a/windows/zsyscall_windows.go +++ b/windows/zsyscall_windows.go @@ -133,6 +133,7 @@ var ( procSetEnvironmentVariableW = modkernel32.NewProc("SetEnvironmentVariableW") procCreateEnvironmentBlock = moduserenv.NewProc("CreateEnvironmentBlock") procDestroyEnvironmentBlock = moduserenv.NewProc("DestroyEnvironmentBlock") + procGetTickCount64 = modkernel32.NewProc("GetTickCount64") procSetFileTime = modkernel32.NewProc("SetFileTime") procGetFileAttributesW = modkernel32.NewProc("GetFileAttributesW") procSetFileAttributesW = modkernel32.NewProc("SetFileAttributesW") @@ -1373,6 +1374,12 @@ func DestroyEnvironmentBlock(block *uint16) (err error) { return } +func getTickCount64() (ms uint64) { + r0, _, _ := syscall.Syscall(procGetTickCount64.Addr(), 0, 0, 0, 0) + ms = uint64(r0) + return +} + func SetFileTime(handle Handle, ctime *Filetime, atime *Filetime, wtime *Filetime) (err error) { r1, _, e1 := syscall.Syscall6(procSetFileTime.Addr(), 4, uintptr(handle), uintptr(unsafe.Pointer(ctime)), uintptr(unsafe.Pointer(atime)), uintptr(unsafe.Pointer(wtime)), 0, 0) if r1 == 0 {