amuz.es/src/infra/goutils@v0.1.3/runtime/runtime.go (about) 1 package runtime 2 3 import ( 4 "reflect" 5 "unsafe" 6 ) 7 8 var ( 9 offsetGoid uintptr 10 offsetSched uintptr 11 ) 12 13 func init() { 14 pg, err := TypeByString("*runtime.g") 15 if err != nil { 16 return 17 } 18 if pg.Kind() != reflect.Ptr { 19 return 20 } 21 g := pg.Elem() 22 if g.Kind() != reflect.Struct { 23 return 24 } 25 goid, ok := g.FieldByName("goid") 26 if !ok { 27 return 28 } 29 sched, ok := g.FieldByName("sched") 30 if !ok { 31 return 32 } 33 offsetGoid = goid.Offset 34 offsetSched = sched.Offset 35 } 36 37 //go:linkname mcall runtime.mcall 38 //go:nosplit 39 func mcall(func(uintptr)) 40 41 //go:linkname gogo runtime.gogo 42 //go:nosplit 43 func gogo(uintptr) 44 45 46 47 func GOID() (goid int64) { 48 mcall(func(g uintptr) { 49 id := unsafe.Pointer(g + offsetGoid) 50 goid = *(*int64)(id) 51 sched := g + offsetSched 52 gogo(sched) 53 }) 54 return 55 }