forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
unbounded_blocking_deque.go
169 lines (134 loc) · 2.94 KB
/
unbounded_blocking_deque.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package buffer
import (
"sync"
"github.com/MetalBlockchain/metalgo/utils"
)
var _ BlockingDeque[int] = (*unboundedBlockingDeque[int])(nil)
type BlockingDeque[T any] interface {
Deque[T]
// Close and empty the deque.
Close()
}
// Returns a new unbounded deque with the given initial size.
// Note that the returned deque is always empty -- [initSize] is just
// a hint to prevent unnecessary resizing.
func NewUnboundedBlockingDeque[T any](initSize int) BlockingDeque[T] {
q := &unboundedBlockingDeque[T]{
Deque: NewUnboundedDeque[T](initSize),
}
q.cond = sync.NewCond(&q.lock)
return q
}
type unboundedBlockingDeque[T any] struct {
lock sync.RWMutex
cond *sync.Cond
closed bool
Deque[T]
}
// If the deque is closed returns false.
func (q *unboundedBlockingDeque[T]) PushRight(elt T) bool {
q.cond.L.Lock()
defer q.cond.L.Unlock()
if q.closed {
return false
}
// Add the item to the queue
q.Deque.PushRight(elt)
// Signal a waiting thread
q.cond.Signal()
return true
}
// If the deque is closed returns false.
func (q *unboundedBlockingDeque[T]) PopRight() (T, bool) {
q.cond.L.Lock()
defer q.cond.L.Unlock()
for {
if q.closed {
return utils.Zero[T](), false
}
if q.Deque.Len() != 0 {
return q.Deque.PopRight()
}
q.cond.Wait()
}
}
func (q *unboundedBlockingDeque[T]) PeekRight() (T, bool) {
q.lock.RLock()
defer q.lock.RUnlock()
if q.closed {
return utils.Zero[T](), false
}
return q.Deque.PeekRight()
}
// If the deque is closed returns false.
func (q *unboundedBlockingDeque[T]) PushLeft(elt T) bool {
q.cond.L.Lock()
defer q.cond.L.Unlock()
if q.closed {
return false
}
// Add the item to the queue
q.Deque.PushLeft(elt)
// Signal a waiting thread
q.cond.Signal()
return true
}
// If the deque is closed returns false.
func (q *unboundedBlockingDeque[T]) PopLeft() (T, bool) {
q.cond.L.Lock()
defer q.cond.L.Unlock()
for {
if q.closed {
return utils.Zero[T](), false
}
if q.Deque.Len() != 0 {
return q.Deque.PopLeft()
}
q.cond.Wait()
}
}
func (q *unboundedBlockingDeque[T]) PeekLeft() (T, bool) {
q.lock.RLock()
defer q.lock.RUnlock()
if q.closed {
return utils.Zero[T](), false
}
return q.Deque.PeekLeft()
}
func (q *unboundedBlockingDeque[T]) Index(i int) (T, bool) {
q.lock.RLock()
defer q.lock.RUnlock()
if q.closed {
return utils.Zero[T](), false
}
return q.Deque.Index(i)
}
func (q *unboundedBlockingDeque[T]) Len() int {
q.lock.RLock()
defer q.lock.RUnlock()
if q.closed {
return 0
}
return q.Deque.Len()
}
func (q *unboundedBlockingDeque[T]) List() []T {
q.lock.RLock()
defer q.lock.RUnlock()
if q.closed {
return nil
}
return q.Deque.List()
}
func (q *unboundedBlockingDeque[T]) Close() {
q.cond.L.Lock()
defer q.cond.L.Unlock()
if q.closed {
return
}
q.Deque = nil
// Mark the queue as closed
q.closed = true
q.cond.Broadcast()
}