Documentation ¶
Overview ¶
Package sync provides synchronisation primitives not available in the standard sync package.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrToggleClosed = errors.New("toggle closed")
ErrToggleClosed is returned by Toggle.Wait() if Toggle.Close() was called.
Functions ¶
This section is empty.
Types ¶
type Toggle ¶
type Toggle struct {
// contains filtered or unexported fields
}
A Toggle allows for Wait()ing until a condition is true. Unlike a broadcast mechanism that may be missed if waiting begins after a signal, waiting on an already "on" Toggle returns immediately.
The zero value for a Toggle is equivalent to Set(false). A Toggle MUST NOT be copied as it contains a sync.Mutex.
Toggle.Set(true) is a replacement for sync.Cond.Broadcast().
The implementation uses a channel with a single-item buffer. When Set() to true, the Toggle adds an item to the buffer, and when Set() to false, it removes said item. All calls to Wait() receive on the channel to unblock, and then immediately return the item. This allows for Context cancellation to be honoured.
func (*Toggle) Close ¶
func (t *Toggle) Close()
Close closes the Toggle. All Wait()ers, current and future, unblock and return ErrToggleClosed.
func (*Toggle) Set ¶
Set sets the state of the Toggle. If the state is true, all current and future calls to Wait() will unblock. Calls to Set are idempotent.
Behaviour of Set() is undefined on a Close()d Toggle.