-
Notifications
You must be signed in to change notification settings - Fork 0
/
locker.go
124 lines (104 loc) · 2.75 KB
/
locker.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
package fcache
import (
"sync"
"sync/atomic"
)
// Locker provides a key based locking mechanism
type Locker struct {
holderPool sync.Pool
mu sync.Mutex
locks map[uint64]*lockHolder
}
type lockHolder struct {
lock sync.RWMutex
users uint32 // users is the number of users currently interacting with the lock
upgrading atomic.Int32
}
// NewLocker creates a new Locker ready to be used
func NewLocker() *Locker {
return &Locker{
holderPool: sync.Pool{New: func() any { return new(lockHolder) }},
locks: make(map[uint64]*lockHolder),
}
}
func (l *Locker) prepareToLock(key uint64) *lockHolder {
l.mu.Lock()
holder, exists := l.locks[key]
if !exists {
holder = l.holderPool.Get().(*lockHolder)
l.locks[key] = holder
}
holder.users += 1
l.mu.Unlock()
return holder
}
// Lock locks a mutex with the given key for writing. If no mutex for that key exists it is created.
func (l *Locker) Lock(key uint64) {
holder := l.prepareToLock(key)
lock:
holder.lock.Lock()
// Upgrade gets priority, retry lock if upgrade is in progress.
if holder.upgrading.Load() > 0 {
holder.lock.Unlock()
goto lock
}
}
// RLock locks a mutex with the given key for reading. If no mutex for that key exists it is created.
func (l *Locker) RLock(key uint64) {
holder := l.prepareToLock(key)
holder.lock.RLock()
}
// Upgrade converts a read lock to a write lock for the given key.
// If the upgrade would deadlock or is not possible it returns false.
// Inspired by https://github.com/kawasin73/umutex
func (l *Locker) Upgrade(key uint64) (success bool) {
l.mu.Lock()
holder, exists := l.locks[key]
if exists {
l.mu.Unlock()
success = holder.upgrading.Add(1) == 1
if success {
holder.lock.RUnlock()
holder.lock.Lock()
}
holder.upgrading.Add(-1)
} else {
l.mu.Unlock()
}
return success
}
func (l *Locker) prepareToUnlock(key uint64) *sync.RWMutex {
l.mu.Lock()
holder, exists := l.locks[key]
if !exists {
l.mu.Unlock()
return nil
}
holder.users -= 1 // about to unlock so we can decrement the number of users for this lock
if holder.users == 0 { // if this was the last user we also remove it from the map
delete(l.locks, key)
l.holderPool.Put(holder) // potential early reuse by another key before final unlock should not be a problem
}
l.mu.Unlock()
return &holder.lock
}
// Unlock unlocks the mutex with the given key
func (l *Locker) Unlock(key uint64) {
lock := l.prepareToUnlock(key)
if lock != nil {
lock.Unlock()
}
}
// RUnlock unlocks the mutex with the given key
func (l *Locker) RUnlock(key uint64) {
lock := l.prepareToUnlock(key)
if lock != nil {
lock.RUnlock()
}
}
// Size returns the number of internal locks
func (l *Locker) Size() int {
l.mu.Lock()
defer l.mu.Unlock()
return len(l.locks)
}