syncs

package
v1.33.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2022 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package syncs contains additional sync types and functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertLocked

func AssertLocked(m *sync.Mutex)

AssertLocked panics if m is not locked.

func AssertRLocked

func AssertRLocked(rw *sync.RWMutex)

AssertRLocked panics if rw is not locked for reading or writing.

func AssertWLocked

func AssertWLocked(rw *sync.RWMutex)

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

func Watch(ctx context.Context, mu sync.Locker, tick, max time.Duration) chan time.Duration

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 Semaphore

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

Semaphore is a counting semaphore.

Use NewSemaphore to create one.

func NewSemaphore

func NewSemaphore(n int) Semaphore

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

func (s Semaphore) AcquireContext(ctx context.Context) bool

AcquireContext reports whether the resource was acquired before the ctx was done.

func (Semaphore) Release

func (s Semaphore) Release()

Release releases a resource.

func (Semaphore) TryAcquire

func (s Semaphore) TryAcquire() bool

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.

Jump to

Keyboard shortcuts

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