spscqueue

package
v0.0.0-...-45583b5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Queue

type Queue[T any] struct {
	// contains filtered or unexported fields
}

Queue is the structure responsible for tracking the state of the bounded single-producer single-consumer queue.

func New

func New[T any](size uint) *Queue[T]

New[T any] returns an empty single-producer single-consumer bounded queue. The queue has capacity for `size` elements of type `T`.

func (*Queue[T]) Advance

func (q *Queue[T]) Advance()

Advance moves the consumer forward. Advance may be called after using the data returned from Front. Advance should be called by the consumer if and only if it follows a successful call to Front.

func (*Queue[T]) Commit

func (q *Queue[T]) Commit()

Commit advances the back of the queue. Commit can be used in conjunction with Reserve to work on the underlying queue data and present them to the consumer. Commit should be called by the producer.

func (*Queue[T]) Fill

func (q *Queue[T]) Fill(f func() T)

func (*Queue[T]) Front

func (q *Queue[T]) Front() (T, bool)

Front is a non-blocking variant of Pop. It returns the oldest element in the queue if the queue is not empty, otherwise the zero-value for the type. A boolean indicator of success or failure is included as a second return value. Contrary to Pop, subsequent calls to Front without a call to Advance will return the same element. Front should be called by the consumer.

func (*Queue[T]) Len

func (q *Queue[T]) Len() uint64

Len returns the number of elements in the queue. Any thread may call Len.

func (*Queue[T]) Offer

func (q *Queue[T]) Offer(el T) bool

Offer adds the passed element to the queue if there is an available slot. Offer returns true if the item was added successfully, otherwise false. Offer should be called by the producer.

func (*Queue[T]) Pop

func (q *Queue[T]) Pop() T

Pop returns the oldest element in the queue and removes it. Pop will block if no element is available. Pop should be called by the consumer.

func (*Queue[T]) Push

func (q *Queue[T]) Push(el T)

Push adds the passed element to the queue. Push will block if the queue is full. Push should be called by the producer.

func (*Queue[T]) Reserve

func (q *Queue[T]) Reserve() (T, bool)

Reserve returns the underlying element which the next Push operation will overwrite, i.e. the next open slot at the back of the queue. Data retrieved through Reserve can be made available to the consumer using Commit. The Reserve-Commit pattern can be used to work on the pre-allocated queue items. Subsequent calls to Reserve without a call to Commit will return the same element. Reserve should be called by the producer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL