msync

package module
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2026 License: BSD-3-Clause Imports: 1 Imported by: 1

README

msync

GoDoc CI

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

Packages

Documentation

Overview

Package msync defines some helpful types for managing concurrency.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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

Link is a snapshot of a Value acquired by a call to its LoadLink method. Use Link.Get to obtain the captured value. The captured value is fixed, and does not change if the underlying Value is updated separately.

A linked snapshot is either "valid" or "invalid". It is "valid" if a call to Link.StoreCond or Link.UpdateCond could succeed at some point in the future; otherwise it is "invalid". A valid snapshot may become invalid, but an invalid snapshot is permanently so. See also: Link.Validate.

func (*Link[T]) Get added in v0.7.0

func (lv *Link[T]) Get() T

Get returns the value captured in the snapshot.

func (*Link[T]) StoreCond added in v0.7.0

func (lv *Link[T]) StoreCond(v T) bool

StoreCond attempts to update the linked Value with v, and reports whether the update succeeded. An update succeeds if no successful StoreCond, UpdateCond, Set, or Update operation has been applied to the linked Value since the Value.LoadLink that initialized lv.

Once StoreCond has been called, whether successful or not, lv is invalid. It is safe to re-link and reuse an invalid Link. If StoreCond succeeds, the Get method returns the updated value.

func (*Link[T]) UpdateCond added in v0.9.1

func (lv *Link[T]) UpdateCond(f func(*T)) bool

UpdateCond attempts to update the linked Value with f, and reports whether the update succeeded. An update succeeds if no successful StoreCond, UpdateCond, Set, or Update operation has been applied to the linked Value since the Value.LoadLink that initialized lv. If an update is not possible, f is not called.

Once UpdateCond has been called, whether successful or not, lv is invalid. It is safe to re-link and reuse an invalid Link. If UpdateCond succeeds, the Get method returns the updated value.

The pointer passed to f will never be nil.

func (*Link[T]) Validate added in v0.7.0

func (lv *Link[T]) Validate() bool

Validate reports whether a call to Link.StoreCond or Link.UpdateCond would have succeeded given the current state of lv. When Validate reports true, it means lv was valid at the time of the call; it may have become invalid by the time the caller receives the result. If Validate reports false, lv is invalid. It is safe to re-link and reuse an invalid Link.

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.

The Value takes ownership of the value in its custody. In particular, if a pointer or a slice is stored in a Value, the caller must not modify the contents without separate synchronization.

Example
package main

import (
	"fmt"
	"log"
	"sync"
	"time"

	"github.com/creachadair/msync"
)

func main() {
	var s msync.Value[string]

	var wg sync.WaitGroup
	defer wg.Wait()

	// Callers can use Set to modify the value concurrently.
	wg.Go(func() { s.Set("apple") })
	wg.Go(func() { s.Set("pear") })
	wg.Go(func() { s.Set("plum") })
	wg.Go(func() { s.Set("cherry") })

	// Use Wait to block until a new value becomes available.
	select {
	case <-time.After(10 * time.Second):
		log.Fatal("Timeout waiting for value to change")
	case <-s.Wait():
		fmt.Printf("Value updated, now %v\n", s.Get())
	}

	// Use Get to fetch the current value at any time.
	fmt.Println(s.Get())
}

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 (v *Value[T]) LoadLink(lv *Link[T]) *Link[T]

LoadLink links a view of the current value of v. If lv == nil, a new link is allocated and returned. Otherwise, the contents of *lv are replaced and lv is returned.

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. Calling Set also wakes any goroutines that are blocked on a Wait channel. Set invalidates any linked snapshots open on v, even if the target value does not change.

func (*Value[T]) Update added in v0.9.1

func (v *Value[T]) Update(f func(*T))

Update updates the value stored in v in-place using f. Calling Update also wakes any goroutines that are blocked on a Wait channel, and invalidates any linked snapshots open on v.

The pointer passed to f will never be nil.

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

func (v *Value[T]) Wait() <-chan struct{}

Wait returns a channel that is closed the next time the value of v is set via Value.Set or a successful call to Link.StoreCond.

Directories

Path Synopsis
Package throttle allows calls to a function to be coalesced among multiple concurrent goroutines.
Package throttle allows calls to a function to be coalesced among multiple concurrent goroutines.
Package trigger implements a channel-based condition variable.
Package trigger implements a channel-based condition variable.

Jump to

Keyboard shortcuts

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