-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixedrw.go
93 lines (77 loc) · 1.58 KB
/
fixedrw.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
package multilock
import (
"fmt"
"sync"
)
func NewFixedRW[K comparable](keys ...K) MultiRWLocker[K] {
//ms := map[K]*sync.RWMutex{}
//for _, key := range keys {
// ms[key] = &sync.RWMutex{}
//}
//return &fixedRW[K]{ms: ms}
f := fixedRW[K]{}
for _, key := range keys {
f[key] = &sync.RWMutex{}
}
return f
}
type fixedRW[K comparable] map[K]*sync.RWMutex
//type fixedRW[K comparable] struct {
// ms map[K]*sync.RWMutex
//}
func (f fixedRW[K]) TryRLockByKey(k K) bool {
return f.mustGet(k).TryRLock()
}
func (f fixedRW[K]) RLockByKey(k K) {
f.mustGet(k).RLock()
}
func (f fixedRW[K]) RUnlockByKey(k K) {
f.mustGet(k).RUnlock()
}
func (f fixedRW[K]) TryRLock(k K) (got bool, unlock func()) {
l := f.mustGet(k)
got = l.TryRLock()
if got {
unlock = func() { l.RUnlock() }
} else {
unlock = noop
}
return
}
func (f fixedRW[K]) RLock(k K) (unlock func()) {
l := f.mustGet(k)
l.RLock()
return func() { l.RUnlock() }
}
func (f fixedRW[K]) TryLockByKey(k K) bool {
return f.mustGet(k).TryLock()
}
func (f fixedRW[K]) LockByKey(k K) {
f.mustGet(k).Lock()
}
func (f fixedRW[K]) UnlockByKey(k K) {
f.mustGet(k).Unlock()
}
func (f fixedRW[K]) TryLock(k K) (got bool, unlock func()) {
l := f.mustGet(k)
got = l.TryLock()
if got {
unlock = func() { l.Unlock() }
} else {
unlock = noop
}
return
}
func (f fixedRW[K]) Lock(k K) (unlock func()) {
l := f.mustGet(k)
l.Lock()
return func() { l.Unlock() }
}
func (f fixedRW[K]) mustGet(k K) *sync.RWMutex {
//mutex := f.ms[k]
mutex := f[k]
if mutex == nil {
panic(fmt.Errorf("%w: %v", ErrLockKeyNotFound, k))
}
return mutex
}