msync

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2022 License: BSD-3-Clause Imports: 2 Imported by: 0 modules, 1 package

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.

README

msync

GoDoc

This repository defines a library of Go types to help in the management of concurrency.

Documentation

Overview

Package msync defines some helpful types for managing concurrency.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handoff

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

A Handoff is a level-triggered single-value buffer shared by a producer and a consumer. A producer calls Send to make a value available, and a consumer calls Ready to obtain a channel which delivers the most recently-sent value.

Sending a value to the handoff does not block: Once a value is buffered, additional values are discarded until the buffered value is consumed.

The Ready method returns a channel that delivers buffered values to the consumer. Once a value has been consumed, the buffer is empty and the producer can send another.

func NewHandoff

func NewHandoff[T any]() *Handoff[T]

NewHandoff constructs a new empty handoff.

func (*Handoff[T]) Ready

func (h *Handoff[T]) Ready() <-chan T

Ready returns a channel that delivers a value when a handoff is available.

func (*Handoff[T]) Send

func (h *Handoff[T]) Send(v T) bool

Send buffers or discards v, and reports whether v was successfully buffered for handoff (true) discarded (false). Send does not block.

type Trigger

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

Trigger is an edge-triggered condition shared by multiple goroutines. It is analogous to the standard condition variable (sync.Cond) but uses a channel for signaling.

To wait on the trigger, call Ready to obtain a channel. This channel will be closed the next time the Signal method is executed. A fresh channel is created after each call to Signal, and is shared among all calls to Ready that occur in the window between signals.

A zero Trigger is ready for use but must not be copied after first use.

func NewTrigger

func NewTrigger() *Trigger

NewTrigger constructs a new unready Trigger.

func (*Trigger) Ready

func (t *Trigger) Ready() <-chan struct{}

Ready returns a channel that is closed when t is signaled.

func (*Trigger) Signal

func (t *Trigger) Signal()

Signal wakes all pending waiters and resets the trigger.

type Value added in v0.0.2

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

A Value is a mutable container for a single value of type T that can be concurrently accessed by multiple goroutines. A zero Value is ready for use, but must not be copied after its first use.

func NewValue added in v0.0.2

func NewValue[T any](init T) *Value[T]

NewValue creates a new Value with the given initial value.

func (*Value[T]) Get added in v0.0.2

func (v *Value[T]) Get() T

Get returns the current value stored in v.

func (*Value[T]) Set added in v0.0.2

func (v *Value[T]) Set(newValue T)

Set updates the value stored in v to newValue.

func (*Value[T]) Wait added in v0.0.2

func (v *Value[T]) Wait(ctx context.Context) (T, bool)

Wait blocks until v.Set is called, or until ctx ends, and returns the current value in v. The flag indicating whether Set was called (true) or ctx timed out (false).

If v.Set is not called before ctx ends, Wait returns the value v held when Wait was called. Otherwise, Wait returns the value from one of the v.Set calls made during its execution.

If multiple goroutines set v concurrently with a call to Wait, the Wait call will return the value from one of them, but not necessarily the first.

Jump to

Keyboard shortcuts

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