Avatar Ilya Mateyko

go.astrophena.name/base Module

GitHub repository | Commit (4b76a30)

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 version of sync.WaitGroup that limits the number of concurrently working goroutines by using a buffered channel as a semaphore.

func NewLimitedWaitGroup

func NewLimitedWaitGroup(limit int) *LimitedWaitGroup

NewLimitedWaitGroup returns a new LimitedWaitGroup that limits the number of concurrently working goroutines to limit.

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 in the semaphore, allowing another goroutine to start.

func (*LimitedWaitGroup) Wait

func (lwg *LimitedWaitGroup) Wait()

Wait blocks until the counter of the LimitedWaitGroup becomes zero.

type Protected

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

Protected provides synchronized access to a value of type T.

func Protect

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

Protect wraps T into Protected.

func (*Protected[T]) Access

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

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

func (*Protected[T]) RAccess

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

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