sync

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: MIT, Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package singleflight provides a duplicate function call suppression mechanism.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddFloat32

func AddFloat32(addr *float32, delta float32) (new float32)

AddFloat32 updates or inserts a value.

func AddFloat64

func AddFloat64(addr *float64, delta float64) (new float64)

AddFloat64 add delta to given address atomically

func CasItem

func CasItem(p *unsafe.Pointer, old, new *DirectItem) bool

CasItem performs an atomic compare-and-swap on a DirectItem pointer.

func CasNode

func CasNode[T any](p *unsafe.Pointer, old, new *Node[T]) bool

CasNode performs an atomic compare-and-swap on a Node pointer.

func SubUint32

func SubUint32(addr *uint32, delta uint32) (new uint32)

SubUint32 performs the operation.

func SubUint64

func SubUint64(addr *uint64, delta uint64) (new uint64)

SubUint64 performs the operation.

func WaitGroupState

func WaitGroupState(wg *sync.WaitGroup) (counter int32, wcounter uint32)

WaitGroupState reads internal counter/state values from the standard sync.WaitGroup.

func WaitGroupStopWait

func WaitGroupStopWait(wg *sync.WaitGroup)

WaitGroupStopWait reduces the wait-group counter by its current running count.

Types

type DirectItem

type DirectItem struct {
	Next  unsafe.Pointer
	Value any
}

func LoadItem

func LoadItem(p *unsafe.Pointer) *DirectItem

LoadItem atomically loads a DirectItem pointer.

type Group added in v1.7.8

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

Group represents a class of work and forms a namespace in which units of work can be executed with duplicate suppression.

func (*Group) Do added in v1.7.8

func (g *Group) Do(key any, fn func() (any, error)) (v any, err error, shared bool)

Do executes and returns the results of the given function, making sure that only one execution is in-flight for a given key at a time. If a duplicate comes in, the duplicate caller waits for the original to complete and receives the same results. The return value shared indicates whether v was given to multiple callers.

func (*Group) DoChan added in v1.7.8

func (g *Group) DoChan(key any, fn func() (any, error)) <-chan Result

DoChan is like Do but returns a channel that will receive the results when they are ready.

The returned channel will not be closed.

func (*Group) Forget added in v1.7.8

func (g *Group) Forget(key any)

Forget tells the singleflight to forget about a key. Future calls to Do for this key will call the function rather than waiting for an earlier call to complete.

type Node

type Node[T any] struct {
	Next  atomic.Pointer[Node[T]]
	Value T
}

Node has better performance than using DirectItem.

func LoadNode

func LoadNode[T any](p *unsafe.Pointer) *Node[T]

LoadNode atomically loads a Node pointer.

type Pool added in v1.6.8

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

func NewPool added in v1.7.2

func NewPool[T any](fn func() T) *Pool[T]

NewPool creates and returns a new instance.

func (*Pool[T]) Get added in v1.6.8

func (p *Pool[T]) Get() T

Get gets a T from the pool, or creates a new one if the pool is empty.

func (*Pool[T]) Put added in v1.6.8

func (p *Pool[T]) Put(x T)

Put returns x into the pool.

type Result added in v1.7.8

type Result struct {
	Val    any
	Err    error
	Shared bool
}

Result holds the results of Do, so they can be passed on a channel.

type WaitGroup

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

running count, waiter count, and signal count.

func (*WaitGroup) State

func (wg *WaitGroup) State() (counter int32, wcounter uint32)

State returns the wait-group's internal counter and waiter counter.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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