-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrwmutex.go
212 lines (182 loc) · 3.79 KB
/
rwmutex.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package xsync
import (
"context"
"fmt"
"runtime"
"sync"
"time"
"github.com/facebookincubator/go-belt/tool/logger"
)
type lockType uint
const (
lockTypeUndefined = lockType(iota)
lockTypeWrite
lockTypeRead
)
func (lt lockType) String() string {
switch lt {
case lockTypeUndefined:
return "<undefined>"
case lockTypeWrite:
return "Lock"
case lockTypeRead:
return "RLock"
default:
return fmt.Sprintf("<unexpected_value_%d>", uint(lt))
}
}
func fixCtx(ctx context.Context) context.Context {
if ctx == nil {
ctx = context.Background()
}
return ctx
}
type Mutex = RWMutex
type RWMutex struct {
mutex sync.RWMutex
OverrideTimeout time.Duration
PanicOnDeadlock *bool
cancelFunc context.CancelFunc
deadlockNotifier *time.Timer
}
func (m *Mutex) ManualRLock(ctx context.Context) {
m.manualLock(ctx, lockTypeRead)
}
func (m *Mutex) ManualLock(ctx context.Context) {
m.manualLock(ctx, lockTypeWrite)
}
func (m *Mutex) manualLock(ctx context.Context, lockType lockType) {
ctx = fixCtx(ctx)
noLogging := IsNoLogging(ctx)
l := logger.FromCtx(ctx)
if !noLogging {
l.Tracef("%sing", lockType)
}
switch lockType {
case lockTypeWrite:
m.mutex.Lock()
m.startDeadlockDetector(ctx)
case lockTypeRead:
m.mutex.RLock()
}
if !noLogging {
l.Tracef("%sed", lockType)
}
}
func (m *Mutex) startDeadlockDetector(ctx context.Context) {
ctx, m.cancelFunc = context.WithCancel(ctx)
timeout := time.Minute
if m.OverrideTimeout != 0 {
timeout = m.OverrideTimeout
}
if timeout <= 0 {
return
}
deadlockNotifier := time.NewTimer(time.Minute)
go func() {
select {
case <-ctx.Done():
return
case <-deadlockNotifier.C:
}
allStacks := make([]byte, 1024*1024)
n := runtime.Stack(allStacks, true)
allStacks = allStacks[:n]
if IsEnableDeadlock(ctx) {
if m.PanicOnDeadlock == nil || *m.PanicOnDeadlock {
logger.Panicf(ctx, "got a deadlock in:\n%s", allStacks)
} else {
logger.Errorf(ctx, "got a deadlock in:\n%s", allStacks)
}
}
}()
m.deadlockNotifier = deadlockNotifier
}
func (m *Mutex) ManualTryRLock(ctx context.Context) bool {
return m.manualTryLock(ctx, lockTypeRead)
}
func (m *Mutex) ManualTryLock(ctx context.Context) bool {
return m.manualTryLock(ctx, lockTypeWrite)
}
func (m *Mutex) manualTryLock(ctx context.Context, lockType lockType) bool {
ctx = fixCtx(ctx)
noLogging := IsNoLogging(ctx)
l := logger.FromCtx(ctx)
if !noLogging {
l.Tracef("Try%sing", lockType)
}
var result bool
switch lockType {
case lockTypeWrite:
result = m.mutex.TryLock()
if result {
m.startDeadlockDetector(ctx)
}
case lockTypeRead:
result = m.mutex.TryRLock()
}
if !noLogging {
l.Tracef("Try%sed, result: %v", lockType, result)
}
return result
}
func (m *Mutex) ManualRUnlock(ctx context.Context) {
m.manualUnlock(ctx, lockTypeRead)
}
func (m *Mutex) ManualUnlock(ctx context.Context) {
m.manualUnlock(ctx, lockTypeWrite)
}
func (m *Mutex) manualUnlock(ctx context.Context, lockType lockType) {
ctx = fixCtx(ctx)
noLogging := IsNoLogging(ctx)
l := logger.FromCtx(ctx)
if !noLogging {
l.Tracef("un%sing", lockType)
}
switch lockType {
case lockTypeWrite:
if m.deadlockNotifier != nil {
m.deadlockNotifier.Stop()
m.cancelFunc()
m.deadlockNotifier, m.cancelFunc = nil, nil
}
m.mutex.Unlock()
case lockTypeRead:
m.mutex.RUnlock()
}
if !noLogging {
l.Tracef("un%sed", lockType)
}
}
func (m *Mutex) Do(
ctx context.Context,
fn func(),
) {
m.ManualLock(ctx)
defer m.ManualUnlock(ctx)
fn()
}
func (m *Mutex) RDo(
ctx context.Context,
fn func(),
) {
m.ManualRLock(ctx)
defer m.ManualRUnlock(ctx)
fn()
}
func (m *Mutex) UDo(
ctx context.Context,
fn func(),
) {
m.ManualUnlock(ctx)
defer m.ManualLock(ctx)
fn()
}
func (m *Mutex) URDo(
ctx context.Context,
fn func(),
) {
m.ManualRUnlock(ctx)
defer m.ManualRLock(ctx)
fn()
}