locks

package
v1.32.1-x.4 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRequestTooLarge = serviceerror.NewInternal("request is larger than the size of semaphore")
)

Functions

This section is empty.

Types

type ConditionVariable

type ConditionVariable interface {
	// Signal wakes one goroutine waiting on this condition variable, if there is any.
	Signal()
	// Broadcast wakes all goroutines waiting on this condition variable.
	Broadcast()
	// Wait atomically unlocks user provided lock and suspends execution of the calling goroutine.
	// After later resuming execution, Wait locks c.L before returning.
	// Wait can be awoken by Broadcast, Signal or user provided interrupt channel.
	Wait(interrupt <-chan struct{})
}

ConditionVariable is the interface for condition variable

type ConditionVariableImpl

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

func NewConditionVariable

func NewConditionVariable(
	lock Locker,
) *ConditionVariableImpl

func (*ConditionVariableImpl) Broadcast

func (c *ConditionVariableImpl) Broadcast()

Broadcast wakes all goroutines waiting on this condition variable.

func (*ConditionVariableImpl) Signal

func (c *ConditionVariableImpl) Signal()

Signal wakes one goroutine waiting on this condition variable, if there is any.

func (*ConditionVariableImpl) Wait

func (c *ConditionVariableImpl) Wait(
	interrupt <-chan struct{},
)

Wait atomically unlocks user provided lock and suspends execution of the calling goroutine. After later resuming execution, Wait locks c.L before returning. Wait can be awoken by Broadcast, Signal or user provided interrupt channel.

type HashFunc

type HashFunc func(any) uint32

HashFunc represents a hash function for string

type IDMutex

type IDMutex interface {
	LockID(identifier any)
	UnlockID(identifier any)
}

IDMutex is an interface which can lock on specific comparable identifier

func NewIDMutex

func NewIDMutex(numShard uint32, hashFn HashFunc) IDMutex

NewIDMutex create a new IDLock

type Locker

type Locker interface {
	// Lock locks
	Lock()
	// Unlock unlocks
	Unlock()
}

Locker is the interface for lock

type Priority

type Priority uint32
const (
	PriorityHigh Priority = iota
	PriorityLow
	NumPriorities
)

type PriorityMutex

type PriorityMutex interface {
	// LockHigh try to lock with high priority, use LockHigh / UnlockHigh pair to lock / unlock
	LockHigh(context.Context) error
	// LockLow try to lock with low priority, use LockLow / UnlockLow pair to lock / unlock
	LockLow(context.Context) error
	// UnlockHigh unlock with high priority, use LockHigh / UnlockHigh pair to lock / unlock
	UnlockHigh()
	// UnlockLow unlock with low priority, use LockLow / UnlockLow pair to lock / unlock
	UnlockLow()
	// TryLockHigh try to lock with high priority, return true if lock acquired
	// use TryLockHigh / UnlockHigh pair to lock / unlock
	TryLockHigh() bool
	// TryLockLow try to lock with high priority, return true if lock acquired
	// use TryLockLow / UnlockLow pair to lock / unlock
	TryLockLow() bool
	// IsLocked returns true if already locked
	IsLocked() bool
}

type PriorityMutexImpl

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

func NewPriorityMutex

func NewPriorityMutex() *PriorityMutexImpl

func (*PriorityMutexImpl) IsLocked

func (c *PriorityMutexImpl) IsLocked() bool

func (*PriorityMutexImpl) LockHigh

func (c *PriorityMutexImpl) LockHigh(
	ctx context.Context,
) error

LockHigh try to lock with high priority, use LockHigh / UnlockHigh pair to lock / unlock

func (*PriorityMutexImpl) LockLow

func (c *PriorityMutexImpl) LockLow(
	ctx context.Context,
) error

LockLow try to lock with low priority, use LockLow / UnlockLow pair to lock / unlock

func (*PriorityMutexImpl) TryLockHigh

func (c *PriorityMutexImpl) TryLockHigh() bool

func (*PriorityMutexImpl) TryLockLow

func (c *PriorityMutexImpl) TryLockLow() bool

func (*PriorityMutexImpl) UnlockHigh

func (c *PriorityMutexImpl) UnlockHigh()

UnlockHigh unlock with high priority, use LockHigh / UnlockHigh pair to lock / unlock

func (*PriorityMutexImpl) UnlockLow

func (c *PriorityMutexImpl) UnlockLow()

UnlockLow unlock with low priority, use LockLow / UnlockLow pair to lock / unlock

type PriorityMutexState

type PriorityMutexState int
const (
	PriorityMutexStateUnlocked     PriorityMutexState = 0
	PriorityMutexStateLockedByHigh PriorityMutexState = 1
	PriorityMutexStateLockedByLow  PriorityMutexState = 2
)

type PrioritySemaphore

type PrioritySemaphore interface {
	Acquire(ctx context.Context, priority Priority, n int) error
	TryAcquire(priority Priority, n int) bool
	Release(n int)
}

type PrioritySemaphoreImpl

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

func NewPrioritySemaphore

func NewPrioritySemaphore(n int) *PrioritySemaphoreImpl

NewPrioritySemaphore creates a new semaphore with the given maximum combined weight for concurrent access, capable of handling multiple priority levels. Most of the logic is taken directly from golang's semaphore.Weighted.

func (*PrioritySemaphoreImpl) Acquire

func (s *PrioritySemaphoreImpl) Acquire(ctx context.Context, priority Priority, n int) error

Acquire acquires the semaphore with a weight of n and a priority, blocking until resources are available or ctx is done. On success, returns nil. On failure, returns ctx.Err() and leaves the semaphore unchanged.

func (*PrioritySemaphoreImpl) Release

func (s *PrioritySemaphoreImpl) Release(n int)

func (*PrioritySemaphoreImpl) TryAcquire

func (s *PrioritySemaphoreImpl) TryAcquire(priority Priority, n int) bool

TryAcquire acquires the semaphore with a weight of n without blocking. On success, returns true. On failure, returns false and leaves the semaphore unchanged.

Jump to

Keyboard shortcuts

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