-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.go
251 lines (211 loc) · 5.01 KB
/
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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package jobq
import (
"container/heap"
"sync"
)
// Queue is an interface for a queue that stores jobs.
type Queue interface {
Enqueue(*Job, interface{})
Dequeue() (*Job, error)
Len() int
Close()
}
// FIFOQueue is a queue that stores jobs in a FIFO order.
type FIFOQueue struct {
jobs chan *Job
mu sync.Mutex
}
// NewFIFOQueue creates a new FIFOQueue with the given size.
func NewFIFOQueue(size int) *FIFOQueue {
return &FIFOQueue{jobs: make(chan *Job, size)}
}
// Enqueue adds a job to the queue.
func (q *FIFOQueue) Enqueue(job *Job, opts interface{}) {
// Ignore opts for FIFOQueue
q.jobs <- job
}
// Dequeue removes a job from the queue.
func (q *FIFOQueue) Dequeue() (*Job, error) {
job, ok := <-q.jobs
if !ok {
return nil, ErrQueueClosed
}
return job, nil
}
// Len returns the number of jobs in the queue.
func (q *FIFOQueue) Len() int {
q.mu.Lock()
defer q.mu.Unlock()
return len(q.jobs)
}
// Close closes the queue.
func (q *FIFOQueue) Close() {
q.mu.Lock()
defer q.mu.Unlock()
close(q.jobs)
}
// LIFOQueue is a queue that stores jobs in a Last In First Out order.
type LIFOQueue struct {
jobs []*Job
mu sync.Mutex
cond *sync.Cond
max int
closed bool
}
// NewLIFOQueue creates a new bounded LIFOQueue with the given size.
func NewLIFOQueue(size int) *LIFOQueue {
q := &LIFOQueue{
jobs: make([]*Job, 0, size),
max: size,
}
q.cond = sync.NewCond(&q.mu)
return q
}
// Enqueue adds a job to the queue.
func (q *LIFOQueue) Enqueue(job *Job, opts interface{}) {
q.mu.Lock()
defer q.mu.Unlock()
for len(q.jobs) == q.max && !q.closed {
q.cond.Wait()
}
if q.closed {
panic("sending on closed queue")
}
q.jobs = append(q.jobs, job)
q.cond.Signal()
}
// Dequeue removes and returns the most recently added job from the queue.
func (q *LIFOQueue) Dequeue() (*Job, error) {
q.mu.Lock()
defer q.mu.Unlock()
for len(q.jobs) == 0 {
if q.closed {
return nil, ErrQueueClosed
}
q.cond.Wait()
}
job := q.jobs[len(q.jobs)-1]
q.jobs = q.jobs[:len(q.jobs)-1]
q.cond.Signal()
return job, nil
}
// Len returns the number of jobs in the queue.
func (q *LIFOQueue) Len() int {
q.mu.Lock()
defer q.mu.Unlock()
return len(q.jobs)
}
// Close closes the queue.
func (q *LIFOQueue) Close() {
q.mu.Lock()
defer q.mu.Unlock()
q.closed = true
q.cond.Broadcast()
}
// PriorityQueueItem is an item in a priority queue.
type PriorityQueueItem struct {
Job *Job
Priority int
Index int
}
// PriorityQueueHeap is a heap implementation of a priority queue.
type PriorityQueueHeap []*PriorityQueueItem
// Len returns the length of the heap.
func (h PriorityQueueHeap) Len() int {
return len(h)
}
// Less returns true if the item at index i has a lower priority than the item at index j.
func (h PriorityQueueHeap) Less(i, j int) bool {
return h[i].Priority < h[j].Priority
}
// Swap swaps the items at index i and j.
func (h PriorityQueueHeap) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
h[i].Index = i
h[j].Index = j
}
// Push pushes an item onto the heap.
func (h *PriorityQueueHeap) Push(x interface{}) {
item := x.(*PriorityQueueItem)
item.Index = len(*h)
*h = append(*h, item)
}
// Pop pops an item from the heap.
func (h *PriorityQueueHeap) Pop() interface{} {
old := *h
n := len(old)
item := old[n-1]
item.Index = -1
*h = old[0 : n-1]
return item
}
// PriorityQueueOptions are options used when enqueueing a job in a PriorityQueue.
type PriorityQueueOptions struct {
Priority int
}
// PriorityQueue is a queue that stores jobs in a priority order.
type PriorityQueue struct {
jobs PriorityQueueHeap
mu sync.Mutex
cond *sync.Cond
closed bool
max int
}
// NewPriorityQueue creates a new PriorityQueue with the given size.
func NewPriorityQueue(size int) *PriorityQueue {
q := &PriorityQueue{
jobs: make(PriorityQueueHeap, 0, size),
max: size,
}
q.cond = sync.NewCond(&q.mu)
heap.Init(&q.jobs)
return q
}
// Enqueue adds a job to the queue.
func (q *PriorityQueue) Enqueue(job *Job, opts interface{}) {
q.mu.Lock()
defer q.mu.Unlock()
// Wait until there is room in the queue
for q.jobs.Len() == q.max && !q.closed {
q.cond.Wait()
}
// Panic if the queue is closed
if q.closed {
panic("sending on closed queue")
}
// Set default priority
priority := 1
// Handle specific options for PriorityQueue
if po, ok := opts.(*PriorityQueueOptions); ok {
priority = po.Priority
}
item := &PriorityQueueItem{Job: job, Priority: priority}
heap.Push(&q.jobs, item)
}
// Dequeue removes a job from the queue.
func (q *PriorityQueue) Dequeue() (*Job, error) {
q.mu.Lock()
defer q.mu.Unlock()
// Wait until there is a job in the queue
for q.jobs.Len() == 0 {
if q.closed {
return nil, ErrQueueClosed
}
q.cond.Wait()
}
item := heap.Pop(&q.jobs).(*PriorityQueueItem)
return item.Job, nil
}
// Len returns the number of jobs in the queue.
func (q *PriorityQueue) Len() int {
q.mu.Lock()
defer q.mu.Unlock()
return q.jobs.Len()
}
// Close closes the queue.
func (q *PriorityQueue) Close() {
q.mu.Lock()
defer q.mu.Unlock()
q.closed = true
q.cond.Broadcast()
}