mtx

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: May 1, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package mtx provides thread-safe wrappers for values and maps using read-write mutexes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mtx

type Mtx[T any] struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Mtx wraps a value of any type T with a sync.Mutex for safe concurrent access.

func NewMtx

func NewMtx[T any](v T) Mtx[T]

NewMtx creates a new Mtx[T] with the initial value v.

func (*Mtx[T]) Get

func (m *Mtx[T]) Get() T

Get returns a copy of the value, safely locking and unlocking the mutex.

func (*Mtx[T]) Set

func (m *Mtx[T]) Set(v T)

Set sets the internal value to v, safely locking and unlocking the mutex.

func (*Mtx[T]) Swap

func (m *Mtx[T]) Swap(newVal T) (old T)

Swap set a new value and return the old value

func (*Mtx[T]) Val

func (m *Mtx[T]) Val() *T

Val returns a raw pointer to the internal value without locking. This is unsafe for concurrent access and should be used cautiously.

func (*Mtx[T]) With

func (m *Mtx[T]) With(clb func(v *T))

With locks the mutex and applies the given function to the value. It ignores errors.

func (*Mtx[T]) WithE

func (m *Mtx[T]) WithE(clb func(v *T) error) error

WithE locks the mutex and applies the given function to the value, allowing the function to return an error that will be propagated.

type RWMtx

type RWMtx[T any] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

RWMtx is a generic thread-safe wrapper for a value of type T using a RWMutex.

func NewRWMtx

func NewRWMtx[T any](v T) RWMtx[T]

NewRWMtx creates a new RWMtx instance with the given value.

func (*RWMtx[T]) Get

func (m *RWMtx[T]) Get() T

Get ...

func (*RWMtx[T]) RWith

func (m *RWMtx[T]) RWith(clb func(v T))

RWith executes a read-only callback with the protected value (non-error version).

func (*RWMtx[T]) RWithE

func (m *RWMtx[T]) RWithE(clb func(v T) error) error

RWithE executes a read-only callback with the protected value (error-returning version).

func (*RWMtx[T]) Set

func (m *RWMtx[T]) Set(v T)

Set ...

func (*RWMtx[T]) With

func (m *RWMtx[T]) With(clb func(v *T))

With executes a write callback with a pointer to the protected value (non-error version).

func (*RWMtx[T]) WithE

func (m *RWMtx[T]) WithE(clb func(v *T) error) error

WithE executes a write callback with a pointer to the protected value (error-returning version).

type RWMtxMap

type RWMtxMap[K comparable, V any] struct {
	RWMtx[map[K]V]
}

RWMtxMap is a thread-safe map wrapper built on RWMtx.

func NewRWMtxMap

func NewRWMtxMap[K comparable, V any]() RWMtxMap[K, V]

NewRWMtxMap creates a new empty thread-safe map.

func (*RWMtxMap[K, V]) Clear

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

Clear removes all elements from the map.

func (*RWMtxMap[K, V]) Delete

func (m *RWMtxMap[K, V]) Delete(k K)

Delete removes a key-value pair from the map.

func (*RWMtxMap[K, V]) Len

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

Len returns the number of elements in the map.

func (*RWMtxMap[K, V]) Load

func (m *RWMtxMap[K, V]) Load(k K) (out V, ok bool)

Load retrieves a value for a key and indicates existence.

func (*RWMtxMap[K, V]) LoadAndDelete

func (m *RWMtxMap[K, V]) LoadAndDelete(k K) (out 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.

func (*RWMtxMap[K, V]) Store

func (m *RWMtxMap[K, V]) Store(k K, v V)

Store adds or updates a key-value pair in the map.

type RWMtxSlice

type RWMtxSlice[T any] struct {
	RWMtx[[]T]
}

func (*RWMtxSlice[T]) Append

func (s *RWMtxSlice[T]) Append(els ...T)

func (*RWMtxSlice[T]) Clear

func (s *RWMtxSlice[T]) Clear()

Clear clears the slice, removing all values

func (*RWMtxSlice[T]) Clone

func (s *RWMtxSlice[T]) Clone() (out []T)

func (*RWMtxSlice[T]) Each

func (s *RWMtxSlice[T]) Each(clb func(T))

func (*RWMtxSlice[T]) Len

func (s *RWMtxSlice[T]) Len() (out int)

Len returns the length of the slice

func (*RWMtxSlice[T]) Remove

func (s *RWMtxSlice[T]) Remove(i int) (out T)

Remove removes the element at position i within the slice, shifting all elements after it to the left Panics if index is out of bounds

func (*RWMtxSlice[T]) Unshift

func (s *RWMtxSlice[T]) Unshift(el T)

Jump to

Keyboard shortcuts

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