gensync

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Atomic

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

func NewAtomic

func NewAtomic[T any](val T) Atomic[T]

func (*Atomic[T]) Load

func (a *Atomic[T]) Load() T

func (*Atomic[T]) Store

func (a *Atomic[T]) Store(val T)

func (*Atomic[T]) StoreIf

func (a *Atomic[T]) StoreIf(val T, condition func(old T, new T) bool) (stored bool)

type AtomicComparable

type AtomicComparable[T comparable] Atomic[T]

func NewAtomicComparable

func NewAtomicComparable[T comparable](val T) AtomicComparable[T]

func (*AtomicComparable[T]) CompareAndSwap

func (a *AtomicComparable[T]) CompareAndSwap(old T, new T) (swapped bool)

type AtomicNumeric

type AtomicNumeric[T constraints.Numeric] AtomicComparable[T]

func NewAtomicNumeric

func NewAtomicNumeric[T constraints.Numeric](val T) AtomicNumeric[T]

func (*AtomicNumeric[T]) Add

func (a *AtomicNumeric[T]) Add(delta T) (new T)

func (*AtomicNumeric[T]) Subtract

func (a *AtomicNumeric[T]) Subtract(delta T) (new T)

type HashMap

type HashMap[T comparable] interface {
	Store(key T)
	Delete(key T)
	Has(key T) bool
	Length() int
	Keys() []T
}

func NewHashMap

func NewHashMap[T comparable](initial []T) HashMap[T]

type Map

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

This is a generically typed version of the built-in sync.Map

func NewMap

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

NewMap creates a new map with initial values

func (*Map[K, V]) Delete

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

Delete deletes the value for a key.

func (*Map[K, V]) Has

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

Has checks if the map contains the given key

func (*Map[K, V]) Keys

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

Keys will get all keys in the map. It is subject to the same conditions/restrictions as Range.

func (*Map[K, V]) Length

func (m *Map[K, V]) Length() (length int)

Length will get the number of elements in the map. It is subject to the same conditions/restrictions as Range.

func (*Map[K, V]) Load

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

Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.

func (*Map[K, V]) LoadAndDelete

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

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

func (*Map[K, V]) LoadOrStore

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. The loaded result is true if the value was loaded, false if stored.

func (*Map[K, V]) Range

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

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

Range does not necessarily correspond to any consistent snapshot of the Map's contents: no key will be visited more than once, but if the value for any key is stored or deleted concurrently (including by f), Range may reflect any mapping for that key from any point during the Range call. Range does not block other methods on the receiver; even f itself may call any method on m.

Range may be O(N) with the number of elements in the map even if f returns false after a constant number of calls.

func (*Map[K, V]) Store

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

Store sets the value for a key.

func (*Map[K, V]) ToMap

func (m *Map[K, V]) ToMap() map[K]V

ToMap returns a standard map with all of the values in this sync map.

func (*Map[K, V]) Values

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

Values will get all values in the map. It is subject to the same conditions/restrictions as Range.

type MultiLock

type MultiLock[T comparable] interface {
	LockAll(excluded ...T)
	UnlockAll(excluded ...T)
	Lock(key T)
	Unlock(key T)
}

func NewMultiLock

func NewMultiLock[T comparable](keys []T) MultiLock[T]

type Once

type Once struct {
	sync.Once
}

gensync.Once is a form of sync.Once that captures and returns stack errors

func (*Once) Do

func (o *Once) Do(f func() stackerr.Error) stackerr.Error

Do calls the function f if and only if Do is being called for the first time for this instance of Once. In other words, given

var once Once

if once.Do(f) is called multiple times, only the first call will invoke f, even if f has a different value in each invocation. A new instance of Once is required for each function to execute.

Do is intended for initialization that must be run exactly once. Since f is niladic, it may be necessary to use a function literal to capture the arguments to a function to be invoked by Do:

config.once.Do(func() { config.init(filename) })

Because no call to Do returns until the one call to f returns, if f causes Do to be called, it will deadlock.

If f panics, Do considers it to have returned; future calls of Do return without calling f.

type RingSlice

type RingSlice[T any] interface {
	// Next will get the next value in the slice
	Next() T
	// Set will set new values for the slice
	Set(values []T)
	// Values will get a copy of the current values
	// (not the actual internal slice, just a copy of it)
	Values() []T
}

A RingSlice is a slice that can be accessed by multiple routines to always return the next value in the slice, resetting at the first value once the last value is reached.

func NewRingSlice

func NewRingSlice[T any](values []T) RingSlice[T]

type Slice

type Slice[V any] interface {
	// Load returns a COPY of the slice.
	Load() (slice []V)

	// SubSlice returns a COPY of the subslice in the form s[start:end].
	SubSlice(start int, end int) (subslice []V)

	// StoreIndex will store a value in the slice at the given index.
	StoreIndex(index int, value V)

	// LoadIndex will load the value in the slice at the given index.
	LoadIndex(index int) (value V)

	// Concat will concatenate c to the end of the slice.
	Concat(c []V)

	// Append will append the value a to the end of the slice.
	Append(a V)

	// Length will get the number of elements in the slice
	Length() int
}

func NewSlice

func NewSlice[T any](initial []T) Slice[T]

Jump to

Keyboard shortcuts

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