updated code to utilize runtime.nanotime() which is not exported

This commit is contained in:
hyung-hwan 2024-12-09 19:22:43 +09:00
parent de113d150f
commit d043fd730b
2 changed files with 12 additions and 4 deletions

View File

@ -1,5 +1,4 @@
//go:build freebsd //go:build freebsd
package hodu package hodu
import "syscall" import "syscall"
@ -13,10 +12,15 @@ import "unsafe"
//const FREEBSD_CLOCK_MONOTONIC uintptr = C.CLOCK_MONOTONIC //const FREEBSD_CLOCK_MONOTONIC uintptr = C.CLOCK_MONOTONIC
const CLOCK_MONOTONIC uintptr = 4 const CLOCK_MONOTONIC uintptr = 4
// utilize the builtin runtime.nanotime()
//go:noescape
//go:linkname nanotime runtime.nanotime
func nanotime() int64
func monotonic_time() uint64 { func monotonic_time() uint64 {
var ts syscall.Timespec var ts syscall.Timespec
var errno syscall.Errno var errno syscall.Errno
_, _, errno = syscall.Syscall(syscall.SYS_CLOCK_GETTIME, CLOCK_MONOTONIC, uintptr(unsafe.Pointer(&ts)), 0) _, _, errno = syscall.Syscall(syscall.SYS_CLOCK_GETTIME, CLOCK_MONOTONIC, uintptr(unsafe.Pointer(&ts)), 0)
if errno != 0 { return 0 } if errno != 0 { return uint64(nanotime()) }
return uint64(ts.Nano()) return uint64(ts.Nano())
} }

View File

@ -1,5 +1,4 @@
//go:build linux //go:build linux
package hodu package hodu
import "syscall" import "syscall"
@ -13,10 +12,15 @@ import "unsafe"
//const CLOCK_MONOTONIC uintptr = C.CLOCK_MONOTONIC //const CLOCK_MONOTONIC uintptr = C.CLOCK_MONOTONIC
const CLOCK_MONOTONIC uintptr = 1 const CLOCK_MONOTONIC uintptr = 1
// utilize the builtin runtime.nanotime()
//go:noescape
//go:linkname nanotime runtime.nanotime
func nanotime() int64
func monotonic_time() uint64 { func monotonic_time() uint64 {
var ts syscall.Timespec var ts syscall.Timespec
var errno syscall.Errno var errno syscall.Errno
_, _, errno = syscall.Syscall(syscall.SYS_CLOCK_GETTIME, CLOCK_MONOTONIC, uintptr(unsafe.Pointer(&ts)), 0) _, _, errno = syscall.Syscall(syscall.SYS_CLOCK_GETTIME, CLOCK_MONOTONIC, uintptr(unsafe.Pointer(&ts)), 0)
if errno != 0 { return 0 } if errno != 0 { return uint64(nanotime()) }
return uint64(ts.Nano()) return uint64(ts.Nano())
} }