-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathblockingQueue.go
More file actions
219 lines (176 loc) · 4.45 KB
/
blockingQueue.go
File metadata and controls
219 lines (176 loc) · 4.45 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package blockingQueues
import (
"math"
"sync"
)
/**
* BlockingQueue is A multi-producer, multi-consumer queue
*/
type BlockingQueue struct {
// The number of items in the Queue
count uint64
// Main lock guarding all access
lock *sync.Mutex
// Condition for waiting reads
notEmpty *sync.Cond
// Condition for waiting writes
notFull *sync.Cond
// store index for next write
writeIndex uint64
// store index for next read or remove
readIndex uint64
// The underling store
store QueueStore
}
// Returns the next increment of idx. Circulates the index
func (q *BlockingQueue) inc(idx uint64) uint64 {
if idx >= math.MaxUint64 {
panic("Overflow")
}
if 1+idx == q.store.Size() {
return 0
} else {
return idx + 1
}
}
// Size returns this current elements size, is concurrent safe
func (q *BlockingQueue) Size() uint64 {
q.lock.Lock()
res := q.count
q.lock.Unlock()
return res
}
// Capacity returns this current elements remaining capacity, is concurrent safe
func (q *BlockingQueue) Capacity() uint64 {
q.lock.Lock()
res := uint64(q.store.Size() - q.count)
q.lock.Unlock()
return res
}
// Push element at current write position, advances, and signals.
// Call only when holding lock.
func (q *BlockingQueue) push(item interface{}) {
q.store.Set(item, q.writeIndex)
q.writeIndex = q.inc(q.writeIndex)
q.count += 1
q.notEmpty.Signal()
}
// Pops element at current read position, advances, and signals.
// Call only when holding lock.
func (q *BlockingQueue) pop() (item interface{}) {
item = q.store.Remove(q.readIndex)
q.readIndex = q.inc(q.readIndex)
q.count -= 1
q.notFull.Signal()
return
}
// Pushes the specified element at the tail of the queue.
// Does not block the current goroutine
func (q *BlockingQueue) Push(item interface{}) (bool, error) {
if q.Offer(item) {
return true, nil
} else {
return false, ErrorFull
}
}
// Inserts the specified element at the tail of this queue if it is possible to
// do so immediately without exceeding the queue's capacity,
// returning true upon success and false if this queue is full.
// Does not block the current goroutine
func (q *BlockingQueue) Offer(item interface{}) (res bool) {
if item == nil {
panic("Null item")
}
q.lock.Lock()
res, _ = q.tryPush(item)
q.lock.Unlock()
return
}
func (q *BlockingQueue) tryPush(item interface{}) (res bool, err error) {
if q.count == q.store.Size() {
res, err = false, ErrorFull
} else {
q.push(item)
res, err = true, nil
}
return
}
// Pops an element from the head of the queue.
// Does not block the current goroutine
func (q *BlockingQueue) Pop() (res interface{}, err error) {
q.lock.Lock()
res, err = q.tryPop()
q.lock.Unlock()
return res, err
}
func (q *BlockingQueue) tryPop() (res interface{}, err error) {
if q.count == 0 {
// Case empty
res, err = nil, ErrorEmpty
} else {
var item = q.pop()
res, err = item, nil
}
return
}
// Just attempts to return the tail element of the queue
func (q BlockingQueue) Peek() interface{} {
q.lock.Lock()
var res interface{}
if q.count == 0 {
// Case empty
res = nil
} else {
var item = q.store.Get(q.readIndex)
res = item
}
q.lock.Unlock()
return res
}
func (q BlockingQueue) IsEmpty() bool {
return q.Size() == 0
}
// Clears all the queues elements, cleans up, signals waiters for queue is empty
func (q *BlockingQueue) Clear() {
q.lock.Lock()
// Start from head up to the tail
next := q.readIndex
for i := uint64(0); i < q.count; i += 1 {
q.store.Remove(next)
next = q.inc(next)
}
q.count = uint64(0)
q.readIndex = uint64(0)
q.writeIndex = uint64(0)
q.notFull.Broadcast()
q.lock.Unlock()
}
// Takes an element from the head of the queue.
// It blocks the current goroutine if the queue is Empty until notified
func (q *BlockingQueue) Get() (interface{}, error) {
q.lock.Lock()
for q.count == 0 {
// We wait here until the queue has an item
q.notEmpty.Wait()
}
// Critical section after wait released and predicate is false
var item, err = q.tryPop()
q.lock.Unlock()
return item, err
}
// Puts an element to the tail of the queue.
// It blocks the current goroutine if the queue is Full until notified
func (q *BlockingQueue) Put(item interface{}) (bool, error) {
if item == nil {
panic("Null item")
}
q.lock.Lock()
for q.count == q.store.Size() {
// We wait here until the queue has an empty slot
q.notFull.Wait()
}
// Critical section after wait released and predicate is false
var res, err = q.tryPush(item)
q.lock.Unlock()
return res, err
}