This repository was archived by the owner on Jan 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathlock.go
157 lines (132 loc) · 2.41 KB
/
lock.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package sync2
import (
"fmt"
"sync"
"sync/atomic"
)
type Spinlock int32
func (s *Spinlock) Lock() {
for {
for atomic.LoadInt32((*int32)(s)) != 0 {
}
if atomic.CompareAndSwapInt32((*int32)(s), 0, 1) {
return
}
}
}
func (s *Spinlock) Unlock() {
if !atomic.CompareAndSwapInt32((*int32)(s), 1, 0) {
panic("unlock unlocked spinlock")
}
}
type rwLocker interface {
sync.Locker
RLock()
RUnlock()
Ref() int
SetRef(int)
}
type mutexRef int
func (m *mutexRef) Ref() int {
return int(*m)
}
func (m *mutexRef) SetRef(ref int) {
*m = mutexRef(ref)
}
type refRWMutex struct {
mutexRef
sync.RWMutex
}
type refMutex struct {
mutexRef
sync.Mutex
}
func (r *refMutex) RLock() {
r.Mutex.Lock()
}
func (r *refMutex) RUnlock() {
r.Mutex.Unlock()
}
type AutorefMutex struct {
mu sync.Mutex
pool sync.Pool
mus map[string]rwLocker
}
func NewAutorefMutex(rw bool) *AutorefMutex {
var new func() interface{}
if rw {
new = func() interface{} {
return &refRWMutex{}
}
} else {
new = func() interface{} {
return &refMutex{}
}
}
return &AutorefMutex{
pool: sync.Pool{
New: new,
},
mus: make(map[string]rwLocker),
}
}
func (m *AutorefMutex) newLocker(key string) rwLocker {
mu := m.pool.Get().(rwLocker)
mu.SetRef(0)
m.mus[key] = mu
return mu
}
func (m *AutorefMutex) freeLocker(mu rwLocker) {
m.pool.Put(mu)
}
func (m *AutorefMutex) locker(key string, unlock bool) (recyle bool, mu rwLocker) {
m.mu.Lock()
mu, has := m.mus[key]
if unlock {
if !has {
m.mu.Unlock()
panic("unlock unexisted key")
}
ref := mu.Ref()
if ref < 1 {
m.mu.Unlock()
panic(fmt.Sprintf("impossible reference count: %d for key: %s", ref, key))
}
mu.SetRef(ref - 1)
recyle = ref == 1
if recyle {
delete(m.mus, key)
}
} else {
if !has {
mu = m.newLocker(key)
}
mu.SetRef(mu.Ref() + 1)
}
m.mu.Unlock()
return recyle, mu
}
func (m *AutorefMutex) Lock(key string) {
_, mu := m.locker(key, false)
mu.Lock()
}
func (m *AutorefMutex) Unlock(key string) {
// unlock mean works done, it's safe to create another mutex with same key
// even if current mutex hasn't been unlocked really.
recyle, mu := m.locker(key, true)
mu.Unlock()
if recyle {
m.freeLocker(mu)
}
}
func (m *AutorefMutex) RLock(key string) {
_, mu := m.locker(key, false)
mu.RLock()
}
func (m *AutorefMutex) RUnlock(key string) {
recyle, mu := m.locker(key, true)
mu.RUnlock()
if recyle {
m.freeLocker(mu)
}
}