-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
43 lines (37 loc) · 846 Bytes
/
queue.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
package xsum
// nodeQueue has concurrency-safe properties.
// Neither enqueue nor dequeue are individually reentrant.
// However, both may be called at the same time.
type nodeQueue struct {
front, back *nodeElement
}
type nodeElement struct {
node <-chan *Node
next chan *nodeElement
}
func newNodeQueue() *nodeQueue {
elem := &nodeElement{
next: make(chan *nodeElement, 1),
}
return &nodeQueue{elem, elem}
}
func (q *nodeQueue) enqueue(ch <-chan *Node) {
elem := &nodeElement{
node: ch,
next: make(chan *nodeElement, 1),
}
q.front.next <- elem
q.front = elem
}
func (q *nodeQueue) dequeue() *Node {
back := <-q.back.next
if back == nil {
return nil
}
q.back = back
return <-q.back.node
}
// after close, enqueue will always panic, dequeue will always return nil
func (q *nodeQueue) close() {
close(q.front.next)
}