-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathg.go
More file actions
66 lines (55 loc) · 1.34 KB
/
g.go
File metadata and controls
66 lines (55 loc) · 1.34 KB
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
package routine
import (
"fmt"
"reflect"
"unsafe"
)
type g struct {
}
//go:norace
func (g *g) goid() uint64 {
return *(*uint64)(add(unsafe.Pointer(g), offsetGoid))
}
//go:norace
func (g *g) gopc() uintptr {
return *(*uintptr)(add(unsafe.Pointer(g), offsetGopc))
}
//go:norace
func (g *g) getPanicOnFault() bool {
return *(*bool)(add(unsafe.Pointer(g), offsetPaniconfault))
}
//go:norace
func (g *g) setPanicOnFault(new bool) (old bool) {
panicOnFault := (*bool)(add(unsafe.Pointer(g), offsetPaniconfault))
old = *panicOnFault
*panicOnFault = new
return old
}
//go:norace
func (g *g) getLabels() unsafe.Pointer {
return *(*unsafe.Pointer)(add(unsafe.Pointer(g), offsetLabels))
}
//go:norace
func (g *g) setLabels(labels unsafe.Pointer) {
*(*unsafe.Pointer)(add(unsafe.Pointer(g), offsetLabels)) = labels
}
// getg returns current coroutine struct.
func getg() *g {
gp := getgp()
if gp == nil {
panic("Failed to get gp from runtime natively.")
}
return gp
}
// 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)
}