Debian Codegraph @

internal/dissolve/queue.go

raw
1 package dissolve
2
3 import (
4 "sync"
5 )
6
7 // Job to do.
8 type Job func() error
9
10 // queue is an unbounded queue of Job.
11 // The queue is goroutine safe.
12 // Inspired by http://blog.dubbelboer.com/2015/04/25/go-faster-queue.html (MIT)
13 type queue interface {
14 // Add an Job to the back of the queue
15 // will return false if the queue is closed.
16 // In that case the Job is dropped.
17 Add(Job) bool
18
19 // Remove will remove a Job from the queue.
20 // If false is returned, it either means 1) there were no items on the queue
21 // or 2) the queue is closed.
22 Remove() (Job, bool)
23
24 // Close the queue and discard all entries in the queue
25 // all goroutines in wait() will return
26 Close()
27
28 // Closed returns true if the queue has been closed
29 // The call cannot guarantee that the queue hasn't been
30 // closed while the function returns, so only "true" has a definite meaning.
31 Closed() bool
32
33 // Wait for a Job to be added or queue to be closed.
34 // If there is items on the queue the first will
35 // be returned immediately.
36 // Will return "", false if the queue is closed.
37 // Otherwise the return value of "remove" is returned.
38 Wait() (Job, bool)
39 }
40
41 type queueImpl struct {
42 mu sync.RWMutex
43 cond *sync.Cond
44 nodes []Job
45 head int
46 tail int
47 cnt int
48 size int
49 closed bool
50 initCap int
51 }
52
53 var initialCapacity = 2
54
55 // newQueue returns a new Job queue with initial capacity.
56 func newQueue() queue {
57 sq := &queueImpl{
58 initCap: initialCapacity,
59 nodes: make([]Job, initialCapacity),
60 }
61 sq.cond = sync.NewCond(&sq.mu)
62 return sq
63 }
64
65 // Write mutex must be held when calling
66 func (q *queueImpl) resize(n int) {
67 nodes := make([]Job, n)
68 if q.head < q.tail {
69 copy(nodes, q.nodes[q.head:q.tail])
70 } else {
71 copy(nodes, q.nodes[q.head:])
72 copy(nodes[len(q.nodes)-q.head:], q.nodes[:q.tail])
73 }
74
75 q.tail = q.cnt % n
76 q.head = 0
77 q.nodes = nodes
78 }
79
80 // Add a Job to the back of the queue
81 // will return false if the queue is closed.
82 // In that case the Job is dropped.
83 func (q *queueImpl) Add(i Job) bool {
84 q.mu.Lock()
85 if q.closed {
86 q.mu.Unlock()
87 return false
88 }
89 if q.cnt == len(q.nodes) {
90 // Also tested a grow rate of 1.5, see: http://stackoverflow.com/questions/2269063/buffer-growth-strategy
91 // In Go this resulted in a higher memory usage.
92 q.resize(q.cnt * 2)
93 }
94 q.nodes[q.tail] = i
95 q.tail = (q.tail + 1) % len(q.nodes)
96 q.cnt++
97 q.cond.Signal()
98 q.mu.Unlock()
99 return true
100 }
101
102 // Close the queue and discard all entries in the queue
103 // all goroutines in wait() will return
104 func (q *queueImpl) Close() {
105 q.mu.Lock()
106 defer q.mu.Unlock()
107 q.closed = true
108 q.cnt = 0
109 q.nodes = nil
110 q.size = 0
111 q.cond.Broadcast()
112 }
113
114 // Closed returns true if the queue has been closed
115 // The call cannot guarantee that the queue hasn't been
116 // closed while the function returns, so only "true" has a definite meaning.
117 func (q *queueImpl) Closed() bool {
118 q.mu.RLock()
119 c := q.closed
120 q.mu.RUnlock()
121 return c
122 }
123
124 // Wait for a Job to be added.
125 // If there is items on the queue the first will
126 // be returned immediately.
127 // Will return nil, false if the queue is closed.
128 // Otherwise the return value of "remove" is returned.
129 func (q *queueImpl) Wait() (Job, bool) {
130 q.mu.Lock()
131 if q.closed {
132 q.mu.Unlock()
133 return nil, false
134 }
135 if q.cnt != 0 {
136 q.mu.Unlock()
137 return q.Remove()
138 }
139 q.cond.Wait()
140 q.mu.Unlock()
141 return q.Remove()
142 }
143
144 // Remove will remove a Job from the queue.
145 // If false is returned, it either means 1) there were no items on the queue
146 // or 2) the queue is closed.
147 func (q *queueImpl) Remove() (Job, bool) {
148 q.mu.Lock()
149 if q.cnt == 0 {
150 q.mu.Unlock()
151 return nil, false
152 }
153 i := q.nodes[q.head]
154 q.head = (q.head + 1) % len(q.nodes)
155 q.cnt--
156
157 if n := len(q.nodes) / 2; n >= q.initCap && q.cnt <= n {
158 q.resize(n)
159 }
160
161 q.mu.Unlock()
162 return i, true
163 }