xsync

package module
v0.0.0-...-4e8049b Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2025 License: MIT Imports: 8 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 Go

func Go[T any](f func() T) *Future[T]

Go runs f concurrently, yielding its value via the returned Future.

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 Map

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func (*Map[K, V]) Clear

func (m *Map[K, V]) Clear()

func (*Map[K, V]) CompareAndDelete

func (m *Map[K, V]) CompareAndDelete(key K, old V) (deleted bool)

func (*Map[K, V]) CompareAndSwap

func (m *Map[K, V]) CompareAndSwap(key K, old, new V) (swapped bool)

func (*Map[K, V]) Delete

func (m *Map[K, V]) Delete(key K)

func (*Map[K, V]) Load

func (m *Map[K, V]) Load(key K) (value V, ok bool)

func (*Map[K, V]) LoadAndDelete

func (m *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool)

func (*Map[K, V]) LoadOrStore

func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)

func (*Map[K, V]) Range

func (m *Map[K, V]) Range(f func(key K, value V) bool)

func (*Map[K, V]) Store

func (m *Map[K, V]) Store(key K, value V)

func (*Map[K, V]) Swap

func (m *Map[K, V]) Swap(key K, value V) (previous V, loaded bool)

type Pub

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

Pub is a publisher in a PubSub system. It tracks subscriptions and can broadcast values of type T to them.

A zero-value Pub is ready to use.

As a safety measure, the receive channels of all associated Subs will be closed when the Pub is freed. This behavior only exists as a backup and should not be relied on.

func (*Pub[T]) Send

func (p *Pub[T]) Send(ctx context.Context, v T) error

Send publishes v to all of p's subscribers. If the context is canceled before v is sent, the context's cause is returned.

Send does not return until all subscriptions have received.

func (*Pub[T]) Sub

func (p *Pub[T]) Sub() *Sub[T]

Sub returns a new subscription to p. See Sub for more information.

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]) All

func (q *Queue[T]) All() <-chan iter.Seq[T]

All returns an iterator over all values currently in the queue. Receiving from the channel will empty the queue. The channel will block until there is at least one value in the queue.

Like the channel returned by [Pop], it will be closed when the Queue is stopped.

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. The channel can already be closed when this method returns.

func (*Stopper) Stop

func (s *Stopper) Stop()

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

type Sub

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

Sub is a subscription to a Pub. A Sub must not be copied after first use.

func (*Sub[T]) Recv

func (s *Sub[T]) Recv() <-chan T

Recv returns a channel that yields values published by the corresponding Pub.

Note that the returned channel is not closed when the Sub is unsubscribed.

func (*Sub[T]) Stop

func (s *Sub[T]) Stop()

Stop unsubscribes from the publisher. It is safe to call multiple times.

Directories

Path Synopsis
internal
Package otp implements Erlang-inspired concurrency patterns.
Package otp implements Erlang-inspired concurrency patterns.

Jump to

Keyboard shortcuts

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