...
1
2
3
4
5
6
7 package quic
8
9 import "context"
10
11
12 type queue[T any] struct {
13
14 gate gate
15 err error
16 q []T
17 }
18
19 func newQueue[T any]() queue[T] {
20 return queue[T]{gate: newGate()}
21 }
22
23
24
25 func (q *queue[T]) close(err error) {
26 q.gate.lock()
27 defer q.unlock()
28 if q.err == nil {
29 q.err = err
30 }
31 }
32
33
34
35 func (q *queue[T]) put(v T) bool {
36 q.gate.lock()
37 defer q.unlock()
38 if q.err != nil {
39 return false
40 }
41 q.q = append(q.q, v)
42 return true
43 }
44
45
46
47 func (q *queue[T]) get(ctx context.Context, testHooks connTestHooks) (T, error) {
48 var zero T
49 if err := q.gate.waitAndLock(ctx, testHooks); err != nil {
50 return zero, err
51 }
52 defer q.unlock()
53 if q.err != nil {
54 return zero, q.err
55 }
56 v := q.q[0]
57 copy(q.q[:], q.q[1:])
58 q.q[len(q.q)-1] = zero
59 q.q = q.q[:len(q.q)-1]
60 return v, nil
61 }
62
63 func (q *queue[T]) unlock() {
64 q.gate.unlock(q.err != nil || len(q.q) > 0)
65 }
66
View as plain text