File: queue.go

package info (click to toggle)
golang-golang-x-net 1%3A0.27.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 8,636 kB
  • sloc: asm: 18; makefile: 12; sh: 7
file content (65 lines) | stat: -rw-r--r-- 1,494 bytes parent folder | download | duplicates (4)
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
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build go1.21

package quic

import "context"

// A queue is an unbounded queue of some item (new connections and streams).
type queue[T any] struct {
	// The gate condition is set if the queue is non-empty or closed.
	gate gate
	err  error
	q    []T
}

func newQueue[T any]() queue[T] {
	return queue[T]{gate: newGate()}
}

// close closes the queue, causing pending and future pop operations
// to return immediately with err.
func (q *queue[T]) close(err error) {
	q.gate.lock()
	defer q.unlock()
	if q.err == nil {
		q.err = err
	}
}

// put appends an item to the queue.
// It returns true if the item was added, false if the queue is closed.
func (q *queue[T]) put(v T) bool {
	q.gate.lock()
	defer q.unlock()
	if q.err != nil {
		return false
	}
	q.q = append(q.q, v)
	return true
}

// get removes the first item from the queue, blocking until ctx is done, an item is available,
// or the queue is closed.
func (q *queue[T]) get(ctx context.Context, testHooks connTestHooks) (T, error) {
	var zero T
	if err := q.gate.waitAndLock(ctx, testHooks); err != nil {
		return zero, err
	}
	defer q.unlock()
	if q.err != nil {
		return zero, q.err
	}
	v := q.q[0]
	copy(q.q[:], q.q[1:])
	q.q[len(q.q)-1] = zero
	q.q = q.q[:len(q.q)-1]
	return v, nil
}

func (q *queue[T]) unlock() {
	q.gate.unlock(q.err != nil || len(q.q) > 0)
}