-
Notifications
You must be signed in to change notification settings - Fork 2
/
taskqueue.go
48 lines (37 loc) · 1.02 KB
/
taskqueue.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
package coopsched
import "container/heap"
type taskQueue interface {
Len() int
Put(t *task)
Get() *task
}
type taskPriorityQueue struct {
ts []*task
}
func newTaskPriorityQueue() *taskPriorityQueue {
return &taskPriorityQueue{}
}
func (q *taskPriorityQueue) Len() int {
return len(q.ts)
}
func (q *taskPriorityQueue) Put(t *task) {
heap.Push((*taskHeap)(q), t)
}
func (q *taskPriorityQueue) Get() *task {
if len(q.ts) == 0 {
return nil
}
return heap.Pop((*taskHeap)(q)).(*task)
}
// A taskHeap implements heap.Interface and orders the queue based on
// the taskPriorityQueue.prio function.
type taskHeap taskPriorityQueue
func (h *taskHeap) Len() int { return len(h.ts) }
func (h *taskHeap) Less(i, j int) bool { return h.ts[i].priority < h.ts[j].priority }
func (h *taskHeap) Swap(i, j int) { h.ts[i], h.ts[j] = h.ts[j], h.ts[i] }
func (h *taskHeap) Push(t interface{}) { h.ts = append(h.ts, t.(*task)) }
func (h *taskHeap) Pop() interface{} {
t := h.ts[len(h.ts)-1]
h.ts = h.ts[:len(h.ts)-1]
return t
}