Documentation
¶
Index ¶
- func LockContext(lock Lockable, ctx context.Context) bool
- func LockWithTimeout(lock Lockable, ctx context.Context, timeout time.Duration) bool
- func RLockContext(lock *sync.RWMutex, ctx context.Context) bool
- func RLockWithTimeout(lock *sync.RWMutex, ctx context.Context, timeout time.Duration) bool
- type LockMap
- type Lockable
- type RWLockMap
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LockContext ¶
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 ¶
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 ¶
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 ¶
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.
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 ¶
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.