Source file src/vendor/golang.org/x/net/quic/queue.go

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package quic
     6  
     7  import "context"
     8  
     9  // A queue is an unbounded queue of some item (new connections and streams).
    10  type queue[T any] struct {
    11  	// The gate condition is set if the queue is non-empty or closed.
    12  	gate gate
    13  	err  error
    14  	q    []T
    15  }
    16  
    17  func newQueue[T any]() queue[T] {
    18  	return queue[T]{gate: newGate()}
    19  }
    20  
    21  // close closes the queue, causing pending and future pop operations
    22  // to return immediately with err.
    23  func (q *queue[T]) close(err error) {
    24  	q.gate.lock()
    25  	defer q.unlock()
    26  	if q.err == nil {
    27  		q.err = err
    28  	}
    29  }
    30  
    31  // put appends an item to the queue.
    32  // It returns true if the item was added, false if the queue is closed.
    33  func (q *queue[T]) put(v T) bool {
    34  	q.gate.lock()
    35  	defer q.unlock()
    36  	if q.err != nil {
    37  		return false
    38  	}
    39  	q.q = append(q.q, v)
    40  	return true
    41  }
    42  
    43  // get removes the first item from the queue, blocking until ctx is done, an item is available,
    44  // or the queue is closed.
    45  func (q *queue[T]) get(ctx context.Context) (T, error) {
    46  	var zero T
    47  	if err := q.gate.waitAndLock(ctx); err != nil {
    48  		return zero, err
    49  	}
    50  	defer q.unlock()
    51  	if q.err != nil {
    52  		return zero, q.err
    53  	}
    54  	v := q.q[0]
    55  	copy(q.q[:], q.q[1:])
    56  	q.q[len(q.q)-1] = zero
    57  	q.q = q.q[:len(q.q)-1]
    58  	return v, nil
    59  }
    60  
    61  func (q *queue[T]) unlock() {
    62  	q.gate.unlock(q.err != nil || len(q.q) > 0)
    63  }
    64  

View as plain text