syncs

package
v1.46.1 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2023 License: BSD-3-Clause Imports: 6 Imported by: 60

Documentation

Overview

Package syncs contains additional sync types and functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertLocked added in v1.14.0

func AssertLocked(m *sync.Mutex)

AssertLocked panics if m is not locked.

func AssertRLocked added in v1.14.0

func AssertRLocked(rw *sync.RWMutex)

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

func AssertWLocked added in v1.14.0

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

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

type AtomicValue[T any] struct {
	// contains filtered or unexported fields
}

AtomicValue is the generic version of atomic.Value.

func (*AtomicValue[T]) CompareAndSwap added in v1.30.0

func (v *AtomicValue[T]) CompareAndSwap(oldV, newV T) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for the Value.

func (*AtomicValue[T]) Load added in v1.30.0

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

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

func (v *AtomicValue[T]) Store(x T)

Store sets the value of the Value to x.

func (*AtomicValue[T]) Swap added in v1.30.0

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

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]) Delete added in v1.34.0

func (m *Map[K, V]) Delete(key K)

func (*Map[K, V]) Len added in v1.42.0

func (m *Map[K, V]) Len() int

Len returns the length of the map.

func (*Map[K, V]) Load added in v1.34.0

func (m *Map[K, V]) Load(key K) (value V, ok bool)

func (*Map[K, V]) LoadAndDelete added in v1.34.0

func (m *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool)

func (*Map[K, V]) LoadOrStore added in v1.34.0

func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)

func (*Map[K, V]) Range added in v1.34.0

func (m *Map[K, V]) Range(f func(key K, value V) bool)

Range iterates over the map in undefined order calling f for each entry. Iteration stops if f returns false. Map changes are blocked during iteration.

func (*Map[K, V]) Store added in v1.34.0

func (m *Map[K, V]) Store(key K, value V)

type Semaphore added in v1.8.0

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

Semaphore is a counting semaphore.

Use NewSemaphore to create one.

func NewSemaphore added in v1.8.0

func NewSemaphore(n int) Semaphore

NewSemaphore returns a semaphore with resource count n.

func (Semaphore) Acquire added in v1.8.0

func (s Semaphore) Acquire()

Acquire blocks until a resource is acquired.

func (Semaphore) AcquireContext added in v1.8.0

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

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

func (Semaphore) Release added in v1.8.0

func (s Semaphore) Release()

Release releases a resource.

func (Semaphore) TryAcquire added in v1.8.0

func (s Semaphore) TryAcquire() bool

TryAcquire reports, without blocking, whether the resource was acquired.

type ShardedMap added in v1.46.0

type ShardedMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

ShardedMap is a synchronized map[K]V, internally sharded by a user-defined K-sharding function.

The zero value is not safe for use; use NewShardedMap.

func NewShardedMap added in v1.46.0

func NewShardedMap[K comparable, V any](shards int, shard func(K) int) *ShardedMap[K, V]

NewShardedMap returns a new ShardedMap with the given number of shards and sharding function.

The shard func must return a integer in the range [0, shards) purely deterministically based on the provided K.

func (*ShardedMap[K, V]) Contains added in v1.46.0

func (m *ShardedMap[K, V]) Contains(key K) bool

Contains reports whether m contains key.

func (*ShardedMap[K, V]) Delete added in v1.46.0

func (m *ShardedMap[K, V]) Delete(key K) (shrunk bool)

Delete removes key from m.

It reports whether the map size shrunk (that is, whether key was present in the map).

func (*ShardedMap[K, V]) Get added in v1.46.0

func (m *ShardedMap[K, V]) Get(key K) (value V)

Get returns m[key] or the zero value of V if key is not present.

func (*ShardedMap[K, V]) GetOk added in v1.46.0

func (m *ShardedMap[K, V]) GetOk(key K) (value V, ok bool)

GetOk returns m[key] and whether it was present.

func (*ShardedMap[K, V]) Len added in v1.46.0

func (m *ShardedMap[K, V]) Len() int

Len returns the number of elements in m.

It does so by locking shards one at a time, so it's not particularly cheap, nor does it give a consistent snapshot of the map. It's mostly intended for metrics or testing.

func (*ShardedMap[K, V]) Mutate added in v1.46.0

func (m *ShardedMap[K, V]) Mutate(key K, mutator func(oldValue V, oldValueExisted bool) (newValue V, keep bool)) (sizeDelta int)

Mutate atomically mutates m[k] by calling mutator.

The mutator function is called with the old value (or its zero value) and whether it existed in the map and it returns the new value and whether it should be set in the map (true) or deleted from the map (false).

It returns the change in size of the map as a result of the mutation, one of -1 (delete), 0 (change), or 1 (addition).

func (*ShardedMap[K, V]) Set added in v1.46.0

func (m *ShardedMap[K, V]) Set(key K, value V) (grew bool)

Set sets m[key] = value.

present in m).

type WaitGroup added in v1.38.0

type WaitGroup struct{ sync.WaitGroup }

WaitGroup is identical to sync.WaitGroup, but provides a Go method to start a goroutine.

func (*WaitGroup) Go added in v1.38.0

func (wg *WaitGroup) Go(f func())

Go calls the given function in a new goroutine. It automatically increments the counter before execution and automatically decrements the counter after execution. It must not be called concurrently with Wait.

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