Skip to content

Commit

Permalink
rename some receivers and variables
Browse files Browse the repository at this point in the history
  • Loading branch information
AdoAdoAdo committed Sep 18, 2024
1 parent ff57a9e commit ecfdff8
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 98 deletions.
76 changes: 38 additions & 38 deletions core/sync/keymutex.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,61 +16,61 @@ func NewKeyRWMutex() *keyRWMutex {
}

// RLock locks for read the Mutex for the given key
func (csa *keyRWMutex) RLock(key string) {
csa.getForRLock(key).rLock()
func (km *keyRWMutex) RLock(key string) {
km.getForRLock(key).rLock()
}

// RUnlock unlocks for read the Mutex for the given key
func (csa *keyRWMutex) RUnlock(key string) {
csa.getForRUnlock(key).rUnlock()
csa.cleanupMutex(key)
func (km *keyRWMutex) RUnlock(key string) {
km.getForRUnlock(key).rUnlock()
km.cleanupMutex(key)
}

// Lock locks the Mutex for the given key
func (csa *keyRWMutex) Lock(key string) {
csa.getForLock(key).lock()
func (km *keyRWMutex) Lock(key string) {
km.getForLock(key).lock()
}

// Unlock unlocks the Mutex for the given key
func (csa *keyRWMutex) Unlock(key string) {
csa.getForUnlock(key).unlock()
csa.cleanupMutex(key)
func (km *keyRWMutex) Unlock(key string) {
km.getForUnlock(key).unlock()
km.cleanupMutex(key)
}

// getForLock returns the Mutex for the given key, updating the Lock counter
func (csa *keyRWMutex) getForLock(key string) *rwMutex {
csa.mut.Lock()
defer csa.mut.Unlock()
func (km *keyRWMutex) getForLock(key string) *rwMutex {
km.mut.Lock()
defer km.mut.Unlock()

mutex, ok := csa.managedMutexes[key]
mutex, ok := km.managedMutexes[key]
if !ok {
mutex = csa.newInternalMutex(key)
mutex = km.newInternalMutex(key)
}
mutex.updateCounterLock()

return mutex
}

// getForRLock returns the Mutex for the given key, updating the RLock counter
func (csa *keyRWMutex) getForRLock(key string) *rwMutex {
csa.mut.Lock()
defer csa.mut.Unlock()
func (km *keyRWMutex) getForRLock(key string) *rwMutex {
km.mut.Lock()
defer km.mut.Unlock()

mutex, ok := csa.managedMutexes[key]
mutex, ok := km.managedMutexes[key]
if !ok {
mutex = csa.newInternalMutex(key)
mutex = km.newInternalMutex(key)
}
mutex.updateCounterRLock()

return mutex
}

// getForUnlock returns the Mutex for the given key, updating the Unlock counter
func (csa *keyRWMutex) getForUnlock(key string) *rwMutex {
csa.mut.Lock()
defer csa.mut.Unlock()
func (km *keyRWMutex) getForUnlock(key string) *rwMutex {
km.mut.Lock()
defer km.mut.Unlock()

mutex, ok := csa.managedMutexes[key]
mutex, ok := km.managedMutexes[key]
if ok {
mutex.updateCounterUnlock()
}
Expand All @@ -79,11 +79,11 @@ func (csa *keyRWMutex) getForUnlock(key string) *rwMutex {
}

// getForRUnlock returns the Mutex for the given key, updating the RUnlock counter
func (csa *keyRWMutex) getForRUnlock(key string) *rwMutex {
csa.mut.Lock()
defer csa.mut.Unlock()
func (km *keyRWMutex) getForRUnlock(key string) *rwMutex {
km.mut.Lock()
defer km.mut.Unlock()

mutex, ok := csa.managedMutexes[key]
mutex, ok := km.managedMutexes[key]
if ok {
mutex.updateCounterRUnlock()
}
Expand All @@ -92,27 +92,27 @@ func (csa *keyRWMutex) getForRUnlock(key string) *rwMutex {
}

// newInternalMutex creates a new mutex for the given key and adds it to the map
func (csa *keyRWMutex) newInternalMutex(key string) *rwMutex {
mutex, ok := csa.managedMutexes[key]
func (km *keyRWMutex) newInternalMutex(key string) *rwMutex {
mutex, ok := km.managedMutexes[key]
if !ok {
mutex = newRWMutex()
csa.managedMutexes[key] = mutex
km.managedMutexes[key] = mutex
}
return mutex
}

// cleanupMutex removes the mutex from the map if it is not used anymore
func (csa *keyRWMutex) cleanupMutex(key string) {
csa.mut.Lock()
defer csa.mut.Unlock()
func (km *keyRWMutex) cleanupMutex(key string) {
km.mut.Lock()
defer km.mut.Unlock()

mut, ok := csa.managedMutexes[key]
mut, ok := km.managedMutexes[key]
if ok && mut.numLocks() == 0 {
delete(csa.managedMutexes, key)
delete(km.managedMutexes, key)
}
}

// IsInterfaceNil returns true if there is no value under the interface
func (csa *keyRWMutex) IsInterfaceNil() bool {
return csa == nil
func (km *keyRWMutex) IsInterfaceNil() bool {
return km == nil
}
86 changes: 43 additions & 43 deletions core/sync/keymutex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,71 +13,71 @@ import (
func TestNewKeyMutex(t *testing.T) {
t.Parallel()

csa := NewKeyRWMutex()
require.NotNil(t, csa)
require.Equal(t, 0, len(csa.managedMutexes))
km := NewKeyRWMutex()
require.NotNil(t, km)
require.Equal(t, 0, len(km.managedMutexes))
}

func TestKeyMutex_Lock_Unlock(t *testing.T) {
t.Parallel()

csa := NewKeyRWMutex()
require.NotNil(t, csa)
require.Len(t, csa.managedMutexes, 0)
csa.Lock("id1")
require.Len(t, csa.managedMutexes, 1)
csa.Lock("id2")
require.Len(t, csa.managedMutexes, 2)
csa.Unlock("id1")
require.Len(t, csa.managedMutexes, 1)
csa.Unlock("id2")
require.Len(t, csa.managedMutexes, 0)
km := NewKeyRWMutex()
require.NotNil(t, km)
require.Len(t, km.managedMutexes, 0)
km.Lock("id1")
require.Len(t, km.managedMutexes, 1)
km.Lock("id2")
require.Len(t, km.managedMutexes, 2)
km.Unlock("id1")
require.Len(t, km.managedMutexes, 1)
km.Unlock("id2")
require.Len(t, km.managedMutexes, 0)
}

func TestKeyMutex_RLock_RUnlock(t *testing.T) {
t.Parallel()

csa := NewKeyRWMutex()
require.NotNil(t, csa)
require.Len(t, csa.managedMutexes, 0)
csa.RLock("id1")
require.Len(t, csa.managedMutexes, 1)
csa.RLock("id2")
require.Len(t, csa.managedMutexes, 2)
csa.RUnlock("id1")
require.Len(t, csa.managedMutexes, 1)
csa.RUnlock("id2")
require.Len(t, csa.managedMutexes, 0)
km := NewKeyRWMutex()
require.NotNil(t, km)
require.Len(t, km.managedMutexes, 0)
km.RLock("id1")
require.Len(t, km.managedMutexes, 1)
km.RLock("id2")
require.Len(t, km.managedMutexes, 2)
km.RUnlock("id1")
require.Len(t, km.managedMutexes, 1)
km.RUnlock("id2")
require.Len(t, km.managedMutexes, 0)
}

func TestKeyMutex_IsInterfaceNil(t *testing.T) {
t.Parallel()

csa := NewKeyRWMutex()
require.False(t, check.IfNil(csa))
km := NewKeyRWMutex()
require.False(t, check.IfNil(km))

csa = nil
require.True(t, check.IfNil(csa))
km = nil
require.True(t, check.IfNil(km))

var csa2 KeyRWMutexHandler
require.True(t, check.IfNil(csa2))
var km2 KeyRWMutexHandler
require.True(t, check.IfNil(km2))
}

func TestKeyMutex_ConcurrencyMultipleCriticalSections(t *testing.T) {
t.Parallel()

wg := sync.WaitGroup{}
csa := NewKeyRWMutex()
require.NotNil(t, csa)
km := NewKeyRWMutex()
require.NotNil(t, km)

f := func(wg *sync.WaitGroup, id string) {
csa.Lock(id)
km.Lock(id)
<-time.After(time.Millisecond * 10)
csa.Unlock(id)
km.Unlock(id)

csa.RLock(id)
km.RLock(id)
<-time.After(time.Millisecond * 10)
csa.RUnlock(id)
km.RUnlock(id)

wg.Done()
}
Expand All @@ -91,19 +91,19 @@ func TestKeyMutex_ConcurrencyMultipleCriticalSections(t *testing.T) {
}
wg.Wait()

require.Len(t, csa.managedMutexes, 0)
require.Len(t, km.managedMutexes, 0)
}

func TestKeyMutex_ConcurrencySameID(t *testing.T) {
t.Parallel()

wg := sync.WaitGroup{}
csa := NewKeyRWMutex()
require.NotNil(t, csa)
km := NewKeyRWMutex()
require.NotNil(t, km)

f := func(wg *sync.WaitGroup, id string) {
csa.RLock(id)
csa.RUnlock(id)
km.RLock(id)
km.RUnlock(id)

wg.Done()
}
Expand All @@ -116,5 +116,5 @@ func TestKeyMutex_ConcurrencySameID(t *testing.T) {
}
wg.Wait()

require.Len(t, csa.managedMutexes, 0)
require.Len(t, km.managedMutexes, 0)
}
34 changes: 17 additions & 17 deletions core/sync/rwmutex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ import (
func TestNewRWMutex(t *testing.T) {
t.Parallel()

cs := newRWMutex()
require.NotNil(t, cs)
require.Equal(t, int32(0), cs.cntLocks)
require.Equal(t, int32(0), cs.cntRLocks)
rwm := newRWMutex()
require.NotNil(t, rwm)
require.Equal(t, int32(0), rwm.cntLocks)
require.Equal(t, int32(0), rwm.cntRLocks)
}

func TestRWMutex_Lock_Unlock_IsLocked_NumLocks(t *testing.T) {
t.Parallel()

cs := &rwMutex{}
cs.lock()
cs.updateCounterLock()
require.Equal(t, int32(1), cs.numLocks())
rwm := &rwMutex{}
rwm.lock()
rwm.updateCounterLock()
require.Equal(t, int32(1), rwm.numLocks())

cs.unlock()
cs.updateCounterUnlock()
require.Equal(t, int32(0), cs.numLocks())
rwm.unlock()
rwm.updateCounterUnlock()
require.Equal(t, int32(0), rwm.numLocks())

cs.rLock()
cs.updateCounterRLock()
require.Equal(t, int32(1), cs.numLocks())
rwm.rLock()
rwm.updateCounterRLock()
require.Equal(t, int32(1), rwm.numLocks())

cs.rUnlock()
cs.updateCounterRUnlock()
require.Equal(t, int32(0), cs.numLocks())
rwm.rUnlock()
rwm.updateCounterRUnlock()
require.Equal(t, int32(0), rwm.numLocks())
}

0 comments on commit ecfdff8

Please sign in to comment.