From e9ab65c230611a436ca4ec66a3e1d9f761d6359d Mon Sep 17 00:00:00 2001 From: joshvanl Date: Mon, 19 Aug 2024 10:57:34 -0500 Subject: [PATCH] concurrency/mutexmap Move Unlock to after operation Signed-off-by: joshvanl --- concurrency/mutexmap.go | 47 +++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/concurrency/mutexmap.go b/concurrency/mutexmap.go index 7a87ed9..a71e22d 100644 --- a/concurrency/mutexmap.go +++ b/concurrency/mutexmap.go @@ -58,50 +58,57 @@ func (a *mutexMap[T]) Lock(key T) { a.lock.RLock() mutex, ok := a.items[key] a.lock.RUnlock() + if ok { + mutex.Lock() + return + } + + a.lock.Lock() + mutex, ok = a.items[key] if !ok { - a.lock.Lock() - mutex, ok = a.items[key] - if !ok { - mutex = &sync.RWMutex{} - a.items[key] = mutex - } - a.lock.Unlock() + mutex = &sync.RWMutex{} + a.items[key] = mutex } + a.lock.Unlock() mutex.Lock() } func (a *mutexMap[T]) Unlock(key T) { a.lock.RLock() mutex, ok := a.items[key] - a.lock.RUnlock() if ok { mutex.Unlock() } + a.lock.RUnlock() } func (a *mutexMap[T]) RLock(key T) { a.lock.RLock() mutex, ok := a.items[key] a.lock.RUnlock() + + if ok { + mutex.RLock() + return + } + + a.lock.Lock() + mutex, ok = a.items[key] if !ok { - a.lock.Lock() - mutex, ok = a.items[key] - if !ok { - mutex = &sync.RWMutex{} - a.items[key] = mutex - } - a.lock.Unlock() + mutex = &sync.RWMutex{} + a.items[key] = mutex } + a.lock.Unlock() mutex.RLock() } func (a *mutexMap[T]) RUnlock(key T) { a.lock.RLock() mutex, ok := a.items[key] - a.lock.RUnlock() if ok { mutex.RUnlock() } + a.lock.RUnlock() } func (a *mutexMap[T]) Delete(key T) { @@ -113,21 +120,21 @@ func (a *mutexMap[T]) Delete(key T) { func (a *mutexMap[T]) DeleteUnlock(key T) { a.lock.Lock() mutex, ok := a.items[key] - delete(a.items, key) - a.lock.Unlock() if ok { mutex.Unlock() } + delete(a.items, key) + a.lock.Unlock() } func (a *mutexMap[T]) DeleteRUnlock(key T) { a.lock.Lock() mutex, ok := a.items[key] - delete(a.items, key) - a.lock.Unlock() if ok { mutex.RUnlock() } + delete(a.items, key) + a.lock.Unlock() } func (a *mutexMap[T]) Clear() {