Back to godoc.org

Package syncqueue

v0.0.9
Latest Go to latest
Published: Jun 24, 2020 | License: Apache-2.0 | Module: github.com/grailbio/base

Index

Examples

Package Files

type LIFO

type LIFO struct {
	// contains filtered or unexported fields
}

LIFO implements a last-in, first-out producer-consumer queue. Thread safe.

Example

Code:

q := syncqueue.NewLIFO()
q.Put("item0")
q.Put("item1")
q.Close()
v0, ok := q.Get()
fmt.Println("Item 0:", v0.(string), ok)
v1, ok := q.Get()
fmt.Println("Item 1:", v1.(string), ok)
v2, ok := q.Get()
fmt.Println("Item 2:", v2, ok)
Item 0: item1 true
Item 1: item0 true
Item 2: <nil> false

func NewLIFO

func NewLIFO() *LIFO

NewFIFO creates an empty FIFO queue.

func (*LIFO) Close

func (q *LIFO) Close()

Close informs the queue that no more objects will be added via Put().

func (*LIFO) Get

func (q *LIFO) Get() (interface{}, bool)

Get removes the newest object added to the queue. It blocks the caller if the queue is empty.

func (*LIFO) Put

func (q *LIFO) Put(v interface{})

Put adds the object in the queue.

type OrderedQueue

type OrderedQueue struct {
	// contains filtered or unexported fields
}

OrderedQueue is a queue that orders entries on their way out. An inserter enqueues each entry with an index, and when the receiver dequeues an entry, the entries arrive in the sequential order of their indices. An OrderedQueue has a maximum size; if the queue contains the next entry, the queue will accept up to maxSize entries. If the queue does not yet contain the next entry, the queue will block an insert that would make the size equal to maxSize unless the new entry is the next entry to be dequeued.

func NewOrderedQueue

func NewOrderedQueue(maxSize int) *OrderedQueue

Create a new OrderedQueue with size maxSize.

func (*OrderedQueue) Close

func (q *OrderedQueue) Close(err error) error

Close the ordered queue. In the normal case, err should be nil, and Close tells the OrderedQueue that all calls to Insert have completed. The user may not call Insert() after calling Close(). The user may close the OrderedQueue prematurely with a non-nil err while some calls to Insert() and/or Next() are blocking; in this case those calls that are blocking will return with err.

func (*OrderedQueue) Insert

func (q *OrderedQueue) Insert(index int, value interface{}) error

Insert an entry into the queue with the specified sequence index. Insert blocks if the insert would make the queue full and the new entry is not the one next one in the output sequence. The sequence index should start at zero.

func (*OrderedQueue) Next

func (q *OrderedQueue) Next() (value interface{}, ok bool, err error)

Next returns true and the next entry in sequence if the next entry is available. Next blocks if the next entry is not yet present. Next returns (nil, false) if there are no more entries, and the queue is closed.

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier