-
Notifications
You must be signed in to change notification settings - Fork 3
/
linkedBlockingQueue.go
53 lines (43 loc) · 1.07 KB
/
linkedBlockingQueue.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
package blockingQueues
import (
"container/list"
"sync"
)
type LinkedListStore struct {
store *list.List
capacity uint64
}
func NewLinkedListStore(capacity uint64) *LinkedListStore {
return &LinkedListStore{
store: list.New(),
capacity: capacity,
}
}
func (s *LinkedListStore) Set(value interface{}, pos uint64) {
s.store.PushBack(value)
}
func (s *LinkedListStore) Get(pos uint64) interface{} {
return s.store.Front()
}
func (s *LinkedListStore) Remove(pos uint64) interface{} {
var item = s.store.Remove(s.store.Front())
return item
}
func (s LinkedListStore) Size() uint64 {
return s.capacity
}
// Creates an BlockingQueue backed by an LinkedList with the given (fixed) capacity
// returns an error if the capacity is less than 1
func NewLinkedBlockingQueue(capacity uint64) (*BlockingQueue, error) {
if capacity < 1 {
return nil, ErrorCapacity
}
lock := new(sync.Mutex)
return &BlockingQueue{
lock: lock,
notEmpty: sync.NewCond(lock),
notFull: sync.NewCond(lock),
count: uint64(0),
store: NewLinkedListStore(capacity),
}, nil
}