usync

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2025 License: BSD-3-Clause Imports: 5 Imported by: 2

Documentation

Overview

Package usync extends sync.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Combine

func Combine[T any, C ChannelRecv[T]](ctx context.Context, channels ...C) <-chan T

Combine returns a channel that continuously returns the result of the select until all input sources are exhauste, or the context is canceled.

func Select

func Select[T any, C ChannelRecv[T]](ctx context.Context, channels ...C) (int, T, bool)

A type-safe wrapper around reflect.Select. Taken from: https://stackoverflow.com/questions/19992334

Types

type ChannelRecv

type ChannelRecv[T any] interface {
	~chan T | ~<-chan T
}

ChannelRecv is a type constraint that includes all channel types that can be recieved from.

type Error

type Error string

Error defines errors that this package can produce

const (
	ErrAlreadyClosed Error = "AlreadyClosed"
)

func (Error) Error

func (err Error) Error() string

Error fullfills the error interface.

type Gate

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

Gate wraps a channel and allows the receiver to abruptly stop receiving messages without causing the sender to lock up.

func NewBufferedGate

func NewBufferedGate[T any](buffer int) Gate[T]

NewBufferedGate creates a new gate with a buffer.

func NewGate

func NewGate[T any]() Gate[T]

NewGate creates a new gate with no buffer.

func (*Gate[T]) Cap

func (this *Gate[T]) Cap() int

Cap returns the amount of items the channel can hold.

func (*Gate[T]) Close

func (this *Gate[T]) Close() error

Close closes the gate, unblocking any send or receive operations.

func (*Gate[T]) Len

func (this *Gate[T]) Len() int

Len returns the amount of items in the channel.

func (*Gate[T]) Open

func (this *Gate[T]) Open() bool

Open returns whether the gate is open.

func (*Gate[T]) Receive

func (this *Gate[T]) Receive() <-chan T

Receive returns a receive-only channel that can be used to receive items.

func (*Gate[T]) Reset

func (this *Gate[T]) Reset() error

Reset re-opens the gate if it is closed, and creates a new channel.

func (*Gate[T]) Send

func (this *Gate[T]) Send(item T) (ok bool)

Send sends and item to the channel, returning whether the item was sent.

type Monitor added in v0.9.0

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

Monitor guards access to a value. It must not be copied after first use.

func NewMonitor added in v0.9.0

func NewMonitor[T any](value T) Monitor[T]

NewMonitor creates a new Monitor with the specified value.

func (*Monitor[T]) Borrow added in v0.9.0

func (this *Monitor[T]) Borrow() (T, func())

Borrow borrows the value from the Monitor, and returns a function that must immediately be deferred, like this:

value, done := monitor.Borrow()
defer done()

From the time Borrow is called to the time the done function is called, it is safe to access the locked object from within the current goroutine.

func (*Monitor[T]) BorrowReturn added in v0.9.0

func (this *Monitor[T]) BorrowReturn() (T, func(T))

BorrowReturn is like borrow, but returns a "done" function that takes in an updated value. The intended use of this function is like this:

value, done := monitor.BorrowReturn()
defer done(value)

func (*Monitor[T]) Set added in v0.9.0

func (this *Monitor[T]) Set(value T)

Set sets the value of the Monitor.

type RWMonitor added in v0.9.0

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

RWMonitor guards separate read/write access to a value.

func NewRWMonitor added in v0.9.0

func NewRWMonitor[T any](value T) RWMonitor[T]

NewRWMonitor creates a new Monitor with the specified value. It must not be copied after first use.

func (*RWMonitor[T]) Borrow added in v0.9.0

func (this *RWMonitor[T]) Borrow() (T, func())

Borrow borrows the value from the Monitor for write access, and returns a function that must immediately be deferred, like this:

value, done := monitor.Borrow()
defer done()

From the time Borrow is called to the time the done function is called, it is safe to access the locked object from within the current goroutine.

func (*RWMonitor[T]) BorrowReturn added in v0.9.0

func (this *RWMonitor[T]) BorrowReturn() (T, func(T))

BorrowReturn is like borrow, but returns a "done" function that takes in an updated value. The intended use of this function is like this:

value, done := monitor.BorrowReturn()
defer done(value)

func (*RWMonitor[T]) RBorrow added in v0.9.0

func (this *RWMonitor[T]) RBorrow() (T, func())

RBorrow is like Borrow, but returns the item for read access only. Do not under any circumstances modify anything returned by this method.

func (*RWMonitor[T]) Set added in v0.9.0

func (this *RWMonitor[T]) Set(value T)

Set sets the value of the Monitor.

Jump to

Keyboard shortcuts

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