-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvault.go
115 lines (95 loc) · 1.83 KB
/
vault.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
package stl
import (
"context"
"sync"
)
// NewVault creates an instance of Vault.
func NewVault() Vault {
return &vault{
wait: make(chan struct{}, 0),
rs: make(map[string]*resource),
}
}
type vault struct {
mu sync.Mutex
wait chan struct{}
rs map[string]*resource
}
// Lock locks the resources according to specified Tx.
func (v *vault) Lock(ctx context.Context, tx Tx) error {
for {
if wait := v.tryLock(tx); wait != nil {
select {
case <-ctx.Done():
return ctx.Err()
case <-wait:
continue
}
}
return nil
}
}
// TryLock tries to lock the resources according to specified Tx.
func (v *vault) TryLock(ctx context.Context, tx Tx) (bool, error) {
return v.tryLock(tx) == nil, nil
}
// Unlock unlocks the resources according to specified Tx.
func (v *vault) Unlock(tx Tx) {
v.mu.Lock()
defer v.mu.Unlock()
for _, rn := range tx.ListExclusive() {
delete(v.rs, rn)
}
for _, rn := range tx.ListShared() {
r := v.rs[rn]
if r.readers == 1 {
delete(v.rs, rn)
} else {
r.readers--
}
}
v.notifyWait()
}
func (v *vault) tryLock(tx Tx) <-chan struct{} {
v.mu.Lock()
defer v.mu.Unlock()
for _, rn := range tx.ListExclusive() {
if _, ok := v.rs[rn]; ok {
return v.exposeWait()
}
}
for _, rn := range tx.ListShared() {
if r, ok := v.rs[rn]; ok {
if r.writer {
return v.exposeWait()
}
}
}
for _, rn := range tx.ListExclusive() {
v.rs[rn] = &resource{0, true}
}
for _, rn := range tx.ListShared() {
if r, ok := v.rs[rn]; ok {
r.readers++
} else {
v.rs[rn] = &resource{1, false}
}
}
return nil
}
func (v *vault) exposeWait() <-chan struct{} {
if v.wait == nil {
v.wait = make(chan struct{}, 0)
}
return v.wait
}
func (v *vault) notifyWait() {
if v.wait != nil {
close(v.wait)
v.wait = nil
}
}
type resource struct {
readers int
writer bool
}