xsync

package module
v0.0.0-...-a7c0ce3 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2023 License: MIT Imports: 3 Imported by: 5

README

xsync

A collection of extra utilities to complement the Go standard library's sync package.

Documentation

Overview

Package xsync provides extra synchronization primitives to supplement the standard library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Select

func Select(cases ...SelectCase)

Select performs a select operation on the provided cases.

Types

type Future

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

A Future holds a value that might not be available yet.

func NewFuture

func NewFuture[T any]() (f *Future[T], complete func(T))

NewFuture returns a new future and a function that completes that future with the given value. The returned complete function becomes a no-op after the first usage.

func (*Future[T]) Done

func (f *Future[T]) Done() <-chan struct{}

Done returns a channel that is closed when the future completes.

func (*Future[T]) Get

func (f *Future[T]) Get() T

Get blocks, if necessary, until the future is completed and then returns its value.

type Queue

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

A Queue concurrently collects values and returns them in FIFO order. A zero value Queue is ready to use.

A Queue is stopped when it is garbage collected. Therefore, a reference to the Queue must be kept alive during its use or its behavior will become undefined. Because of that, it is recommended to access the Queue's channels via the methods every time instead of storing a copy somewhere.

A Queue is initialized by calling any of its methods, so a copy of a Queue made before those methods are called is a completely independent Queue, while a copy made afterwards is the same Queue.

If a Queue's contents contain any references to the Queue itself, it can cause garbage collection to fail. For example, given a Queue[func()], if the Queue contains any closures which reference the actual instance of the Queue, the Queue's finalizer will not run until those elements have been removed from the Queue. Because of this, such a Queue will need to be manually stopped with a call to Queue.Stop.

func (*Queue[T]) Pop

func (q *Queue[T]) Pop() <-chan T

Pop returns a channel that yields values from the queue when they are available. The channel will be closed when the Queue is stopped.

func (*Queue[T]) Push

func (q *Queue[T]) Push() chan<- T

Push returns a channel that enqueues values sent to it. Closing this channel will cause the channel returned by Pop to be closed once the Queue's contents are emptied, similar to how a regular channel works.

func (*Queue[T]) Stop

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

type SelectCase

type SelectCase interface {
	Dir() reflect.SelectDir
	// contains filtered or unexported methods
}

SelectCase represents either a send or receive on a channel, or a default case with no channel associated.

func Default

func Default(f func()) SelectCase

Default returns a SelectCase that represents a default case. If f is not nil, it will be called if the case is selected.

func Recv

func Recv[T any](c <-chan T, f func(T)) SelectCase

Recv returns a SelectCase representing single-value receive from the channel c. If f is not nil, it will be called with the result of the receive if the receive is selected.

func RecvOK

func RecvOK[T any](c <-chan T, f func(T, bool)) SelectCase

RecvOK returns a SelectCase representing a two-value receive from the channel c. If f is not nil, it will be called with the result of the receive if the receive is selected.

func Send

func Send[T any](c chan<- T, v T, f func()) SelectCase

Send returns a SelectCase representing a send of v to the channel c. If f is not nil, it will be called if the send is selected.

type Stopper

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

A Stopper provides a simple way to handle a done channel for internal coordination. For coordination across API boundaries, it is generally better to use context.Context.

The zero value of a Stopper is ready to use.

func (*Stopper) Done

func (s *Stopper) Done() <-chan struct{}

Done returns a channel that is closed when the Stop method is called.

func (*Stopper) Stop

func (s *Stopper) Stop()

Stop closes the Stoppers Done channel. It is safe to call more than once.

Jump to

Keyboard shortcuts

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