syncutil

package
v0.35.8 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: Unlicense Imports: 3 Imported by: 9

Documentation

Overview

Package syncutil contains extensions and utilities for package sync from the standard library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChanSemaphore added in v0.18.1

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

ChanSemaphore is a channel-based semaphore.

It must be initialized with NewChanSemaphore.

func NewChanSemaphore added in v0.18.1

func NewChanSemaphore(maxRes uint) (c *ChanSemaphore)

NewChanSemaphore returns a new *ChanSemaphore with the provided maximum resource number.

func (*ChanSemaphore) Acquire added in v0.18.1

func (c *ChanSemaphore) Acquire(ctx context.Context) (err error)

Acquire implements the Semaphore interface for *ChanSemaphore.

func (*ChanSemaphore) Release added in v0.18.1

func (c *ChanSemaphore) Release()

Release implements the Semaphore interface for *ChanSemaphore.

type EmptySemaphore added in v0.18.1

type EmptySemaphore struct{}

EmptySemaphore is a semaphore that has no limit.

func (EmptySemaphore) Acquire added in v0.18.1

func (EmptySemaphore) Acquire(_ context.Context) (err error)

Acquire implements the Semaphore interface for EmptySemaphore. It always returns nil.

func (EmptySemaphore) Release added in v0.18.1

func (EmptySemaphore) Release()

Release implements the Semaphore interface for EmptySemaphore.

type Map added in v0.34.1

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

Map is a generic wrapper around sync.Map for better type safety.

TODO(a.garipov): Remove once https://github.com/golang/go/issues/71076 is implemented.

func NewMap added in v0.34.1

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

NewMap returns a new properly initialized *Map.

func (*Map[K, V]) Clear added in v0.34.1

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

Clear deletes all the entries, resulting in an empty Map.

See sync.Map.Clear.

func (*Map[K, V]) CompareAndDelete added in v0.34.1

func (m *Map[K, V]) CompareAndDelete(key K, old V) (deleted bool)

CompareAndDelete deletes the entry for key if its value is equal to old. The old value must be of a comparable type.

If there is no current value for key in the map, CompareAndDelete returns false (even if the old value is the nil interface value).

See sync.Map.CompareAndDelete.

func (*Map[K, V]) CompareAndSwap added in v0.34.1

func (m *Map[K, V]) CompareAndSwap(key K, old, new V) (swapped bool)

CompareAndSwap swaps the old and new values for key if the value stored in the map is equal to old.

See sync.Map.CompareAndSwap.

func (*Map[K, V]) Delete added in v0.34.1

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

Delete deletes the value for a key.

See sync.Map.Delete.

func (*Map[K, V]) Load added in v0.34.1

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

Load returns the value stored in the map for a key, or its zero value if no value is present.

See sync.Map.Load.

func (*Map[K, V]) LoadAndDelete added in v0.34.1

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

LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.

See sync.Map.LoadAndDelete.

func (*Map[K, V]) LoadOrStore added in v0.34.1

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

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value.

See sync.Map.LoadOrStore.

func (*Map[K, V]) Range added in v0.34.1

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

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

See sync.Map.Range.

func (*Map[K, V]) Store added in v0.34.1

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

Store sets the value for a key.

See sync.Map.Store.

func (*Map[K, V]) Swap added in v0.34.1

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

Swap swaps the value for a key and returns the previous value if any. The loaded result reports whether the key was present.

See sync.Map.Swap.

type OnceConstructor added in v0.27.0

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

OnceConstructor initializes a value for one key only once.

TODO(a.garipov): Add benchmarks.

func NewOnceConstructor added in v0.27.0

func NewOnceConstructor[K comparable, V any](newFunc func(k K) (v V)) (c *OnceConstructor[K, V])

NewOnceConstructor returns a new properly initialized *OnceConstructor that uses newFunc to construct a value for the given key.

func (*OnceConstructor[K, V]) Get added in v0.27.0

func (c *OnceConstructor[K, V]) Get(key K) (v V)

Get returns a value for the given key. If a value isn't available, it waits until it is.

type Pool

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

Pool is the strongly typed version of sync.Pool that manages pointers to T.

func NewPool

func NewPool[T any](newFunc func() (v *T)) (p *Pool[T])

NewPool returns a new strongly typed pool. newFunc must not be nil.

func NewSlicePool

func NewSlicePool[T any](l int) (p *Pool[[]T])

NewSlicePool is a helper for constructing pools with pointers to slices of a type with the given length.

func (*Pool[T]) Get

func (p *Pool[T]) Get() (v *T)

Get selects an arbitrary item from the pool, removes it from the pool, and returns it to the caller.

See sync.Pool.Get.

func (*Pool[T]) Put

func (p *Pool[T]) Put(v *T)

Put adds v to the pool.

See sync.Pool.Put.

type Semaphore added in v0.18.1

type Semaphore interface {
	// Acquire gets the resource, will block until the resource can be acquired.
	// ctx is used for cancellation.
	Acquire(ctx context.Context) (err error)

	// Release the resource, never blocks.
	Release()
}

Semaphore is the semaphore interface.

Jump to

Keyboard shortcuts

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