mutexes

package module
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: MIT Imports: 4 Imported by: 3

README

Library that provides more complex mutex implementations than default libraries

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cond added in v1.4.0

type Cond struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Cond is similar to a sync.Cond{}, but it encompasses the Mutex{} within itself.

func (*Cond) Broadcast added in v1.4.0

func (c *Cond) Broadcast()

See: sync.Cond{}.Broadcast().

func (*Cond) Signal added in v1.4.0

func (c *Cond) Signal()

See: sync.Cond{}.Signal().

func (*Cond) Wait added in v1.4.0

func (c *Cond) Wait()

See: sync.Cond{}.Wait().

type Mutex

type Mutex interface {
	// Lock performs a mutex lock, returning an unlock function
	Lock() (unlock func())
}

Mutex defines a wrappable mutex. By forcing unlocks via returned function it makes wrapping much easier

func New

func New() Mutex

New returns a new base Mutex implementation

func WithFunc

func WithFunc(mu Mutex, onLock, onUnlock func()) Mutex

WithFunc wraps the supplied Mutex to call the provided hooks on lock / unlock

func WithSafety

func WithSafety(mu Mutex) Mutex

WithSafety wrapps the supplied Mutex to protect unlock fns from being called multiple times

type MutexMap

type MutexMap struct {
	// contains filtered or unexported fields
}

MutexMap is a structure that allows read / write locking per key, performing as you'd expect a map[string]*RWMutex to perform, without you needing to worry about deadlocks between competing read / write locks and the map's own mutex. It uses memory pooling for the internal "mutex" (ish) types and performs self-eviction of keys.

Under the hood this is achieved using a single mutex for the map, state tracking for individual keys, and some sync.Cond{} like structures for sleeping / awaking awaiting goroutines.

func (*MutexMap) Lock

func (mm *MutexMap) Lock(key string) func()

Lock acquires a write lock on key in map, returning unlock function.

func (*MutexMap) RLock

func (mm *MutexMap) RLock(key string) func()

RLock acquires a read lock on key in map, returning runlock function.

type RWCond added in v1.4.0

type RWCond struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

RWCond is similar to a sync.Cond{}, but it encompasses the RWMutex{} within itself.

func (*RWCond) Broadcast added in v1.4.0

func (c *RWCond) Broadcast()

See: sync.Cond{}.Broadcast().

func (*RWCond) Signal added in v1.4.0

func (c *RWCond) Signal()

See: sync.Cond{}.Signal().

func (*RWCond) Wait added in v1.4.0

func (c *RWCond) Wait()

See: sync.Cond{}.Wait().

type RWMutex

type RWMutex interface {
	Mutex

	// RLock performs a mutex read lock, returning an unlock function
	RLock() (runlock func())
}

RWMutex defines a wrappable read-write mutex. By forcing unlocks via returned functions it makes wrapping much easier

func NewRW

func NewRW() RWMutex

NewRW returns a new base RWMutex implementation

func WithFuncRW

func WithFuncRW(mu RWMutex, onLock, onRLock, onUnlock, onRUnlock func()) RWMutex

WithFuncRW wrapps the supplied RWMutex to call the provided hooks on lock / rlock / unlock/ runlock

func WithSafetyRW

func WithSafetyRW(mu RWMutex) RWMutex

WithSafetyRW wrapps the supplied RWMutex to protect unlock fns from being called multiple times

type TimeoutMutex

type TimeoutMutex interface {
	Mutex

	// LockFunc is functionally the same as Lock(), but allows setting a custom hook called on timeout
	LockFunc(func()) func()
}

TimeoutMutex defines a Mutex with timeouts on locks

func WithTimeout

func WithTimeout(mu Mutex, d time.Duration) TimeoutMutex

WithTimeout wraps the supplied Mutex to add a timeout

type TimeoutRWMutex

type TimeoutRWMutex interface {
	RWMutex

	// LockFunc is functionally the same as Lock(), but allows setting a custom hook called on timeout
	LockFunc(func()) func()

	// RLockFunc is functionally the same as RLock(), but allows setting a custom hook called on timeout
	RLockFunc(func()) func()
}

TimeoutRWMutex defines a RWMutex with timeouts on locks

func WithTimeoutRW

func WithTimeoutRW(mu RWMutex, rd, wd time.Duration) TimeoutRWMutex

WithTimeoutRW wraps the supplied RWMutex to add read/write timeouts

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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