-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariablerw.go
172 lines (148 loc) · 3.25 KB
/
variablerw.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
package multilock
import "sync"
func NewRWVariable[K comparable]() MultiRWLocker[K] {
m := &variableRW[K]{f: &defaultRWLockerFactory{}, locks: map[K]refTryRWLocker{}}
return m
}
type variableRW[K comparable] struct {
f rwLockerFactory
// 最初的版本是没有m的 然后unlock也没加锁。测试发现有bug
// 所以就把sync.Map换成了普通map,然后map操作的时候加锁
m sync.Mutex
locks map[K]refTryRWLocker
}
func (ml *variableRW[K]) TryRLockByKey(k K) (got bool) {
_, got = ml.tryRLock(k)
return
}
func (ml *variableRW[K]) RLockByKey(k K) {
ml.getLockerAndIncRefCount(k).RLock()
}
func (ml *variableRW[K]) RUnlockByKey(k K) {
ml.m.Lock()
l, ok := ml.locks[k]
ml.m.Unlock()
if !ok {
panic("sync: Unlock of unlocked RWMutex")
}
ml.rUnlock(k, l)()
}
func (ml *variableRW[K]) TryLockByKey(k K) (got bool) {
_, got = ml.tryLock(k)
return
}
func (ml *variableRW[K]) LockByKey(k K) {
ml.getLockerAndIncRefCount(k).Lock()
}
func (ml *variableRW[K]) UnlockByKey(k K) {
ml.m.Lock()
l, ok := ml.locks[k]
ml.m.Unlock()
if !ok {
panic("sync: Unlock of unlocked RWMutex")
}
ml.unlock(k, l)()
}
func (ml *variableRW[K]) TryRLock(k K) (got bool, unlock func()) {
l, got := ml.tryRLock(k)
if got {
return true, ml.rUnlock(k, l)
}
return false, noop
}
func (ml *variableRW[K]) RLock(k K) (unlock func()) {
l := ml.getLockerAndIncRefCount(k)
l.RLock()
return ml.rUnlock(k, l)
}
func (ml *variableRW[K]) TryLock(k K) (got bool, unlock func()) {
l, got := ml.tryLock(k)
if got {
return true, ml.unlock(k, l)
}
return false, noop
}
func (ml *variableRW[K]) Lock(k K) (unlock func()) {
l := ml.getLockerAndIncRefCount(k)
l.Lock()
return ml.unlock(k, l)
}
func (ml *variableRW[K]) tryLock(k K) (l refTryRWLocker, got bool) {
ml.withMutex(func() {
l = ml._getLockerAndIncRefCount(k)
if l.TryLock() {
got = true
return
}
ml.tryPut(k, l)
got = false
})
return
}
func (ml *variableRW[K]) tryRLock(k K) (l refTryRWLocker, got bool) {
ml.withMutex(func() {
l = ml._getLockerAndIncRefCount(k)
if l.TryRLock() {
got = true
return
}
ml.tryPut(k, l)
got = false
})
return
}
func (ml *variableRW[K]) unlock(k K, l refTryRWLocker) func() {
// 必须要锁 不然这两个操作不原子 不正确
return func() {
ml.withMutex(func() {
l.Unlock()
ml.tryPut(k, l)
})
}
}
func (ml *variableRW[K]) rUnlock(k K, l refTryRWLocker) func() {
return func() {
ml.withMutex(func() {
l.RUnlock()
ml.tryPut(k, l)
})
}
}
func (ml *variableRW[K]) tryPut(k K, m refTryRWLocker) {
v := m.DecRefCount()
if v == 0 {
delete(ml.locks, k)
ml.f.Put(m)
}
}
func (ml *variableRW[K]) tryPutWithMutex(k K, m refTryRWLocker) {
ml.withMutex(func() {
ml.tryPut(k, m)
})
}
func (ml *variableRW[K]) getLockerAndIncRefCount(k K) (l refTryRWLocker) {
ml.withMutex(func() {
l = ml._getLockerAndIncRefCount(k)
})
return
}
func (ml *variableRW[K]) withMutex(f func()) {
ml.m.Lock()
defer ml.m.Unlock()
f()
}
func (ml *variableRW[K]) _getLockerAndIncRefCount(k K) refTryRWLocker {
if ml.f == nil {
ml.f = &defaultRWLockerFactory{}
}
if ml.locks == nil {
ml.locks = map[K]refTryRWLocker{}
}
locker, ok := ml.locks[k]
if !ok {
locker = ml.f.Get()
ml.locks[k] = locker
}
locker.IncRefCount()
return locker
}