-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmultilock.go
142 lines (118 loc) · 2.74 KB
/
multilock.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
Copyright 2017 Albert Tedja
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 multilock
import (
"runtime"
"sort"
)
var locks = struct {
lock chan byte
list map[string]chan byte
}{
lock: make(chan byte, 1),
list: make(map[string]chan byte),
}
type Lock struct {
keys []string
chans []chan byte
lock chan byte
unlock chan byte
}
func (self *Lock) Lock() {
self.lock <- 1
// get the channels and attempt to acquire them
self.chans = make([]chan byte, 0, len(self.keys))
for i := 0; i < len(self.keys); {
ch := getChan(self.keys[i])
_, ok := <-ch
if ok {
self.chans = append(self.chans, ch)
i++
}
}
self.unlock <- 1
}
// Unlocks this lock. Must be called after Lock.
// Can only be invoked if there is a previous call to Lock.
func (self *Lock) Unlock() {
<-self.unlock
if self.chans != nil {
for _, ch := range self.chans {
ch <- 1
}
self.chans = nil
}
<-self.lock
}
// Temporarily unlocks, gives up the cpu time to other goroutine, and attempts to lock again.
func (self *Lock) Yield() {
self.Unlock()
runtime.Gosched()
self.Lock()
}
// Creates a new multilock for the specified keys
func New(locks ...string) *Lock {
if len(locks) == 0 {
return nil
}
locks = unique(locks)
sort.Strings(locks)
return &Lock{
keys: locks,
lock: make(chan byte, 1),
unlock: make(chan byte, 1),
}
}
// Cleans old unused locks. Returns removed keys.
func Clean() []string {
locks.lock <- 1
defer func() { <-locks.lock }()
toDelete := make([]string, 0, len(locks.list))
for key, ch := range locks.list {
select {
case <-ch:
close(ch)
toDelete = append(toDelete, key)
default:
}
}
for _, del := range toDelete {
delete(locks.list, del)
}
return toDelete
}
// Create and get the channel for the specified key.
func getChan(key string) chan byte {
locks.lock <- 1
defer func() { <-locks.lock }()
if locks.list[key] == nil {
locks.list[key] = make(chan byte, 1)
locks.list[key] <- 1
}
return locks.list[key]
}
// Return a new string with unique elements.
func unique(arr []string) []string {
if arr == nil || len(arr) <= 1 {
return arr
}
found := map[string]bool{}
result := make([]string, 0, len(arr))
for _, v := range arr {
if !found[v] {
found[v] = true
result = append(result, v)
}
}
return result
}