-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrw_mutex.go
384 lines (338 loc) · 7.86 KB
/
rw_mutex.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package gorex
import (
"context"
"fmt"
"sync"
"github.com/xaionaro-go/spinlock"
)
// RWMutex is a goroutine-aware analog of sync.RWMutex, so it works
// the same way as sync.RWMutex, but tracks which goroutine locked
// it. So it could be locked multiple times with the same routine.
type RWMutex struct {
// InfiniteContext is used as the default context used on any try to lock if
// a custom context is not set (see LockCtx/RLockCtx), but with the difference
// if this context will be done, then it will panic with debugging information.
//
// To specify a context with deadline may be useful for unit tests.
//
// The zero-value means to use DefaultInfiniteContext.
InfiniteContext context.Context
lazyInitOnce sync.Once
rlockDone chan struct{}
lockDone chan struct{}
lockCount int
lockedBy GoroutineID
rlockCount int64
backendLocker sync.Mutex
internalLocker spinlock.Locker
usedBy map[GoroutineID]*int64
int64Pool int64Pool
gcCallCount uint8
}
func (m *RWMutex) lazyInit() {
m.lazyInitOnce.Do(func() {
m.usedBy = map[GoroutineID]*int64{}
})
}
// Lock is analog of `(*sync.RWMutex)`.Lock, but it allows one goroutine
// to call it and RLock multiple times without calling Unlock/RUnlock.
func (m *RWMutex) Lock() {
if !m.lock(nil, true) {
panic("should not happen")
}
}
// LockTry is analog of Lock(), but it does not block if it cannot lock
// right away.
//
// Returns `false` if was unable to lock.
func (m *RWMutex) LockTry() bool {
return m.lock(nil, false)
}
// LockCtx is analog of Lock(), but allows to continue the try to lock only until context is done.
//
// Returns `false` if was unable to lock (context finished before it was possible to lock).
func (m *RWMutex) LockCtx(ctx context.Context) bool {
return m.lock(ctx, true)
}
func (m *RWMutex) infiniteContext() context.Context {
if m.InfiniteContext == nil {
return DefaultInfiniteContext
}
return m.InfiniteContext
}
func (m *RWMutex) lock(ctx context.Context, shouldWait bool) bool {
m.lazyInit()
me := GetGoroutineID()
m.internalLocker.Lock()
if m.lockedBy == me {
// already locked by me
m.lockCount++
m.internalLocker.Unlock()
return true
}
if !m.setLockedByMe(ctx, me, shouldWait) {
return false
}
goroutineOpenedLock(m, true)
m.internalLocker.Unlock()
m.backendLocker.Lock()
return true
}
func (m *RWMutex) setLockedByMe(
ctx context.Context,
me GoroutineID,
shouldWait bool,
) (result bool) {
defer func() {
if !result {
return
}
m.lockCount++
m.lockedBy = me
}()
for {
if m.lockCount == 0 {
if m.rlockCount == 0 {
return true
}
if myReadersCountPtr, _ := m.usedBy[me]; myReadersCountPtr != nil {
if m.rlockCount-*myReadersCountPtr == 0 {
return true
}
}
}
if !shouldWait {
m.internalLocker.Unlock()
return false
}
if m.rlockDone == nil {
m.rlockDone = make(chan struct{})
}
rlockDone := m.rlockDone
if m.lockDone == nil {
m.lockDone = make(chan struct{})
}
lockDone := m.lockDone
isInfiniteContext := false
if ctx == nil {
ctx = m.infiniteContext()
isInfiniteContext = true
}
m.internalLocker.Unlock()
select {
case <-rlockDone:
case <-lockDone:
case <-ctx.Done():
if isInfiniteContext {
m.debugPanic()
}
return false
}
m.internalLocker.Lock()
}
}
// Unlock is analog of `(*sync.RWMutex)`.Unlock, but it cannot be called
// from a routine which does not hold the lock (see `Lock`).
func (m *RWMutex) Unlock() {
me := GetGoroutineID()
m.internalLocker.Lock()
switch {
case m.lockedBy == 0:
m.internalLocker.Unlock()
panic("An attempt to unlock a non-locked mutex.")
case me != m.lockedBy:
m.internalLocker.Unlock()
panic(fmt.Sprintf("I'm not the one, who locked this mutex: %X != %X", me, m.lockedBy))
}
m.lockCount--
if m.lockCount == 0 {
m.lockedBy = 0
goroutineClosedLock(m, true)
m.backendLocker.Unlock()
}
chPtr := m.lockDone
m.lockDone = nil
m.internalLocker.Unlock()
if chPtr != nil {
close(chPtr)
}
}
// LockDo is a wrapper around Lock and Unlock.
// It's a handy function to see in the call stack trace which locker where was locked.
// Also it's handy not to forget to unlock the locker.
func (m *RWMutex) LockDo(fn func()) {
m.Lock()
defer m.Unlock()
fn()
}
// LockTryDo is a wrapper around LockTry and Unlock.
//
// See also LockDo and LockTry.
func (m *RWMutex) LockTryDo(fn func()) (success bool) {
if !m.LockTry() {
return false
}
defer m.Unlock()
success = true
fn()
return
}
// LockCtxDo is a wrapper around LockCtx and Unlock.
//
// See also LockDo and LockCtx.
func (m *RWMutex) LockCtxDo(ctx context.Context, fn func()) (success bool) {
if !m.LockCtx(ctx) {
return false
}
defer m.Unlock()
success = true
fn()
return
}
func (m *RWMutex) incMyReaders(me GoroutineID) {
if v := m.usedBy[me]; v == nil {
m.usedBy[me] = m.int64Pool.get()
goroutineOpenedLock(m, false)
} else {
*v++
}
m.rlockCount++
}
func (m *RWMutex) gc() {
m.gcCallCount++
if m.gcCallCount != 0 {
return
}
for k, v := range m.usedBy {
if *v != 0 {
continue
}
delete(m.usedBy, k)
m.int64Pool.put(v)
}
}
func (m *RWMutex) decMyReaders(me GoroutineID) {
m.rlockCount--
v := m.usedBy[me]
if v == nil || *v == 0 {
panic("RUnlock()-ing not RLock()-ed")
}
*v--
if *v != 0 {
return
}
goroutineClosedLock(m, false)
m.gc()
ch := m.rlockDone
if ch == nil {
return
}
close(ch)
m.rlockDone = nil
}
// RLock is analog of `(*sync.RWMutex)`.RLock, but it allows one goroutine
// to call Lock and RLock multiple times without calling Unlock/RUnlock.
func (m *RWMutex) RLock() {
m.rLock(nil, true)
}
// RLockTry is analog of RLock(), but it does not block if it cannot lock
// right away.
//
// Returns `false` if was unable to lock.
func (m *RWMutex) RLockTry() bool {
return m.rLock(nil, false)
}
// RLockCtx is analog of RLock(), but allows to continue the try to lock only until context is done.
//
// Returns `false` if was unable to lock.
func (m *RWMutex) RLockCtx(ctx context.Context) bool {
return m.rLock(ctx, true)
}
func (m *RWMutex) rLock(
ctx context.Context,
shouldWait bool,
) bool {
m.lazyInit()
me := GetGoroutineID()
m.internalLocker.Lock()
for {
if m.lockCount == 0 {
break
}
monopolizedBy := m.lockedBy
if monopolizedBy == me {
break
}
if !shouldWait {
m.internalLocker.Unlock()
return false
}
if m.lockDone == nil {
m.lockDone = make(chan struct{})
}
ch := m.lockDone
isInfiniteContext := false
if ctx == nil {
ctx = m.infiniteContext()
isInfiniteContext = true
}
m.internalLocker.Unlock()
select {
case <-ch:
case <-ctx.Done():
if isInfiniteContext {
m.debugPanic()
}
return false
}
m.internalLocker.Lock()
}
m.incMyReaders(me)
m.internalLocker.Unlock()
return true
}
// RUnlock is analog of `(*sync.RWMutex)`.RUnlock, but it cannot be called
// from a routine which does not hold the lock (see `RLock`).
func (m *RWMutex) RUnlock() {
me := GetGoroutineID()
m.internalLocker.Lock()
m.decMyReaders(me)
m.internalLocker.Unlock()
}
// RLockDo is a wrapper around RLock and RUnlock.
// It's a handy function to see in the call stack trace which locker where was locked.
// Also it's handy not to forget to unlock the locker.
func (m *RWMutex) RLockDo(fn func()) {
m.RLock()
defer m.RUnlock()
fn()
}
// RLockTryDo is a wrapper around RLockTry and RUnlock.
//
// See also RLockDo and RLockTry.
func (m *RWMutex) RLockTryDo(fn func()) (success bool) {
if !m.RLockTry() {
return false
}
defer m.RUnlock()
success = true
fn()
return
}
// RLockCtxDo is a wrapper around RLockTry and RUnlock.
//
// See also RLockDo and RLockCtx.
func (m *RWMutex) RLockCtxDo(ctx context.Context, fn func()) (success bool) {
if !m.RLockCtx(ctx) {
return false
}
defer m.RUnlock()
success = true
fn()
return
}
func (m *RWMutex) debugPanic() {
m.internalLocker.Lock()
defer m.internalLocker.Unlock()
debugPanic(m.lockedBy, m.usedBy)
}