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