-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmplock_rw.go
231 lines (186 loc) · 4.39 KB
/
mplock_rw.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package asdf
import (
"sync/atomic"
"unsafe"
)
type MpLockRw struct {
r mplock
w mplock
}
func (me *MpLockRw) dump(role mplock_role_t, act mplock_act_t, tag string) {
if MpLockDump {
Log.Info("%s role[%s] act[%s]: reader[want:%d, value:%d] writer[want:%d, value:%d]",
tag,
role,
act,
me.r.want(), me.r.value(),
me.w.want(), me.w.value())
}
}
func (me *MpLockRw) split(role mplock_role_t, self *mplock, other *mplock) {
if MPLOCK_READER == role {
self.v = me.r.v
other.v = me.w.v
} else {
self.v = me.w.v
other.v = me.r.v
}
}
func (me *MpLockRw) merge(role mplock_role_t, self *mplock, other *mplock) {
if MPLOCK_READER == role {
me.r.v = self.v
me.w.v = other.v
} else {
me.w.v = self.v
me.r.v = other.v
}
}
type mplock_rw_try_f func() bool
type mplock_rw_do_f func(self *mplock, other *mplock)
func (me *MpLockRw) until_ok(try mplock_rw_try_f) bool {
for {
if try() {
return true
} else {
MpLockPause()
}
}
return false
}
func (me *MpLockRw) try(
role mplock_role_t,
act mplock_act_t,
can mplock_can_f,
exec mplock_rw_do_f,
want mplock_rw_do_f) bool {
//--------------------------------------------------------------------------
var new_, old_ MpLockRw
var self, other mplock
v := atomic.LoadUint32(me.Address())
old_.Update(v)
old_.split(role, &self, &other)
if err := mplock_check(role, &self, &other); nil != err {
return false
}
switch can(role, &self, &other) {
case MPLOCK_CAN_EXEC:
exec(&self, &other)
new_.merge(role, &self, &other)
if atomic.CompareAndSwapUint32(me.Address(), v, new_.Value()) {
old_.dump(role, act, "mplock-rw exec old")
new_.dump(role, act, "mplock-rw exec new")
return true
}
case MPLOCK_CAN_WANT:
want(&self, &other)
new_.merge(role, &self, &other)
if atomic.CompareAndSwapUint32(me.Address(), v, new_.Value()) {
old_.dump(role, act, "mplock-rw want old")
new_.dump(role, act, "mplock-rw want new")
// do want, NOT do lock, return false
}
}
return false
}
func mplock_rw_want(self *mplock, other *mplock) {
if other.want() {
Panic("other have wanted")
}
self.set_want(true)
}
func mplock_rw_lock(self *mplock, other *mplock) {
if other.want() {
Panic("other have wanted")
} else if other.locked() {
Panic("other have locked")
}
self.add()
self.set_want(false)
}
func mplock_rw_unlock(self *mplock, other *mplock) {
self.sub()
}
func mplock_rw_upgrade(self *mplock, other *mplock) {
self.add()
other.sub()
}
func mplock_rw_degrade(self *mplock, other *mplock) {
self.add()
other.sub()
}
func (me *MpLockRw) lock(role mplock_role_t) bool {
return me.try(role, MPLOCK_ACT_LOCK, mplock_can_lock, mplock_rw_lock, mplock_rw_want)
}
func (me *MpLockRw) unlock(role mplock_role_t) bool {
return me.try(role, MPLOCK_ACT_UNLOCK, mplock_can_unlock, mplock_rw_unlock, nil)
}
func (me *MpLockRw) upgrade(role mplock_role_t) bool {
return me.try(role, MPLOCK_ACT_UPGRADE, mplock_can_upgrade, mplock_rw_upgrade, nil)
}
func (me *MpLockRw) degrade(role mplock_role_t) bool {
return me.try(role, MPLOCK_ACT_DEGRADE, mplock_can_degrade, mplock_rw_degrade, nil)
}
func (me *MpLockRw) Address() *uint32 {
return (*uint32)(unsafe.Pointer(me))
}
func (me *MpLockRw) Value() uint32 {
return *me.Address()
}
func (me *MpLockRw) Update(v uint32) {
*me.Address() = v
}
func (me *MpLockRw) Init() {
me.Update(0)
}
func (me *MpLockRw) LockR() bool {
return me.until_ok(func() bool {
return me.lock(MPLOCK_READER)
})
}
func (me *MpLockRw) LockW() bool {
return me.until_ok(func() bool {
return me.lock(MPLOCK_WRITER)
})
}
func (me *MpLockRw) UnLockR() bool {
return me.until_ok(func() bool {
return me.unlock(MPLOCK_READER)
})
}
func (me *MpLockRw) UnLockW() bool {
return me.until_ok(func() bool {
return me.unlock(MPLOCK_WRITER)
})
}
func (me *MpLockRw) Upgrade_rtow() bool {
return me.until_ok(func() bool {
return me.upgrade(MPLOCK_READER)
})
}
func (me *MpLockRw) Upgrade_wtor() bool {
return me.until_ok(func() bool {
return me.upgrade(MPLOCK_WRITER)
})
}
func (me *MpLockRw) Degrade_rtow() bool {
return me.until_ok(func() bool {
return me.degrade(MPLOCK_READER)
})
}
func (me *MpLockRw) Degrade_wtor() bool {
return me.until_ok(func() bool {
return me.degrade(MPLOCK_WRITER)
})
}
func (me *MpLockRw) HandleR(handle func()) {
if me.LockR() {
handle()
me.UnLockR()
}
}
func (me *MpLockRw) HandleW(handle func()) {
if me.LockW() {
handle()
me.UnLockW()
}
}