-
Notifications
You must be signed in to change notification settings - Fork 0
/
locker.go
89 lines (72 loc) · 1.93 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
package fcache
import (
"sync"
)
// 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 is the number of users currently interacting with the lock
users 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) *sync.RWMutex {
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
}
// 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) {
lock := l.prepareToLock(key)
lock.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) {
lock := l.prepareToLock(key)
lock.RLock()
}
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()
}
}