/
sync.go
93 lines (81 loc) · 1.37 KB
/
sync.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package sync
import (
"os"
"runtime"
"runtime/pprof"
"sync"
"time"
)
var enabled = false
var (
lockHolders *pprof.Profile
lockBlockers *pprof.Profile
// mu sync.Mutex
)
func init() {
if os.Getenv("PPROF_SYNC") != "" {
enabled = true
}
if enabled {
lockHolders = pprof.NewProfile("lockHolders")
lockBlockers = pprof.NewProfile("lockBlockers")
}
}
type lockAction struct {
time.Time
*Mutex
Stack string
}
func stack() string {
var buf [0x1000]byte
n := runtime.Stack(buf[:], false)
return string(buf[:n])
}
type Mutex struct {
mu sync.Mutex
hold *int
}
func (m *Mutex) newAction() *lockAction {
return &lockAction{
time.Now(),
m,
stack(),
}
}
func (m *Mutex) Lock() {
if !enabled {
m.mu.Lock()
return
}
v := new(int)
lockBlockers.Add(v, 0)
m.mu.Lock()
lockBlockers.Remove(v)
m.hold = v
lockHolders.Add(v, 0)
}
func (m *Mutex) Unlock() {
if enabled {
lockHolders.Remove(m.hold)
}
m.mu.Unlock()
}
type WaitGroup struct {
sync.WaitGroup
}
type Cond struct {
sync.Cond
}
// This RWMutex's RLock and RUnlock methods don't allow shared reading because
// there's no way to determine what goroutine has stopped holding the read
// lock when RUnlock is called. So for debugging purposes, it's just like
// Mutex.
type RWMutex struct {
Mutex
}
func (me *RWMutex) RLock() {
me.Lock()
}
func (me *RWMutex) RUnlock() {
me.Unlock()
}