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 ¶
Mtx wraps a value of any type T with a sync.Mutex for safe concurrent access.
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.
type RWMtx ¶
RWMtx is a generic thread-safe wrapper for a value of type T using a RWMutex.
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 ¶
RWithE executes a read-only callback with 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]) LoadAndDelete ¶
LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.
type RWMtxSlice ¶
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)