-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
keyedmutex.go
75 lines (61 loc) · 1.95 KB
/
keyedmutex.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
65
66
67
68
69
70
71
72
73
74
75
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package remote
import "sync"
// keyedMutex is a mutex locking on the key provided to the Lock function.
// Only one caller can hold the lock for a specific key at a time.
type keyedMutex struct {
locksMtx sync.Mutex
locks map[interface{}]*keyLock
}
// newKeyedMutex creates a new keyed mutex ready for use.
func newKeyedMutex() *keyedMutex {
return &keyedMutex{
locks: make(map[interface{}]*keyLock),
}
}
// keyLock is the lock for a single specific key.
type keyLock struct {
sync.Mutex
// users is the number of callers attempting to acquire the mutex, including the one currently holding it.
users uint
}
// unlock unlocks a currently locked key.
type unlock func()
// Lock locks the passed in key, blocking if the key is locked.
// Returns the unlock function to release the lock on the key.
func (k *keyedMutex) Lock(key interface{}) unlock {
// Get an existing keyLock for the key or create a new one and increase the number of users.
l := func() *keyLock {
k.locksMtx.Lock()
defer k.locksMtx.Unlock()
l, ok := k.locks[key]
if !ok {
l = &keyLock{}
k.locks[key] = l
}
l.users++
return l
}()
l.Lock()
// Unlocks the keyLock for the key, decreases the counter and removes the keyLock from the map if there are no more users left.
return func() {
k.locksMtx.Lock()
defer k.locksMtx.Unlock()
l.Unlock()
l.users--
if l.users == 0 {
delete(k.locks, key)
}
}
}