Avatar Ilya Mateyko

go.astrophena.name/base Module

GitHub repository | Commit (49382e9)

Base Go packages for my projects.

package syncx

import "go.astrophena.name/base/syncx"

Package syncx contains useful synchronization primitives.

Index

Types

type Lazy

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

Lazy represents a lazily computed value.

func (*Lazy[T]) Get

func (l *Lazy[T]) Get(f func() T) T

Get returns T, calling f to compute it, if necessary.

func (*Lazy[T]) GetErr

func (l *Lazy[T]) GetErr(f func() (T, error)) (T, error)

GetErr returns T and an error, calling f to compute them, if necessary.

type LimitedWaitGroup

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

LimitedWaitGroup is a sync.WaitGroup that limits the number of concurrently working goroutines.

func NewLimitedWaitGroup

func NewLimitedWaitGroup(limit int) *LimitedWaitGroup

NewLimitedWaitGroup returns a new LimitedWaitGroup.

func (*LimitedWaitGroup) Add

func (lwg *LimitedWaitGroup) Add(delta int)

Add increments the counter of the LimitedWaitGroup by the specified delta. It blocks if the number of active goroutines reaches the concurrency limit.

func (*LimitedWaitGroup) Done

func (lwg *LimitedWaitGroup) Done()

Done decrements the counter of the LimitedWaitGroup by one and releases a slot, allowing another goroutine to start.

func (*LimitedWaitGroup) Go

func (lwg *LimitedWaitGroup) Go(f func())

Go starts a new goroutine that executes f. It blocks if the number of active goroutines reaches the concurrency limit.

func (*LimitedWaitGroup) Wait

func (lwg *LimitedWaitGroup) Wait()

Wait blocks until the counter of the LimitedWaitGroup becomes zero.

type Map

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

Map is a generic version of sync.Map.

func (*Map[K, V]) Delete

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

Delete is sync.Map.Delete.

func (*Map[K, V]) Load

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

Load is sync.Map.Load.

func (*Map[K, V]) LoadAndDelete

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

LoadAndDelete is sync.Map.LoadAndDelete.

func (*Map[K, V]) LoadOrStore

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

LoadOrStore is sync.Map.LoadOrStore.

func (*Map[K, V]) Range

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

Range is sync.Map.Range.

func (*Map[K, V]) Store

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

Store is sync.Map.Store.

type Protected

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

Protected provides synchronized access to a value of type T. It should not be copied.

func Protect

func Protect[T any](val T) Protected[T]

Protect wraps T into Protected.

func (*Protected[T]) ReadAccess

func (p *Protected[T]) ReadAccess(f func(T))

ReadAccess provides read access to the protected value. It executes the provided function f with the value under a read lock.

func (*Protected[T]) WriteAccess

func (p *Protected[T]) WriteAccess(f func(T))

WriteAccess provides write access to the protected value. It executes the provided function f with the value under a write lock.