github.com/sandwich-go/boost@v1.3.29/z/mono.go (about)

     1  package z
     2  
     3  import (
     4  	"time"
     5  	_ "unsafe"
     6  )
     7  
     8  // runtimeNanotime 返回从某一个节点开始(如系统启动)的单调时间
     9  //go:noescape
    10  func runtimeNanotime() int64
    11  
    12  // MonoTimeDuration 防止MonoSince时参数传递错误
    13  type MonoTimeDuration time.Duration
    14  
    15  //go:linkname runtimeNanotime runtime.nanotime
    16  func MonoOffset() MonoTimeDuration {
    17  	return MonoTimeDuration(runtimeNanotime())
    18  }
    19  
    20  // MonoSince returns the time elapsed since t, obtained previously using Now.
    21  func MonoSince(t MonoTimeDuration) time.Duration {
    22  	return time.Duration(MonoOffset() - t)
    23  }
    24  
    25  // BusyDelay waits for given duration using busy waiting
    26  func BusyDelay(duration time.Duration) {
    27  	end := runtimeNanotime() + int64(duration)
    28  	for runtimeNanotime() < end {
    29  	}
    30  }