-
Notifications
You must be signed in to change notification settings - Fork 28
/
goid.go
79 lines (68 loc) · 1.56 KB
/
goid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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)
}