Documentation ¶
Overview ¶
Package syncs contains additional sync types and functionality.
Index ¶
- func AssertLocked(m *sync.Mutex)
- func AssertRLocked(rw *sync.RWMutex)
- func AssertWLocked(rw *sync.RWMutex)
- func ClosedChan() <-chan struct{}
- func Watch(ctx context.Context, mu sync.Locker, tick, max time.Duration) chan time.Duration
- type AtomicValue
- type Map
- func (m *Map[K, V]) Delete(key K)
- func (m *Map[K, V]) Load(key K) (value V, ok bool)
- func (m *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool)
- func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)
- func (m *Map[K, V]) Range(f func(key K, value V) bool)
- func (m *Map[K, V]) Store(key K, value V)
- type Semaphore
- type WaitGroupChan
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertRLocked ¶
AssertRLocked panics if rw is not locked for reading or writing.
func AssertWLocked ¶
AssertWLocked panics if rw is not locked for writing.
func ClosedChan ¶
func ClosedChan() <-chan struct{}
ClosedChan returns a channel that's already closed.
func Watch ¶
Watch monitors mu for contention. On first call, and at every tick, Watch locks and unlocks mu. (Tick should be large to avoid adding contention to mu.) Max is the maximum length of time Watch will wait to acquire the lock. The time required to lock mu is sent on the returned channel. Watch exits when ctx is done, and closes the returned channel.
Types ¶
type AtomicValue ¶
type AtomicValue[T any] struct { // contains filtered or unexported fields }
AtomicValue is the generic version of atomic.Value.
func (*AtomicValue[T]) CompareAndSwap ¶
func (v *AtomicValue[T]) CompareAndSwap(oldV, newV T) (swapped bool)
CompareAndSwap executes the compare-and-swap operation for the Value.
func (*AtomicValue[T]) Load ¶
func (v *AtomicValue[T]) Load() T
Load returns the value set by the most recent Store. It returns the zero value for T if the value is empty.
func (*AtomicValue[T]) LoadOk ¶
func (v *AtomicValue[T]) LoadOk() (_ T, ok bool)
LoadOk is like Load but returns a boolean indicating whether the value was loaded.
func (*AtomicValue[T]) Store ¶
func (v *AtomicValue[T]) Store(x T)
Store sets the value of the Value to x.
func (*AtomicValue[T]) Swap ¶
func (v *AtomicValue[T]) Swap(x T) (old T)
Swap stores new into Value and returns the previous value. It returns the zero value for T if the value is empty.
type Map ¶
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
Map is a Go map protected by a sync.RWMutex. It is preferred over sync.Map for maps with entries that change at a relatively high frequency. This must not be shallow copied.
func (*Map[K, V]) LoadAndDelete ¶
func (*Map[K, V]) LoadOrStore ¶
type Semaphore ¶
type Semaphore struct {
// contains filtered or unexported fields
}
Semaphore is a counting semaphore.
Use NewSemaphore to create one.
func NewSemaphore ¶
NewSemaphore returns a semaphore with resource count n.
func (Semaphore) Acquire ¶
func (s Semaphore) Acquire()
Acquire blocks until a resource is acquired.
func (Semaphore) AcquireContext ¶
AcquireContext reports whether the resource was acquired before the ctx was done.
func (Semaphore) TryAcquire ¶
TryAcquire reports, without blocking, whether the resource was acquired.
type WaitGroupChan ¶
type WaitGroupChan struct {
// contains filtered or unexported fields
}
WaitGroupChan is like a sync.WaitGroup, but has a chan that closes on completion that you can wait on. (This, you can only use the value once) Also, its zero value is not usable. Use the constructor.
func NewWaitGroupChan ¶
func NewWaitGroupChan() *WaitGroupChan
NewWaitGroupChan returns a new single-use WaitGroupChan.
func (*WaitGroupChan) Add ¶
func (wg *WaitGroupChan) Add(delta int)
Add adds delta, which may be negative, to the WaitGroupChan counter. If the counter becomes zero, all goroutines blocked on Wait or the Done chan are released. If the counter goes negative, Add panics.
Note that calls with a positive delta that occur when the counter is zero must happen before a Wait. Calls with a negative delta, or calls with a positive delta that start when the counter is greater than zero, may happen at any time. Typically this means the calls to Add should execute before the statement creating the goroutine or other event to be waited for.
func (*WaitGroupChan) Decr ¶
func (wg *WaitGroupChan) Decr()
Decr decrements the WaitGroup counter by one.
(It is like sync.WaitGroup's Done method, but we don't use Done in this type, because it's ambiguous between Context.Done and WaitGroup.Done. So we use DoneChan and Decr instead.)
func (*WaitGroupChan) DoneChan ¶
func (wg *WaitGroupChan) DoneChan() <-chan struct{}
DoneChan returns a channel that's closed on completion.
func (*WaitGroupChan) Wait ¶
func (wg *WaitGroupChan) Wait()
Wait blocks until the WaitGroupChan counter is zero.