From ec8ece9878ac8ab0db8e7f46d0a1030734024979 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sat, 28 Apr 2018 22:08:19 +0100 Subject: [PATCH] Remove for loop from mutex --- db/mutex.go | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/db/mutex.go b/db/mutex.go index 25894cb3..a1847c4e 100644 --- a/db/mutex.go +++ b/db/mutex.go @@ -35,23 +35,31 @@ 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: - return - case v.l <- struct{}{}: - 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 + 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) {