Repository navigation
Expand file tree
/
Copy pathmutexes.go
More file actions
204 lines (179 loc) · 3.98 KB
/
Copy pathmutexes.go
File metadata and controls
204 lines (179 loc) · 3.98 KB
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
package portal
import (
"fmt"
"sort"
"sync"
)
// PortalMutex holds a mapping of keys to mutexes, as well as maintains a global mutex according to the given id.
type PortalMutex struct {
local *sync.Mutex
global *sync.Mutex
locks map[string]*sync.Mutex
vals map[string]bool
held map[string]int
}
// NewPortalMutex returns a new PortalMutex mapping.
func NewPortalMutex() *PortalMutex {
pm := new(PortalMutex)
pm.local = &sync.Mutex{}
pm.global = &sync.Mutex{}
pm.locks = make(map[string]*sync.Mutex)
pm.vals = make(map[string]bool)
pm.held = make(map[string]int)
//Make sure the mutexes are working, or else we panic!
pm.LockKey("")
pm.UnlockKey("")
pm.TestLock("")
return pm
}
// Exists returns true if each of the specified keys exists, else false.
func (pm *PortalMutex) Exists(keys ...string) bool {
if pm == nil || pm.locks == nil {
return false
}
pm.local.Lock()
defer pm.local.Unlock()
exists := false
for i := 0; i < len(keys); i++ {
for k := range pm.locks {
if k == keys[i] {
exists = true
break
}
}
if !exists {
return false
}
}
return exists //Remains false when no keys are passed to be checked.
}
// Lock attempts to lock all keys which are presently unlocked. Does nothing if all the keys were locked.
func (pm *PortalMutex) Lock() {
if pm == nil {
return
}
for lock := range pm.locks {
if pm.IsUnlocked(lock) {
pm.LockKey(lock)
pm.held[lock] = 1
}
}
}
// LockKey attempts to lock a key, creating the mutex for it if it has never been locked before.
func (pm *PortalMutex) LockKey(key ...string) {
if pm == nil {
return
}
lockedGlobal := false
for _, k := range key {
lock := pm.global
exists := true
if k != "" {
pm.local.Lock()
lock, exists = pm.locks[k]
pm.local.Unlock()
} else {
if lockedGlobal {
continue
}
lockedGlobal = true
}
if lock == nil || !exists {
lock = &sync.Mutex{}
pm.local.Lock()
pm.locks[k] = lock
pm.local.Unlock()
}
lock.Lock()
pm.local.Lock()
pm.vals[k] = true
pm.local.Unlock()
}
}
// Unlock frees the locks on any keys that were held from a previous call to Lock.
func (pm *PortalMutex) Unlock() {
if pm == nil {
return
}
for lock := range pm.held {
pm.UnlockKey(lock)
delete(pm.held, lock)
}
}
// UnlockKey attempts to unlock a previously locked key, but panics if the key was already unlocked or does not exist in the mutex map.
func (pm *PortalMutex) UnlockKey(key ...string) {
if pm == nil {
return
}
unlockedGlobal := false
for _, k := range key {
lock := pm.global
if k != "" {
pm.local.Lock()
lock = pm.locks[k]
pm.local.Unlock()
} else {
if unlockedGlobal {
continue
}
unlockedGlobal = true
}
if lock == nil {
continue
}
lock.Unlock()
pm.local.Lock()
pm.vals[k] = false
pm.local.Unlock()
}
}
// IsUnlocked returns true if the key is unlocked.
func (pm *PortalMutex) IsUnlocked(key string) bool {
if pm == nil {
return true
}
return !pm.IsLocked(key)
}
// IsLocked returns true if the key is locked.
func (pm *PortalMutex) IsLocked(key string) bool {
if pm == nil {
return false
}
pm.local.Lock()
defer pm.local.Unlock()
return pm.vals[key]
}
// TestLock panics if a key is not free for locking.
func (pm *PortalMutex) TestLock(key ...string) {
if pm == nil {
return
}
for _, k := range key {
if pm.IsLocked(k) {
panic(fmt.Sprintf("mutex: TestLock: %s", k))
}
}
}
func (pm *PortalMutex) String() (s string) {
if pm == nil {
return
}
keys := make([]string, 0)
for k := range pm.locks {
keys = append(keys, k)
}
sort.Strings(keys)
for i := 0; i < len(keys); i++ {
state := "null"
if pm.IsUnlocked(keys[i]) {
state = "unlocked"
} else {
state = "locked"
}
s += fmt.Sprintf("%s: %s\n", keys[i], state)
}
if s != "" {
s = s[:len(s)-1]
}
return
}