-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
queue.go
184 lines (151 loc) · 3.53 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
// Copyright © by Jeff Foley 2017-2024. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// SPDX-License-Identifier: Apache-2.0
package queue
import (
"sync"
)
type QueuePriority int
// The priority levels for the priority Queue.
const (
PriorityLow QueuePriority = 0
PriorityNormal QueuePriority = 1
PriorityHigh QueuePriority = 2
PriorityCritical QueuePriority = 3
)
// Queue implements a FIFO data structure that can support a few priorities.
type Queue interface {
// Append adds the data to the Queue at priority level PriorityNormal.
Append(data interface{})
// AppendPriority adds the data to the Queue with respect to priority.
AppendPriority(data interface{}, priority QueuePriority)
// Signal returns the Queue signal channel.
Signal() <-chan struct{}
// Next returns the data at the front of the Queue.
Next() (interface{}, bool)
// Process will execute the callback parameter for each element on the Queue.
Process(callback func(interface{}))
// Empty returns true if the Queue is empty.
Empty() bool
// Len returns the current length of the Queue.
Len() int
}
type queue struct {
sync.Mutex
signal chan struct{}
low []interface{}
norm []interface{}
high []interface{}
crit []interface{}
}
// NewQueue returns an initialized Queue.
func NewQueue() Queue {
return &queue{signal: make(chan struct{}, 1)}
}
// Append implements the Queue interface.
func (q *queue) Append(data interface{}) {
q.append(data, PriorityNormal)
}
// AppendPriority implements the Queue interface.
func (q *queue) AppendPriority(data interface{}, priority QueuePriority) {
q.append(data, priority)
}
func (q *queue) append(data interface{}, priority QueuePriority) {
q.Lock()
defer q.Unlock()
switch priority {
case PriorityLow:
q.low = append(q.low, data)
case PriorityNormal:
q.norm = append(q.norm, data)
case PriorityHigh:
q.high = append(q.high, data)
case PriorityCritical:
q.crit = append(q.crit, data)
}
select {
case q.signal <- struct{}{}:
default:
}
}
// Signal implements the Queue interface.
func (q *queue) Signal() <-chan struct{} {
q.Lock()
defer q.Unlock()
q.prepSignal()
return q.signal
}
func (q *queue) prepSignal() {
var send bool
select {
case _, send = <-q.signal:
default:
}
if !send && q.lenWithoutLock() > 0 {
send = true
}
if send {
select {
case q.signal <- struct{}{}:
default:
}
}
}
func (q *queue) drain() {
for {
select {
case <-q.signal:
default:
return
}
}
}
// Next implements the Queue interface.
func (q *queue) Next() (interface{}, bool) {
q.Lock()
defer q.Unlock()
var data interface{}
if len(q.crit) > 0 {
data = q.crit[0]
q.crit = q.crit[1:]
} else if len(q.high) > 0 {
data = q.high[0]
q.high = q.high[1:]
} else if len(q.norm) > 0 {
data = q.norm[0]
q.norm = q.norm[1:]
} else if len(q.low) > 0 {
data = q.low[0]
q.low = q.low[1:]
} else {
q.drain()
return nil, false
}
q.prepSignal()
return data, true
}
// Process implements the Queue interface.
func (q *queue) Process(callback func(interface{})) {
element, ok := q.Next()
for ok {
callback(element)
element, ok = q.Next()
}
}
// Empty implements the Queue interface.
func (q *queue) Empty() bool {
return q.Len() == 0
}
// Len implements the Queue interface.
func (q *queue) Len() int {
q.Lock()
defer q.Unlock()
return q.lenWithoutLock()
}
func (q *queue) lenWithoutLock() int {
qlen := len(q.low)
qlen += len(q.norm)
qlen += len(q.high)
qlen += len(q.crit)
return qlen
}