-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmutex.go
84 lines (67 loc) · 1.58 KB
/
kmutex.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
package kmutex
import "sync"
var ( // preset default instance
stdKmutex *KMutex
)
type MutexKey string // mutex key used to identify different mutexes
type kMutexItem struct { // key based mutex item
sync.Mutex
refCnt uint32
}
// KMutex key based multiple mutex, which is used to help improve locking performance by
// preventing a giant time-consuming locking.
type KMutex struct {
l sync.RWMutex // operation lock
s map[MutexKey]*kMutexItem // key mutex mapping
p *sync.Pool // mutex pool
}
// NewKMutex creates a new instance of KMutex
func NewKMutex() *KMutex {
return &KMutex{
s: make(map[MutexKey]*kMutexItem),
p: &sync.Pool{
New: func() interface{} {
return &kMutexItem{refCnt: 0}
},
},
}
}
// Lock locks by key
func (km *KMutex) Lock(key MutexKey) {
km.l.Lock()
lock, ok := km.s[key]
if !ok {
lock = km.p.Get().(*kMutexItem)
km.s[key] = lock
}
lock.refCnt++
km.l.Unlock() // must unlock km.l first, otherwise the next lock may block
lock.Lock()
}
// Unlock unlocks by key
func (km *KMutex) Unlock(key MutexKey) {
km.l.Lock()
defer km.l.Unlock()
lock, ok := km.s[key]
if !ok || lock.refCnt == 0 {
panic("must lock mutex before unlock")
}
lock.refCnt--
if lock.refCnt == 0 { // put back to sync pool
km.p.Put(lock)
delete(km.s, key)
}
lock.Unlock()
}
// init initializes the standard KMutex
func init() {
stdKmutex = NewKMutex()
}
// Lock locks the standard KMutex by key
func Lock(key MutexKey) {
stdKmutex.Lock(key)
}
// Unlock unlocks the standard KMutex by key
func Unlock(key MutexKey) {
stdKmutex.Unlock(key)
}