concurrency

package
v2.2.2 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoWithTimeout

func DoWithTimeout(w Waitable, action func(), timeout time.Duration) bool

DoWithTimeout performs the action as soon as the waitable is done. It gives up and returns after timeout, and returns a bool indicating whether the action was performed or not.

func IsDone

func IsDone(w Waitable) bool

IsDone checks if the given waitable's condition is fulfilled.

func Wait

func Wait(w Waitable)

Wait waits indefinitely until the condition represented by the given Waitable is fulfilled.

func WaitWithTimeout

func WaitWithTimeout(w Waitable, timeout time.Duration) bool

WaitWithTimeout waits for the given Waitable with a specified timeout. It returns false if the timeout expired before the condition was fulfilled, true otherwise.

Types

type Signal

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

Signal implements a signalling facility. Unlike sync.Cond, it is based on channels and can hence be used in `select` statements. There are two ways to instantiate a Signal. The preferred way is by calling `NewSignal()`, which will return a signal that is not triggered. Alternatively, the zero-value can be used to instantiate a signal in triggered condition, which is not what you usually want. To reset it to the non-triggered state, call `Reset()`. Similarly to `sync.(RW)Mutex` and `sync.Cond`, a signal should not be copied once used.

func NewSignal

func NewSignal() Signal

NewSignal creates a new signal that is in the reset state.

func (*Signal) Done

func (s *Signal) Done() <-chan struct{}

Done returns a channel that is closed when this signal was triggered.

func (*Signal) IsDone

func (s *Signal) IsDone() bool

IsDone checks if the signal was triggered. It is a slightly more efficient alternative to calling `IsDone(s)`.

func (*Signal) Reset

func (s *Signal) Reset() bool

Reset resets the signal to the non-triggered state, if necessary. The return value indicates whether a reset was actually performed (i.e., the signal was triggered). It returns false if the signal was not in the triggered state.

func (*Signal) Signal

func (s *Signal) Signal() bool

Signal triggers the signal. The return value indicates whether the signal was actually triggered. It returns false if the signal was already in the triggered state.

func (*Signal) SignalWhen

func (s *Signal) SignalWhen(triggerCond Waitable, cancelCond Waitable) bool

SignalWhen triggers this signal when the given trigger condition is satisfied. It returns as soon as either this signal is triggered (either by this function or another goroutine), or cancelCond is triggered (in which case the signal will not be triggered). CAREFUL: This function blocks; if you do not want this, invoke it in a goroutine.

func (*Signal) Snapshot

func (s *Signal) Snapshot() WaitableChan

Snapshot returns a WaitableChan that observers will only see triggering once, i.e., if this signal is triggered (or has been triggered) and then `Reset()` is called, subsequent calls to `Done()` on the returned object will still see a triggered channel.

func (*Signal) Wait

func (s *Signal) Wait()

Wait waits for the signal to be triggered. It is a slightly more efficient and convenient alternative to calling `Wait(s)`.

func (*Signal) WaitC

func (s *Signal) WaitC() WaitableChan

WaitC returns a WaitableChan for this signal.

type Waitable

type Waitable interface {
	Done() <-chan struct{}
}

Waitable is a generic interface for things that can be waited upon. The method `Done` returns a channel that, when closed, signals that whatever condition is represented by this waitable is satisfied. Note: The name `Done` was chosen such that `context.Context` conforms to this interface.

type WaitableChan

type WaitableChan <-chan struct{}

WaitableChan is an alias around a `<-chan struct{}` that returns itself in its `Done` method.

func (WaitableChan) Done

func (c WaitableChan) Done() <-chan struct{}

Done returns the channel itself.

Jump to

Keyboard shortcuts

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