Documentation
¶
Overview ¶
Package concurrency provides concurrency utilities.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithLock ¶
WithLock executes the given action while holding the provided lock.
It accepts any sync.Locker (e.g., *sync.Mutex, *sync.RWMutex) and a function with no parameters or return values. If the action is nil, nothing is executed.
The lock is guaranteed to be released after the action completes, even if the action panics or returns early.
Types ¶
type Semaphore ¶
type Semaphore struct {
// contains filtered or unexported fields
}
Semaphore is a counting semaphore that bounds the number of concurrent holders.
The zero value (and a nil *Semaphore) is an unlimited semaphore: all acquire operations succeed immediately and Release is a no-op.
All methods are safe for concurrent use by multiple goroutines.
func NewSemaphore ¶
NewSemaphore returns a semaphore with the provided capacity.
If capacity <= 0, it returns an unlimited semaphore, for which all acquire operations succeed immediately and Release does nothing.
func (*Semaphore) Acquire ¶
func (s *Semaphore) Acquire()
Acquire obtains one slot from s, blocking until a slot is available. For an unlimited semaphore, Acquire is a no-op.
func (*Semaphore) AcquireContext ¶
AcquireContext attempts to obtain one slot, blocking until a slot is available or the context is canceled or its deadline is exceeded. It returns ctx.Err() if the context is done first. For an unlimited semaphore, AcquireContext returns nil immediately.
func (*Semaphore) Cap ¶
Cap returns the maximum number of concurrent holders (the capacity). For an unlimited semaphore, Cap returns 0.
func (*Semaphore) InUse ¶
InUse reports the current number of acquired slots. For an unlimited semaphore, InUse returns 0.
func (*Semaphore) Release ¶
func (s *Semaphore) Release()
Release releases one previously acquired slot. On a limited semaphore, calling Release without a matching acquire panics. On an unlimited semaphore, Release is a no-op.
func (*Semaphore) TryAcquire ¶
TryAcquire attempts to obtain one slot without blocking. It returns true if a slot was acquired and false otherwise. For an unlimited semaphore, TryAcquire always returns true.