-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrwlocker.go
More file actions
65 lines (56 loc) · 979 Bytes
/
rwlocker.go
File metadata and controls
65 lines (56 loc) · 979 Bytes
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
package blobfs
import "sync"
type rwLockGroup struct {
group sync.Map
}
func newRWLockGroup() *rwLockGroup {
return &rwLockGroup{
sync.Map{},
}
}
func (g *rwLockGroup) Open(key string) *rwLocker {
actual, _ := g.group.LoadOrStore(key, &rwLocker{
locker: sync.RWMutex{},
switchLocker: sync.Mutex{},
})
locker := actual.(*rwLocker)
return locker
}
func (g *rwLockGroup) Del(key string) {
g.group.Delete(key)
}
type rwLocker struct {
locker sync.RWMutex
switchLocker sync.Mutex
}
func (rw *rwLocker) Lock(read bool) *lockerContent {
rw.switchLocker.Lock()
rw.switchLocker.Unlock()
if read {
rw.locker.RLock()
return &lockerContent{
rw,
true,
func() {
rw.locker.RUnlock()
},
}
} else {
rw.locker.Lock()
return &lockerContent{
rw,
false,
func() {
rw.locker.Unlock()
},
}
}
}
type lockerContent struct {
locker *rwLocker
rLock bool
close func()
}
func (c *lockerContent) Close() {
c.close()
}