mutex

package
v0.0.0-...-49d5ce3 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LockContext

func LockContext(lock Lockable, ctx context.Context) bool

LockContext attempts to lock a Lockable (RW/Mutex), but will return false if the provided context is done before the lock is acquired. If the lock is acquired, it returns true. The caller is responsible for unlocking the mutex if true is returned.

func LockWithTimeout

func LockWithTimeout(lock Lockable, ctx context.Context, timeout time.Duration) bool

LockWithTimeout attempts to lock a Lockable (RW/Mutex), but will return false if the timeout is reached before the lock is acquired. If the lock is acquired, it returns true. The caller is responsible for unlocking the mutex if true is returned.

func RLockContext

func RLockContext(lock *sync.RWMutex, ctx context.Context) bool

RLockContext attempts to lock a sync.RWMutex for reading, but will return false if the provided context is done before the lock is acquired. If the lock is acquired, it returns true. The caller is responsible for unlocking the mutex if true is returned.

func RLockWithTimeout

func RLockWithTimeout(lock *sync.RWMutex, ctx context.Context, timeout time.Duration) bool

RLockWithTimeout attempts to lock a sync.RWMutex for reading, but will return false if the timeout is reached before the lock is acquired. If the lock is acquired, it returns true. The caller is responsible for unlocking the mutex if true is returned.

Types

type LockMap

type LockMap[K comparable] struct {
	// contains filtered or unexported fields
}

LockMap is a simple implementation of a map of mutexes. It allows locking and unlocking of mutexes based on a key of type K.

Unlocking a mutex does not automatically remove it from the map. The caller must call Remove to delete the mutex associated with a key. Without this manual garbage collection, the map may grow indefinitely if many unique keys are used.

func (*LockMap[K]) Lock

func (lm *LockMap[K]) Lock(key K)

Lock locks the mutex associated with the given key.

func (*LockMap[K]) Remove

func (lm *LockMap[K]) Remove(key K)

Remove deletes the mutex associated with the given key from the map.

func (*LockMap[K]) TryLock

func (lm *LockMap[K]) TryLock(key K) bool

TryLock attempts to lock the mutex associated with the given key. It returns true if the lock was acquired, false otherwise. This is equivalent to sync.Mutex's TryLock method.

func (*LockMap[K]) Unlock

func (lm *LockMap[K]) Unlock(key K)

Unlock unlocks the mutex associated with the given key. It panics if the mutex is not found in the map (or the mutex is already unlocked).

type Lockable

type Lockable interface {
	Lock()
}

Lockable is a simple interface that only requires a Lock method.

type RWLockMap

type RWLockMap[K comparable] struct {
	// contains filtered or unexported fields
}

RWLockMap is the read-write lock version of LockMap. It allows read-locking, write-locking, and unlocking of mutexes based on a key of type K.

Unlocking a mutex does not automatically remove it from the map. The caller must call Remove to delete the mutex associated with a key. Without this manual garbage collection, the map may grow indefinitely if many unique keys are used.

func (*RWLockMap[K]) Lock

func (rlm *RWLockMap[K]) Lock(key K)

Lock locks the mutex associated with the given key.

func (*RWLockMap[K]) RLock

func (rlm *RWLockMap[K]) RLock(key K)

RLock read-locks the mutex associated with the given key.

func (*RWLockMap[K]) RUnlock

func (rlm *RWLockMap[K]) RUnlock(key K)

RUnlock read-unlocks the mutex associated with the given key. It panics if the mutex is not found in the map (or the mutex is already unlocked).

func (*RWLockMap[K]) Remove

func (rlm *RWLockMap[K]) Remove(key K)

Remove deletes the mutex associated with the given key from the map.

func (*RWLockMap[K]) TryLock

func (rlm *RWLockMap[K]) TryLock(key K) bool

TryLock attempts to write lock the mutex associated with the given key. It returns true if the lock was acquired, false otherwise. This is equivalent to sync.RWMutex's TryLock method.

func (*RWLockMap[K]) TryRLock

func (rlm *RWLockMap[K]) TryRLock(key K) bool

TryRLock attempts to read lock the mutex associated with the given key. It returns true if the lock was acquired, false otherwise. This is equivalent to sync.RWMutex's TryLock method.

func (*RWLockMap[K]) Unlock

func (rlm *RWLockMap[K]) Unlock(key K)

Unlock write-unlocks the mutex associated with the given key. It panics if the mutex is not found in the map (or the mutex is already unlocked).

Jump to

Keyboard shortcuts

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