Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions gls/procid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package gls

import (
"unsafe"
"github.com/v2pro/plz/reflect2"
)

// offset for go1.19
var mOffset uintptr = 48
var midOffset uintptr = 72

func init() {
gType := reflect2.TypeByName("runtime.g").(reflect2.StructType)
if gType == nil {
panic("failed to get runtime.g type")
}

mField := gType.FieldByName("m")
mOffset = mField.Offset()

mType := reflect2.TypeByName("runtime.m").(reflect2.StructType)
if mType == nil {
panic("failed to get runtime.m type")
}
midField := mType.FieldByName("procid")
midOffset = midField.Offset()
}

// ProcID returns the thread id of current thread
func ProcID() uint64 {
g := getg()
p_m := (*uintptr)(unsafe.Pointer(g + mOffset))
p_mid := (*uint64)(unsafe.Pointer(*p_m + midOffset))
return *p_mid
}