mirror of
https://github.com/golang/sys.git
synced 2026-02-08 11:46:04 +03:00
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 <Jason@zx2c4.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Matt Layher <mdlayher@gmail.com>
This commit is contained in:
committed by
Matt Layher
parent
79a91cf218
commit
5da285871e
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user