xsync

package
v0.15.2 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2024 License: Apache-2.0 Imports: 1 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 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.

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 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 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

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 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.

Jump to

Keyboard shortcuts

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