-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
stringlock.go
49 lines (43 loc) · 976 Bytes
/
stringlock.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
// +build !plan9
package sftp
import "sync"
// stringLock locks for string IDs passed in
type stringLock struct {
mu sync.Mutex // mutex to protect below
locks map[string]chan struct{} // map of locks
}
// newStringLock creates a stringLock
func newStringLock() *stringLock {
return &stringLock{
locks: make(map[string]chan struct{}),
}
}
// Lock locks on the id passed in
func (l *stringLock) Lock(ID string) {
l.mu.Lock()
for {
ch, ok := l.locks[ID]
if !ok {
break
}
// Wait for the channel to be closed
l.mu.Unlock()
// fs.Logf(nil, "Waiting for stringLock on %q", ID)
<-ch
l.mu.Lock()
}
l.locks[ID] = make(chan struct{})
l.mu.Unlock()
}
// Unlock unlocks on the id passed in. Will panic if Lock with the
// given id wasn't called first.
func (l *stringLock) Unlock(ID string) {
l.mu.Lock()
ch, ok := l.locks[ID]
if !ok {
panic("stringLock: Unlock before Lock")
}
close(ch)
delete(l.locks, ID)
l.mu.Unlock()
}