1 Star 2 Fork 0

TimAndy/routine

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
goid.go 1.56 KB
一键复制 编辑 原始数据 按行查看 历史
TimAndy 提交于 2022-05-27 13:53 . Update gohack to the latest version
package routine
import (
"fmt"
"reflect"
"unsafe"
)
var (
offsetGoid uintptr
offsetPaniconfault uintptr
offsetGopc uintptr
offsetLabels uintptr
)
func init() {
gt := getgt()
offsetGoid = offset(gt, "goid")
offsetPaniconfault = offset(gt, "paniconfault")
offsetGopc = offset(gt, "gopc")
offsetLabels = offset(gt, "labels")
}
type g struct {
goid int64
paniconfault *bool
gopc *uintptr
labels *unsafe.Pointer
}
//go:norace
func (gp g) getPanicOnFault() bool {
return *gp.paniconfault
}
//go:norace
func (gp g) setPanicOnFault(new bool) (old bool) {
old = *gp.paniconfault
*gp.paniconfault = new
return old
}
//go:norace
func (gp g) getLabels() unsafe.Pointer {
return *gp.labels
}
//go:norace
func (gp g) setLabels(labels unsafe.Pointer) {
*gp.labels = labels
}
// getg returns current coroutine struct.
func getg() g {
gp := getgp()
if gp == nil {
panic("Failed to get gp from runtime natively.")
}
return g{
goid: *(*int64)(add(gp, offsetGoid)),
paniconfault: (*bool)(add(gp, offsetPaniconfault)),
gopc: (*uintptr)(add(gp, offsetGopc)),
labels: (*unsafe.Pointer)(add(gp, offsetLabels)),
}
}
// offset returns the offset of the specified field.
func offset(t reflect.Type, f string) uintptr {
field, found := t.FieldByName(f)
if found {
return field.Offset
}
panic(fmt.Sprintf("No such field '%v' of struct '%v.%v'.", f, t.PkgPath(), t.Name()))
}
// add pointer addition operation.
func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
return unsafe.Pointer(uintptr(p) + x)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/timandy/routine.git
git@gitee.com:timandy/routine.git
timandy
routine
routine
main

搜索帮助