This repository was archived by the owner on May 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmap.go
More file actions
173 lines (155 loc) · 3.47 KB
/
map.go
File metadata and controls
173 lines (155 loc) · 3.47 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package synapse
import (
"sync"
"github.com/tinylib/synapse/sema"
)
const (
// number of significant bits in
// bucket addressing. more bits
// means more memory and better
// worst-case performance.
bucketBits = 6
// number of buckets per map
mbuckets = 1 << bucketBits
// bitmask for bucket index
bucketMask = mbuckets - 1
)
// wMap is really buckets of queues.
// locking is done on the bucket level, so
// concurrent reads and writes generally do
// not contend with one another. the fact that
// waiters are inserted and deleted in approximately
// FIFO order means that we can often expect best-case
// performance for removal.
type wMap [mbuckets]mNode
// returns a sequence number's canonical node
func (w *wMap) node(seq uint64) *mNode { return &w[seq&bucketMask] }
// a node is just a linked list of *waiter with
// a mutex protecting access
type mNode struct {
sync.Mutex
list *waiter // stack of *waiter
tail *waiter // insert point
}
// pop searches the list from top to bottom
// for the sequence number 'seq' and removes
// the *waiter from the list if it exists. returns
// nil otherwise.
func (n *mNode) pop(seq uint64) *waiter {
fwd := &n.list
var prev *waiter
for cur := n.list; cur != nil; cur = cur.next {
if cur.seq == seq {
if cur == n.tail {
n.tail = prev
}
*fwd, cur.next = cur.next, nil
return cur
}
fwd = &cur.next
prev = cur
}
return nil
}
// insert puts a waiter at the tail of the list
func (n *mNode) insert(q *waiter) {
if n.list == nil {
n.list = q
} else {
n.tail.next = q
}
n.tail = q
}
func (n *mNode) size() (i int) {
for w := n.list; w != nil; w = w.next {
i++
}
return
}
// flush waiters marked with 'reap',
// and mark unmarked waiters.
func (n *mNode) reap() {
fwd := &n.list
var prev *waiter
for cur := n.list; cur != nil; {
if cur.reap {
if cur == n.tail {
n.tail = prev
}
*fwd, cur.next = cur.next, nil
cur.err = ErrTimeout
sema.Wake(&cur.done)
cur = *fwd
} else {
cur.reap = true
prev = cur
fwd = &cur.next
cur = cur.next
}
}
}
// flush the entire contents of the node
func (n *mNode) flush(err error) {
var next *waiter
for l := n.list; l != nil; {
next, l.next = l.next, nil
l.err = err
sema.Wake(&l.done)
l = next
}
n.list = nil
n.tail = nil
}
// insert inserts a waiter keyed on its
// sequence number. this has undefined behavior
// if the waiter is already in the map. (!!!)
func (w *wMap) insert(q *waiter) {
n := w.node(q.seq)
n.Lock()
n.insert(q)
n.Unlock()
}
// remove gets a value and removes it if it exists.
// it returns nil if it didn't exist in the first place.
func (w *wMap) remove(seq uint64) *waiter {
n := w.node(seq)
n.Lock()
wt := n.pop(seq)
n.Unlock()
return wt
}
// return the total size of the map... sort of.
// this is only used for testing. during concurrent
// access, the returned value may not be the size
// of the map at *any* fixed point in time. it's
// an approximation.
func (w *wMap) length() (count int) {
for i := range w {
n := &w[i]
n.Lock()
count += n.size()
n.Unlock()
}
return count
}
// unlock every waiter with the provided error,
// and then zero out the entire contents of the map
func (w *wMap) flush(err error) {
for i := range w {
n := &w[i]
n.Lock()
n.flush(err)
n.Unlock()
}
}
// reap carries out a timeout reap.
// if (*waiter).reap==true, then delete it,
// otherwise set (*waiter).reap to true.
func (w *wMap) reap() {
for i := range w {
n := &w[i]
n.Lock()
n.reap()
n.Unlock()
}
}