-
Notifications
You must be signed in to change notification settings - Fork 1
/
map_mutex.go
61 lines (46 loc) · 1.04 KB
/
map_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
package builder
import "sync"
type MapMutex struct {
innerMap map[string]*sync.RWMutex
mutex *sync.RWMutex
}
func NewMapMutex() *MapMutex {
return &MapMutex{
innerMap: make(map[string]*sync.RWMutex),
mutex: &sync.RWMutex{},
}
}
func (m *MapMutex) getKeyLock(key string) *sync.RWMutex {
// set read lock to check if key exists
m.mutex.RLock()
if lock, ok := m.innerMap[key]; ok {
m.mutex.RUnlock()
return lock
}
m.mutex.RUnlock()
// set write lock to add lock to initialize key
m.mutex.Lock()
// double check if it hasn't been created in the meantime
if lock, ok := m.innerMap[key]; ok {
m.mutex.Unlock()
return lock
}
// otherwise create it
lock := &sync.RWMutex{}
m.innerMap[key] = lock
m.mutex.Unlock()
// return lock
return lock
}
func (m *MapMutex) RLock(key string) {
m.getKeyLock(key).RLock()
}
func (m *MapMutex) RUnlock(key string) {
m.getKeyLock(key).RUnlock()
}
func (m *MapMutex) Lock(key string) {
m.getKeyLock(key).Lock()
}
func (m *MapMutex) Unlock(key string) {
m.getKeyLock(key).Unlock()
}