Skip to content

Commit

Permalink
concurrency/mutexmap: Adds DeleteUnlock (#99)
Browse files Browse the repository at this point in the history
Signed-off-by: joshvanl <[email protected]>
  • Loading branch information
JoshVanL authored Jul 22, 2024
1 parent e2508d6 commit 58c6d9d
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions concurrency/mutexmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
// - Delete(key T): Removes the mutex associated with the given key from the map.
// - Clear(): Removes all mutexes from the map.
// - ItemCount() int: Returns the number of items (mutexes) in the map.
// - DeleteUnlock(key T): Removes the mutex associated with the given key from the map and releases the lock.
type MutexMap[T comparable] interface {
Lock(key T)
Unlock(key T)
Expand All @@ -37,6 +38,7 @@ type MutexMap[T comparable] interface {
Delete(key T)
Clear()
ItemCount() int
DeleteUnlock(key T)
}

type mutexMap[T comparable] struct {
Expand Down Expand Up @@ -106,6 +108,16 @@ func (a *mutexMap[T]) Delete(key T) {
a.lock.Unlock()
}

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()
}
}

func (a *mutexMap[T]) Clear() {
a.lock.Lock()
clear(a.items)
Expand Down

0 comments on commit 58c6d9d

Please sign in to comment.