locks

package
v1.26.2-121.4 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2024 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 added in v1.11.0

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 added in v1.11.0

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

func NewConditionVariable added in v1.11.0

func NewConditionVariable(
	lock Locker,
) *ConditionVariableImpl

func (*ConditionVariableImpl) Broadcast added in v1.11.0

func (c *ConditionVariableImpl) Broadcast()

Broadcast wakes all goroutines waiting on this condition variable.

func (*ConditionVariableImpl) Signal added in v1.11.0

func (c *ConditionVariableImpl) Signal()

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

func (*ConditionVariableImpl) Wait added in v1.11.0

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(interface{}) uint32

HashFunc represents a hash function for string

type IDMutex

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

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 added in v1.11.0

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

Locker is the interface for lock

type Priority added in v1.25.0

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

type PriorityMutex added in v1.11.0

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 added in v1.11.0

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

func NewPriorityMutex added in v1.11.0

func NewPriorityMutex() *PriorityMutexImpl

func (*PriorityMutexImpl) IsLocked added in v1.14.0

func (c *PriorityMutexImpl) IsLocked() bool

func (*PriorityMutexImpl) LockHigh added in v1.11.0

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 added in v1.11.0

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 added in v1.14.0

func (c *PriorityMutexImpl) TryLockHigh() bool

func (*PriorityMutexImpl) TryLockLow added in v1.14.0

func (c *PriorityMutexImpl) TryLockLow() bool

func (*PriorityMutexImpl) UnlockHigh added in v1.11.0

func (c *PriorityMutexImpl) UnlockHigh()

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

func (*PriorityMutexImpl) UnlockLow added in v1.11.0

func (c *PriorityMutexImpl) UnlockLow()

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

type PriorityMutexState added in v1.11.0

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

type PrioritySemaphore added in v1.25.0

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

type PrioritySemaphoreImpl added in v1.25.0

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

func NewPrioritySemaphore added in v1.25.0

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 added in v1.25.0

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 added in v1.25.0

func (s *PrioritySemaphoreImpl) Release(n int)

func (*PrioritySemaphoreImpl) TryAcquire added in v1.25.0

func (s *PrioritySemaphoreImpl) TryAcquire(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