Remove for loop from mutex
This commit is contained in:
parent
f335d71aba
commit
ec8ece9878
1 changed files with 22 additions and 14 deletions
20
db/mutex.go
20
db/mutex.go
|
@ -35,13 +35,7 @@ func (m *mutex) Lock(ctx context.Context, key fmt.Stringer) {
|
|||
|
||||
_, v := m.item(ctx, key)
|
||||
|
||||
for {
|
||||
select {
|
||||
default:
|
||||
if atomic.LoadUint32(&v.v) < vers(ctx) {
|
||||
close(v.q)
|
||||
panic(errRaceCondition)
|
||||
}
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-v.q:
|
||||
|
@ -49,9 +43,23 @@ func (m *mutex) Lock(ctx context.Context, key fmt.Stringer) {
|
|||
case v.l <- struct{}{}:
|
||||
atomic.StoreUint32(&v.v, vers(ctx))
|
||||
return
|
||||
default:
|
||||
if atomic.LoadUint32(&v.v) < vers(ctx) {
|
||||
close(v.q)
|
||||
panic(errRaceCondition)
|
||||
}
|
||||
}
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-v.q:
|
||||
return
|
||||
case v.l <- struct{}{}:
|
||||
atomic.StoreUint32(&v.v, vers(ctx))
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (m *mutex) Unlock(ctx context.Context, key fmt.Stringer) {
|
||||
|
|
Loading…
Reference in a new issue