-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.go
59 lines (46 loc) · 1.69 KB
/
runtime.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
package goroutines
import (
"unsafe"
)
// Event types in the trace, args are given in square brackets.
// go tool trace will behave as per data-values inside square brackets.
const (
waitReasonChanReceive = 14
waitReasonChanSend = 15
traceEvGoBlockSend = 22 // goroutine blocks on chan send [timestamp, stack]
traceEvGoBlockRecv = 23 // goroutine blocks on chan recv [timestamp, stack]
waiting = 4 // must be in sync with _Gwaiting in runtime/runtime2.go
)
type g struct{}
type mutex struct {
key uintptr
}
//go:linkname lock runtime.lock
func lock(m *mutex)
//go:linkname unlock runtime.unlock
func unlock(m *mutex)
//go:linkname goparkunlock runtime.goparkunlock
func goparkunlock(lock *mutex, reason uint8, traceEv byte, traceskip int)
//go:linkname gopark runtime.gopark
func gopark(unlockf func(*g, unsafe.Pointer) bool, lock unsafe.Pointer, reason uint8, traceEv int, traceSkip int)
//go:linkname goready runtime.goready
func goready(gp *g, traceskip int)
//go:linkname readgstatus runtime.readgstatus
func readgstatus(gp *g) uint32
// Park puts calling go-routine in blocking state and invokes unlockFunc before blocking.
func Park(isSender bool, unlockFunc func(*g, unsafe.Pointer) bool, lck *mutex) {
lock(lck)
//clouse := (func(*g, unsafe.Pointer) bool)(unlockFunc) // todo: make it exported by making *g unsafe.Pointer
if isSender {
gopark(unlockFunc, unsafe.Pointer(lck), waitReasonChanSend, traceEvGoBlockSend, 1)
} else {
gopark(unlockFunc, unsafe.Pointer(lck), waitReasonChanReceive, traceEvGoBlockRecv, 1)
}
}
// Awake alters blocked/waiting rtn(go-routine) to running.
func Awake(rtn unsafe.Pointer) {
gp := (*g)(rtn)
if readgstatus(gp) == waiting {
goready(gp, 1)
}
}