forked from foize/go.fifo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfifo.go
133 lines (111 loc) · 3.06 KB
/
fifo.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
// Created by Yaz Saito on 06/15/12.
// Modified by Geert-Johan Riemer, Foize B.V.
// Further additions by Miiro Juuso, Artirix Limited
// TODO:
// - travis CI
package fifo
import (
"sync"
)
const chunkSize = 64
// chunks are used to make a queue auto resizeable.
type chunk struct {
items [chunkSize]interface{} // list of queue'ed items
first, last int // positions for the first and list item in this chunk
next *chunk // pointer to the next chunk (if any)
}
// fifo queue
type Queue struct {
head, tail *chunk // chunk head and tail
count int // total amount of items in the queue
lock sync.Mutex // synchronisation lock
}
// NewQueue creates a new and empty *fifo.Queue
func NewQueue() (q *Queue) {
initChunk := new(chunk)
q = &Queue{
head: initChunk,
tail: initChunk,
}
return q
}
// Return the number of items in the queue
func (q *Queue) Len() (length int) {
// locking to make Queue thread-safe
q.lock.Lock()
defer q.lock.Unlock()
// copy q.count and return length
length = q.count
return length
}
// Add an item to the end of the queue
func (q *Queue) Add(item interface{}) {
// locking to make Queue thread-safe
q.lock.Lock()
defer q.lock.Unlock()
// check if item is valid
if item == nil {
panic("can not add nil item to fifo queue")
}
// if the tail chunk is full, create a new one and add it to the queue.
if q.tail.last >= chunkSize {
q.tail.next = new(chunk)
q.tail = q.tail.next
}
// add item to the tail chunk at the last position
q.tail.items[q.tail.last] = item
q.tail.last++
q.count++
}
// Remove the item at the head of the queue and return it.
// Returns nil when there are no items left in queue.
func (q *Queue) Next() (item interface{}) {
// locking to make Queue thread-safe
q.lock.Lock()
defer q.lock.Unlock()
// Return nil if there are no items to return
if q.count == 0 {
return nil
}
// FIXME: why would this check be required?
if q.head.first >= q.head.last {
return nil
}
// Get item from queue
item = q.head.items[q.head.first]
// increment first position and decrement queue item count
q.head.first++
q.count--
if q.head.first >= q.head.last {
// we're at the end of this chunk and we should do some maintainance
// if there are no follow up chunks then reset the current one so it can be used again.
if q.count == 0 {
q.head.first = 0
q.head.last = 0
q.head.next = nil
} else {
// set queue's head chunk to the next chunk
// old head will fall out of scope and be GC-ed
q.head = q.head.next
}
}
// return the retrieved item
return item
}
// Return the item at the head of the queue without removing it
// Returns nil when there are no items left in queue.
func (q *Queue) Peek() (item interface{}) {
// locking to make Queue thread-safe
q.lock.Lock()
defer q.lock.Unlock()
// Return nil if there are no items to return
if q.count == 0 {
return nil
}
// FIXME: why would this check be required?
if q.head.first >= q.head.last {
return nil
}
// return the first item
return q.head.items[q.head.first]
}