Documentation
¶
Overview ¶
Package xsync implements some extra synchronization tools.
Index ¶
- func SendNoBlock[T any](c chan T, value T) (status int)
- func TrySend[T any](c chan T, value T) (ok bool)
- type DynamicWaitGroup
- type Latch
- type LatchWithValue
- type Semaphore
- type SyncMap
- func (m *SyncMap[K, V]) Clear()
- func (m *SyncMap[K, V]) Delete(key K)
- func (m *SyncMap[K, V]) Load(key K) (value V, ok bool)
- func (m *SyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool)
- func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)
- func (m *SyncMap[K, V]) Range(f func(key K, value V) bool)
- func (m *SyncMap[K, V]) Store(key K, value V)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SendNoBlock ¶
SendNoBlock tries to send value through the channel. It returns 0 if the value was sent, 1 if sending it would block (channel buffer full) or 2 if the channel `c` was closed.
Types ¶
type DynamicWaitGroup ¶ added in v0.19.5
type DynamicWaitGroup struct {
// contains filtered or unexported fields
}
DynamicWaitGroup is a WaitGroup-like synchronization primitive that allows the count to be changed (new values added) while someone is waiting for it.
It uses sync.Cond to coordinate changes.
func NewDynamicWaitGroup ¶ added in v0.19.5
func NewDynamicWaitGroup() *DynamicWaitGroup
NewDynamicWaitGroup creates a new DynamicWaitGroup.
func (*DynamicWaitGroup) Add ¶ added in v0.19.5
func (cwg *DynamicWaitGroup) Add(delta int)
Add changes the DynamicWaitGroup counter by the given delta. If the counter becomes zero, it broadcasts to all waiting goroutines. If the counter would go negative, it panics.
func (*DynamicWaitGroup) Done ¶ added in v0.19.5
func (cwg *DynamicWaitGroup) Done()
Done decrements the DynamicWaitGroup counter by one. This is a convenience wrapper around Add(-1).
func (*DynamicWaitGroup) Wait ¶ added in v0.19.5
func (cwg *DynamicWaitGroup) Wait()
Wait blocks until the DynamicWaitGroup counter is zero.
type Latch ¶
type Latch struct {
// contains filtered or unexported fields
}
Latch implements a "latch" synchronization mechanism.
A Latch is a signal that can be waited for until it is triggered. Once triggered it never changes state, it's forever triggered.
type LatchWithValue ¶
type LatchWithValue[T any] struct { // contains filtered or unexported fields }
LatchWithValue implements a "latch" synchronization mechanism, with a value associated with the triggering of the latch.
A LatchWithValue is a signal that can be waited for until it is triggered. Once triggered it never changes state, it's forever triggered.
func NewLatchWithValue ¶
func NewLatchWithValue[T any]() *LatchWithValue[T]
NewLatchWithValue returns an un-triggered latch.
func (*LatchWithValue[T]) Test ¶
func (l *LatchWithValue[T]) Test() bool
Test checks whether the latch has been triggered.
func (*LatchWithValue[T]) Trigger ¶
func (l *LatchWithValue[T]) Trigger(value T)
Trigger latch and saves the associated value.
func (*LatchWithValue[T]) Wait ¶
func (l *LatchWithValue[T]) Wait() T
Wait waits for the latch to be triggered.
type Semaphore ¶
type Semaphore struct {
// contains filtered or unexported fields
}
Semaphore that allows dynamic resizing.
It uses a sync.Cond, to allow dynamic resizing, so it will be slower than a pure channel version of a semaphore, with a fixed capacity. This shouldn't matter for more coarse resource control.
func NewSemaphore ¶
NewSemaphore returns a Semaphore that allows at most capacity simultaneous acquisitions. If capacity <= 0, there is no limit on acquisitions.
FIFO ordering may be lost during resizes (Semaphore.Resize) to larger capacity, but otherwise it is respected.
func (*Semaphore) Acquire ¶
func (s *Semaphore) Acquire()
Acquire resource observing current semaphore capacity. It must be matched by exactly one call to Semaphore.Release after the reservation is no longer needed.
func (*Semaphore) Release ¶
func (s *Semaphore) Release()
Release resource previously allocated with Semaphore.Acquire.
func (*Semaphore) Resize ¶
Resize number of available resources in the Semaphore.
If newCapacity is larger than previous one, this may immediately allow pending Semaphore.Acquire to proceed. Notice since all waiting Semaphore.Acquire are awoken (broadcast), the queue order may be lost.
If newCapacity is smaller than previous one, it doesn't have any effect on current acquisitions. So if the Semaphore is being used to control a worker pool, reducing its size won't stop workers currently executing.
func (*Semaphore) TryAcquire ¶ added in v0.19.5
TryAcquire tries to acquire resource without blocking. It returns true if the resource was acquired, false if the semaphore is full.
If acquisition was successful (returned true), the caller must call Semaphore.Release.
type SyncMap ¶ added in v0.18.1
type SyncMap[K comparable, V any] struct { Map sync.Map }
SyncMap is a trivial wrapper to sync.Map that casts the key and value types accordingly.
As sync.Map, it can be created ready to go, but should not be copied once it is used.
func (*SyncMap[K, V]) Clear ¶ added in v0.18.1
func (m *SyncMap[K, V]) Clear()
Clear removes all key-value pairs from the map.
func (*SyncMap[K, V]) Delete ¶ added in v0.18.1
func (m *SyncMap[K, V]) Delete(key K)
Delete deletes the value for a key.
func (*SyncMap[K, V]) Load ¶ added in v0.18.1
Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.
func (*SyncMap[K, V]) LoadAndDelete ¶ added in v0.18.1
LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.
func (*SyncMap[K, V]) LoadOrStore ¶ added in v0.18.1
LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.