gitee.com/quant1x/gox@v1.21.2/gls/goid.go (about)

     1  package gls
     2  
     3  import (
     4  	reflect "gitee.com/quant1x/gox/util/reflect2"
     5  	"unsafe"
     6  )
     7  
     8  var (
     9  	goidOffset uintptr = 128 // offset for go1.4
    10  )
    11  
    12  func init() {
    13  	gType := reflect.TypeByName("runtime.g").(reflect.StructType)
    14  	if gType == nil {
    15  		panic("failed to get runtime.g type")
    16  	}
    17  	goidField := gType.FieldByName("goid")
    18  	goidOffset = goidField.Offset()
    19  }
    20  
    21  // GoID returns the goroutine id of current goroutine
    22  //
    23  //go:nocheckptr
    24  func GoID() int64 {
    25  	g := getg()
    26  	// TODO: fatal error: checkptr: pointer arithmetic result points to invalid allocation
    27  	p_goid := (*int64)(unsafe.Pointer(g + goidOffset))
    28  	return *p_goid
    29  }
    30  
    31  func getg() uintptr