-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeymutex.go
64 lines (55 loc) · 1.29 KB
/
keymutex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package types
import (
"fmt"
"sync"
)
// KeyMutex manages a unique mutex for every locked key.
// The mutex for a key exists as long as there are any locks
// waiting to be unlocked.
// This is equivalent to declaring a mutex variable for every key,
// except that the key and the number of mutexes are dynamic.
type KeyMutex[T comparable] struct {
global sync.Mutex
locks map[T]*countedLock
}
type countedLock struct {
sync.Mutex
count int
}
// NewKeyMutex returns a new KeyMutex
func NewKeyMutex[T comparable]() *KeyMutex[T] {
return &KeyMutex[T]{locks: make(map[T]*countedLock)}
}
// Lock the mutex for a given key
func (m *KeyMutex[T]) Lock(key T) {
m.global.Lock()
lock := m.locks[key]
if lock == nil {
lock = new(countedLock)
m.locks[key] = lock
}
lock.count++
m.global.Unlock()
lock.Lock()
}
// Unlock the mutex for a given key.
func (m *KeyMutex[T]) Unlock(key T) {
m.global.Lock()
defer m.global.Unlock()
lock := m.locks[key]
if lock == nil {
panic(fmt.Sprintf("KeyMutex[%T].Unlock(%#v) called for non locked key", key, key))
}
lock.count--
if lock.count == 0 {
delete(m.locks, key)
}
lock.Unlock()
}
// IsLocked tells wether a key is locked.
func (m *KeyMutex[T]) IsLocked(key T) bool {
m.global.Lock()
_, locked := m.locks[key]
m.global.Unlock()
return locked
}