Documentation
¶
Overview ¶
Package msync defines some helpful types for managing concurrency.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("collector is closed")
ErrClosed is the sentinel error reported by a collector that is closed before a value could be delivered.
Functions ¶
This section is empty.
Types ¶
type Collector ¶ added in v0.0.4
type Collector[T any] struct { // contains filtered or unexported fields }
A Collector is a fan-in channel that delivers values from multiple writers to one or more readers.
A collector wraps and behaves like a normal buffered channel, but when c is closed any pending writes are safely terminated and report errors rather than panicking.
func NewCollector ¶ added in v0.0.4
NewCollector creates a new collector with the specified channel buffer capacity. If cap == 0, the collector is unbuffered.
func (*Collector[T]) Close ¶ added in v0.0.4
Close closes the collector, which closes the receiver and causes any pending sends to fail. If c is already closed, Close returns ErrClosed.
type Flag ¶ added in v0.1.0
type Flag[T any] struct { // contains filtered or unexported fields }
A Flag is a level-triggered single-value buffer shared by a producer and a consumer. A producer calls Send to make a value available, and a consumer calls Ready to obtain a channel which delivers the most recently-sent value.
Setting value on the flag does not block: Once a value is set, additional values are discarded until the buffered value is consumed.
The Ready method returns a channel that delivers buffered values to the consumer. Once a value has been consumed, the buffer is empty and the producer can send another.
type Trigger ¶
type Trigger struct {
// contains filtered or unexported fields
}
Trigger is an edge-triggered condition shared by multiple goroutines. It is analogous in effect to the standard condition variable (sync.Cond) but uses a channel for signaling.
To wait on the trigger, call Ready to obtain a channel. This channel will be closed the next time the Signal method is executed. A fresh channel is created after each call to Signal, and is shared among all calls to Ready that occur in the window between signals.
A zero Trigger is ready for use but must not be copied after first use.
type Value ¶ added in v0.0.2
type Value[T any] struct { // contains filtered or unexported fields }
A Value is a mutable container for a single value of type T that can be concurrently accessed by multiple goroutines. A zero Value is ready for use, but must not be copied after its first use.
func (*Value[T]) Get ¶ added in v0.0.2
func (v *Value[T]) Get() T
Get returns the current value stored in v.
func (*Value[T]) Set ¶ added in v0.0.2
func (v *Value[T]) Set(newValue T)
Set updates the value stored in v to newValue. Calling Set also wakes any goroutines that are blocked in the Wait method.
func (*Value[T]) Wait ¶ added in v0.0.2
Wait blocks until v.Set is called, or until ctx ends, and returns the current value in v. The flag indicates whether Set was called (true) or ctx timed out (false).
If v.Set is not called before ctx ends, Wait returns the value v held when Wait was called. Otherwise, Wait returns the value from one of the v.Set calls made during its execution.
If multiple goroutines set v concurrently with a call to Wait, the Wait call will return the value from one of them, but not necessarily the first.