locks

package
v1.11.2 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitPtrMaybe added in v1.11.0

func InitPtrMaybe[K comparable, V any](m *RWMap[K, *V], key K)

InitPtrMaybe sets a pointer-value in the map, if it's not set yet, to a new object.

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 (*RWMap[K, V]) Clear

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

Clear removes all key-value pairs from the map.

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

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

Default 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]) 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)

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