Remove for loop from mutex

This commit is contained in:
Tobie Morgan Hitchcock 2018-04-28 22:08:19 +01:00
parent f335d71aba
commit ec8ece9878

View file

@ -35,13 +35,7 @@ func (m *mutex) Lock(ctx context.Context, key fmt.Stringer) {
_, v := m.item(ctx, key) _, v := m.item(ctx, key)
for {
select { select {
default:
if atomic.LoadUint32(&v.v) < vers(ctx) {
close(v.q)
panic(errRaceCondition)
}
case <-ctx.Done(): case <-ctx.Done():
return return
case <-v.q: case <-v.q:
@ -49,9 +43,23 @@ func (m *mutex) Lock(ctx context.Context, key fmt.Stringer) {
case v.l <- struct{}{}: case v.l <- struct{}{}:
atomic.StoreUint32(&v.v, vers(ctx)) atomic.StoreUint32(&v.v, vers(ctx))
return 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) { func (m *mutex) Unlock(ctx context.Context, key fmt.Stringer) {