csync

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2024 License: Apache-2.0 Imports: 6 Imported by: 12

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(ctx context.Context, fn func()) error

Run executes fn in a goroutine and waits for it to return. If the context gets canceled before that happens the method returns the context error.

This is useful for executing long-running functions like sync.WaitGroup.Wait that don't take a context and can potentially block the execution forever.

func RunTimeout

func RunTimeout(ctx context.Context, fn func(), timeout time.Duration) error

RunTimeout executes fn in a goroutine and waits for it to return. If the context gets canceled before that happens or the timeout is reached the method returns the context error.

Types

type LockedValueWatcher

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

func (*LockedValueWatcher[T]) Get

func (lvw *LockedValueWatcher[T]) Get() T

Get returns the current value stored in ValueWatcher.

func (*LockedValueWatcher[T]) Set

func (lvw *LockedValueWatcher[T]) Set(val T)

Set stores val in ValueWatcher and notifies all goroutines that called Watch about the new value, if such goroutines exists.

func (*LockedValueWatcher[T]) Unlock

func (lvw *LockedValueWatcher[T]) Unlock() *ValueWatcher[T]

Unlock unlocks the ValueWatcher. After this the LockedValueWatcher should be discarded.

type Map added in v0.4.0

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

Map is a thread-safe map.

func NewMap added in v0.4.0

func NewMap[K comparable, T any]() *Map[K, T]

NewMap creates a new map.

func (*Map[K, T]) All added in v0.4.0

func (m *Map[K, T]) All() iter.Seq2[K, T]

All returns an iterator over the map's key-value pairs. This can be used to iterate over the map using a for-range loop.

Example:

for key, value := range m.All() {
    fmt.Println(key, value)
}

func (*Map[K, T]) Clear added in v0.4.0

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

Clear removes all items from the map.

func (*Map[K, T]) Copy added in v0.4.0

func (m *Map[K, T]) Copy() *Map[K, T]

Copy returns a new map with the same key-value pairs.

func (*Map[K, T]) Delete added in v0.4.0

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

Delete removes the value for a key.

func (*Map[K, T]) Get added in v0.4.0

func (m *Map[K, T]) Get(key K) (T, bool)

Get retrieves the value for a key.

func (*Map[K, T]) Keys added in v0.4.0

func (m *Map[K, T]) Keys() []K

Keys returns all the keys in the map.

func (*Map[K, T]) Len added in v0.4.0

func (m *Map[K, T]) Len() int

Len returns the number of items in the map.

func (*Map[K, T]) Merge added in v0.4.0

func (m *Map[K, T]) Merge(other *Map[K, T])

Merge adds all key-value pairs from another map to this map.

func (*Map[K, T]) Set added in v0.4.0

func (m *Map[K, T]) Set(key K, value T)

Set sets the value for a key.

func (*Map[K, T]) ToGoMap added in v0.4.0

func (m *Map[K, T]) ToGoMap() map[K]T

ToGoMap returns a copy of the map as a Go map.

func (*Map[K, T]) Values added in v0.4.0

func (m *Map[K, T]) Values() []T

Values returns all the values in the map.

type ValueWatcher

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

ValueWatcher holds a reference to a value. Multiple goroutines are able to put or retrieve the value into/from the ValueWatcher, as well as wait for a certain value to be put into the ValueWatcher. It is similar to atomic.Value except the caller can call Watch to be notified each time the value in ValueWatcher changes.

func (*ValueWatcher[T]) CompareAndSwap

func (vw *ValueWatcher[T]) CompareAndSwap(val T, shouldSwap func(T) bool) bool

CompareAndSwap allows the caller to peek at the currently stored value in ValueWatcher and decide if it should be swapped for a new val, all while holding the lock. If the value was swapped, it notifies all goroutines that called Watch about the new value and returns true. Otherwise it returns false.

func (*ValueWatcher[T]) Get

func (vw *ValueWatcher[T]) Get() T

Get returns the current value stored in ValueWatcher.

func (*ValueWatcher[T]) Lock

func (vw *ValueWatcher[T]) Lock() *LockedValueWatcher[T]

Lock locks the ValueWatcher and returns an instance of LockedValueWatcher that allows one to set and get the value while holding the lock. After the caller executed the operations and doesn't need the lock anymore it should call Unlock on the LockedValueWatcher before discarding it.

func (*ValueWatcher[T]) Set

func (vw *ValueWatcher[T]) Set(val T)

Set stores val in ValueWatcher and notifies all goroutines that called Watch about the new value, if such goroutines exists.

func (*ValueWatcher[T]) Watch

func (vw *ValueWatcher[T]) Watch(ctx context.Context, f ValueWatcherCompareFunc[T]) (T, error)

Watch blocks and calls f for every value that is put into the ValueWatcher. Once f returns true it stops blocking and returns the value. First call to f will be with the current value stored in ValueWatcher. Note that if no value was stored in ValueWatcher yet, the zero value of type T will be passed to f. If the context gets canceled before the value is found, the function returns the last seen value and the context error.

Watch can be safely called by multiple goroutines. If the context gets cancelled before f returns true, the function will return the context error.

type ValueWatcherCompareFunc

type ValueWatcherCompareFunc[T any] func(val T) bool

func WatchValues

func WatchValues[T comparable](want ...T) ValueWatcherCompareFunc[T]

WatchValues is a utility function for creating a simple ValueWatcherCompareFunc that waits for one of the supplied values.

type WaitGroup

type WaitGroup sync.WaitGroup

WaitGroup is a sync.WaitGroup with utility methods.

func (*WaitGroup) Add

func (wg *WaitGroup) Add(delta int)

Add adds delta, which may be negative, to the WaitGroup counter. If the counter becomes zero, all goroutines blocked on Wait are released. If the counter goes negative, Add panics.

Note that calls with a positive delta that occur when the counter is zero must happen before a Wait. Calls with a negative delta, or calls with a positive delta that start when the counter is greater than zero, may happen at any time. Typically this means the calls to Add should execute before the statement creating the goroutine or other event to be waited for. If a WaitGroup is reused to wait for several independent sets of events, new Add calls must happen after all previous Wait calls have returned. See the WaitGroup example.

func (*WaitGroup) Done

func (wg *WaitGroup) Done()

Done decrements the WaitGroup counter by one.

func (*WaitGroup) Wait

func (wg *WaitGroup) Wait(ctx context.Context) error

Wait blocks until the WaitGroup counter is zero. If the context gets canceled before that happens the method returns an error.

func (*WaitGroup) WaitTimeout

func (wg *WaitGroup) WaitTimeout(ctx context.Context, timeout time.Duration) error

WaitTimeout blocks until the WaitGroup counter is zero. If the context gets canceled or the timeout is reached before that happens the method returns an error.

Jump to

Keyboard shortcuts

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