xsync

package
v0.1.37 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package xsync implements some extra synchronization tools.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SendNoBlock

func SendNoBlock[T any](c chan T, value T) (status int)

SendNoBlock tries to send a value through the channel.

It returns 0 if the value was sent, 1 if sending it blocks (channel buffer full) or 2 if the channel `c` was closed.

func TrySend

func TrySend[T any](c chan T, value T) (ok bool)

TrySend tries to send value through the channel. It returns false if it failed, presumably because the channel is closed.

Types

type DynamicWaitGroup

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

func NewDynamicWaitGroup() *DynamicWaitGroup

NewDynamicWaitGroup creates a new DynamicWaitGroup.

func (*DynamicWaitGroup) Add

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

func (cwg *DynamicWaitGroup) Done()

Done decrements the DynamicWaitGroup counter by one. This is a convenience wrapper around Add(-1).

func (*DynamicWaitGroup) Wait

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.

func NewLatch

func NewLatch() *Latch

NewLatch returns an un-triggered latch.

func (*Latch) Test

func (l *Latch) Test() bool

Test checks whether the latch has been triggered.

func (*Latch) Trigger

func (l *Latch) Trigger()

Trigger latch.

func (*Latch) Wait

func (l *Latch) Wait()

Wait waits for the latch to be triggered.

func (*Latch) WaitChan

func (l *Latch) WaitChan() <-chan struct{}

WaitChan returns the channel that one can use on a `select` to check when the latch triggers. The returned channel is closed when the latch is 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 Lifecycle added in v0.1.33

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

Lifecycle coordinates concurrent operations against an idempotent Finalize teardown.

In-flight operations call Enter/Leave (acquiring a shared RWMutex read lock), while Finalize acquires the exclusive write lock, waiting for all in-flight operations to finish before running the teardown callback exactly once.

Zero value is ready to use. Must not be copied after first use.

IMPORTANT: Go's sync.RWMutex is not reentrant. Never call Enter() again on the same goroutine before calling Leave(), or a concurrent Finalize() waiting on Lock() will deadlock.

func (*Lifecycle) Enter added in v0.1.33

func (l *Lifecycle) Enter() bool

Enter acquires a shared read lock for an in-flight operation.

If Finalize has already been called, Enter returns false without holding any lock. If Enter returns true, the caller MUST call Leave exactly once when the operation finishes.

Do not call Enter reentrantly from a goroutine that already holds Enter.

func (*Lifecycle) Finalize added in v0.1.33

func (l *Lifecycle) Finalize(teardown func())

Finalize waits for all in-flight Enter/Leave pairs to complete, marks the lifecycle as finalized, and invokes teardown exactly once while holding the exclusive write lock.

Concurrent callers to Finalize block until teardown completes and then return without invoking teardown again.

func (*Lifecycle) IsFinalized added in v0.1.33

func (l *Lifecycle) IsFinalized() bool

IsFinalized reports whether Finalize has started or completed. It is lock-free and safe to call concurrently from any goroutine.

func (*Lifecycle) Leave added in v0.1.33

func (l *Lifecycle) Leave()

Leave releases the shared read lock acquired by a successful Enter.

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 semaphore version with a fixed capacity. This shouldn't matter for more coarse resource control.

func NewSemaphore

func NewSemaphore(capacity int) *Semaphore

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

func (s *Semaphore) Resize(newCapacity int)

Resize the number of available resources in the Semaphore.

If the newCapacity is larger than the 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 the newCapacity is smaller than the 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

func (s *Semaphore) TryAcquire() bool

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

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

func (m *SyncMap[K, V]) Clear()

Clear removes all key-value pairs from the map.

func (*SyncMap[K, V]) Delete

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

Delete deletes the value for a key.

func (*SyncMap[K, V]) Load

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

Load returns the value stored in the map for a key or nil if no value is present. The ok result indicates whether a value was found in the map.

func (*SyncMap[K, V]) LoadAndDelete

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

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

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

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.

func (*SyncMap[K, V]) Range

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

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

func (*SyncMap[K, V]) Store

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

Store sets the value for a key.

type WeakHandle added in v0.1.33

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

WeakHandle represents a tracked registration in a WeakTracker. Calling Untrack on a nil handle or multiple times is a safe no-op.

func (*WeakHandle[T]) Untrack added in v0.1.33

func (h *WeakHandle[T]) Untrack()

Untrack removes the object from its WeakTracker and cancels its GC cleanup. Safe to call concurrently, multiple times, or on a nil handle.

type WeakTracker added in v0.1.33

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

WeakTracker tracks live objects of type *T without preventing them from being garbage-collected.

Intended for low-frequency objects such as compiled Executables and Builders (NOT hot-path Buffers). Tracked objects are automatically removed from the tracker if garbage-collected before Untrack is called.

Zero value is ready to use. Must not be copied after first use.

func (*WeakTracker[T]) Drain added in v0.1.33

func (t *WeakTracker[T]) Drain() []*T

Drain marks the tracker closed, clears all entries, and returns strong pointers to all currently reachable tracked objects (omitting any already collected by GC).

Drain releases the tracker's mutex before returning so callers can safely finalize each returned object (which may call Untrack) without risking deadlocks.

func (*WeakTracker[T]) Len added in v0.1.33

func (t *WeakTracker[T]) Len() int

Len returns the current number of entries in the tracker (including any not yet pruned by GC cleanup).

func (*WeakTracker[T]) Track added in v0.1.33

func (t *WeakTracker[T]) Track(obj *T) *WeakHandle[T]

Track registers obj in the tracker and returns a handle that should be passed to Untrack when obj is explicitly finalized.

If obj is nil or the tracker has already been drained, Track returns nil.

Jump to

Keyboard shortcuts

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