locks

package
v1.16.7 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RWMap

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

RWMap is a simple wrapper around a map, with global Read-Write protection. For many concurrent reads/writes a sync.Map may be more performant, although it does not utilize Go generics. The RWMap does not have to be initialized, it is immediately ready for reads/writes.

func RWMapFromMap added in v1.12.0

func RWMapFromMap[K comparable, V any](m map[K]V) *RWMap[K, V]

RWMapFromMap creates a RWMap from the given map. This shallow-copies the map, changes to the original map will not affect the new RWMap.

func (*RWMap[K, V]) Clear

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

Clear removes all key-value pairs from the map.

func (*RWMap[K, V]) CreateIfMissing added in v1.12.0

func (m *RWMap[K, V]) CreateIfMissing(key K, fn func() V) (changed bool)

CreateIfMissing creates a value at the given key, if the key is not set yet.

func (*RWMap[K, V]) Delete added in v1.11.0

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

func (*RWMap[K, V]) Get

func (m *RWMap[K, V]) Get(key K) (value V, ok bool)

func (*RWMap[K, V]) Has

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

func (*RWMap[K, V]) Keys added in v1.12.0

func (m *RWMap[K, V]) Keys() (out []K)

Keys returns an unsorted list of keys of the map.

func (*RWMap[K, V]) Len added in v1.11.0

func (m *RWMap[K, V]) Len() int

func (*RWMap[K, V]) Range

func (m *RWMap[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.

func (*RWMap[K, V]) Set

func (m *RWMap[K, V]) Set(key K, value V)

func (*RWMap[K, V]) SetIfMissing added in v1.12.0

func (m *RWMap[K, V]) SetIfMissing(key K, v V) (changed bool)

SetIfMissing is a convenience function to set a missing value if it does not already exist. To lazy-init the value, see CreateIfMissing.

func (*RWMap[K, V]) Values added in v1.12.0

func (m *RWMap[K, V]) Values() (out []V)

Values returns an unsorted list of values of the map.

type RWValue

type RWValue[E any] struct {
	sync.RWMutex
	Value E
}

RWValue is a simple container struct, to deconflict reads/writes of the value, without locking up a bigger structure in the caller. It exposes the underlying RWLock and Value for direct access where needed.

func (*RWValue[E]) Get

func (c *RWValue[E]) Get() (out E)

func (*RWValue[E]) Set

func (c *RWValue[E]) Set(v E)

type Watch added in v1.11.0

type Watch[E any] struct {
	// contains filtered or unexported fields
}

Watch makes a value watch-able: every change will be notified to those watching.

func (*Watch[E]) Catch added in v1.11.0

func (c *Watch[E]) Catch(ctx context.Context, condition func(E) bool) (E, error)

func (*Watch[E]) Get added in v1.11.0

func (c *Watch[E]) Get() (out E)

func (*Watch[E]) Set added in v1.11.0

func (c *Watch[E]) Set(v E)

Set changes the value. This blocks until all watching subscribers have accepted the value.

func (*Watch[E]) Watch added in v1.11.0

func (c *Watch[E]) Watch(dest chan E) (cancel func())

Watch adds a subscriber. Make sure it has channel buffer capacity, since subscribers block.

Jump to

Keyboard shortcuts

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