Movatterモバイル変換


[0]ホーム

URL:


syncs

package
v1.92.2Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2025 License:BSD-3-ClauseImports:9Imported by:62

Details

Repository

github.com/tailscale/tailscale

Links

Documentation

Overview

Package syncs contains additional sync types and functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

funcAssertLockedadded inv1.14.0

func AssertLocked(m *Mutex)

AssertLocked panics if m is not locked.

funcAssertRLockedadded inv1.14.0

func AssertRLocked(rw *RWMutex)

AssertRLocked panics if rw is not locked for reading or writing.

funcAssertWLockedadded inv1.14.0

func AssertWLocked(rw *sync.RWMutex)

AssertWLocked panics if rw is not locked for writing.

funcClosedChan

func ClosedChan() <-chan struct{}

ClosedChan returns a channel that's already closed.

Types

typeAtomicValueadded inv1.30.0

type AtomicValue[Tany] struct {// contains filtered or unexported fields}

AtomicValue is the generic version ofatomic.Value.SeeMutexValue for guidance on whether to use this type.

func (*AtomicValue[T])CompareAndSwapadded inv1.30.0

func (v *AtomicValue[T]) CompareAndSwap(oldV, newV T) (swappedbool)

CompareAndSwap executes the compare-and-swap operation for the Value.It panics if T is not comparable.

func (*AtomicValue[T])Loadadded inv1.30.0

func (v *AtomicValue[T]) Load() T

Load returns the value set by the most recent Store.It returns the zero value for T if the value is empty.

func (*AtomicValue[T])LoadOkadded inv1.30.0

func (v *AtomicValue[T]) LoadOk() (_ T, okbool)

LoadOk is like Load but returns a boolean indicating whether the value wasloaded.

func (*AtomicValue[T])Storeadded inv1.30.0

func (v *AtomicValue[T]) Store(x T)

Store sets the value of the Value to x.

func (*AtomicValue[T])Swapadded inv1.30.0

func (v *AtomicValue[T]) Swap(x T) (old T)

Swap stores new into Value and returns the previous value.It returns the zero value for T if the value is empty.

typeMapadded inv1.34.0

type Map[Kcomparable, Vany] struct {// contains filtered or unexported fields}

Map is a Go map protected by async.RWMutex.It is preferred oversync.Map for maps with entries that changeat a relatively high frequency.This must not be shallow copied.

func (*Map[K, V])Alladded inv1.76.0

func (m *Map[K, V]) All()iter.Seq2[K, V]

All iterates over all entries in the map in an undefined order.A read lock is held for the entire duration of the iteration.Use the [WithLock] method instead to mutate the map during iteration.

func (*Map[K, V])Clearadded inv1.50.0

func (m *Map[K, V]) Clear()

Clear removes all entries from the map.

func (*Map[K, V])Deleteadded inv1.34.0

func (m *Map[K, V]) Delete(key K)

Delete deletes the entry identified by key.

func (*Map[K, V])Keysadded inv1.76.0

func (m *Map[K, V]) Keys()iter.Seq[K]

Keys iterates over all keys in the map in an undefined order.A read lock is held for the entire duration of the iteration.Use the [WithLock] method instead to mutate the map during iteration.

func (*Map[K, V])Lenadded inv1.42.0

func (m *Map[K, V]) Len()int

Len returns the length of the map.

func (*Map[K, V])Loadadded inv1.34.0

func (m *Map[K, V]) Load(key K) (value V, loadedbool)

Load loads the value for the provided key and whether it was found.

func (*Map[K, V])LoadAndDeleteadded inv1.34.0

func (m *Map[K, V]) LoadAndDelete(key K) (value V, loadedbool)

LoadAndDelete returns the value for the given key if it exists.It ensures that the map is cleared of any entry for the key.

func (*Map[K, V])LoadFuncadded inv1.52.0

func (m *Map[K, V]) LoadFunc(key K, f func(value V, loadedbool))

LoadFunc calls f with the value for the provided keyregardless of whether the entry exists or not.The lock is held for the duration of the call to f.

func (*Map[K, V])LoadOrInitadded inv1.52.0

func (m *Map[K, V]) LoadOrInit(key K, f func() V) (actual V, loadedbool)

LoadOrInit returns the value for the given key if it existsotherwise f is called to construct the value to be set.The lock is held for the duration to prevent duplicate initialization.

func (*Map[K, V])LoadOrStoreadded inv1.34.0

func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loadedbool)

LoadOrStore returns the value for the given key if it existsotherwise it stores value.

func (*Map[K, V])Storeadded inv1.34.0

func (m *Map[K, V]) Store(key K, value V)

Store stores the value for the provided key.

func (*Map[K, V])Swapadded inv1.64.0

func (m *Map[K, V]) Swap(key K, value V) (oldValue V)

Swap stores the value for the provided key, and returns the previous value(if any). If there was no previous value set, a zero value will be returned.

func (*Map[K, V])Valuesadded inv1.76.0

func (m *Map[K, V]) Values()iter.Seq[V]

Values iterates over all values in the map in an undefined order.A read lock is held for the entire duration of the iteration.Use the [WithLock] method instead to mutate the map during iteration.

func (*Map[K, V])WithLockadded inv1.70.0

func (m *Map[K, V]) WithLock(f func(m2 map[K]V))

WithLock calls f with the underlying map.Use of m2 must not escape the duration of this call.The write-lock is held for the entire duration of this call.

typeMutexadded inv1.92.0

type Mutex =sync.Mutex

Mutex is an alias for sync.Mutex.

It's only not a sync.Mutex when built with the ts_mutex_debug build tag.

typeMutexValueadded inv1.80.0

type MutexValue[Tany] struct {// contains filtered or unexported fields}

MutexValue is a value protected by a mutex.

AtomicValue,MutexValue,atomic.Pointer are similar andoverlap in their use cases.

  • Useatomic.Pointer if the value being stored is a pointer andyou only ever need load and store operations.An atomic pointer only occupies 1 word of memory.

  • UseMutexValue if the value being stored is not a pointer oryou need the ability for a mutex to protect a set of operationsperformed on the value.A mutex-guarded value occupies 1 word of memory plusthe memory representation of T.

  • AtomicValue is useful for non-pointer types that happen tohave the memory layout of a single pointer.Examples include a map, channel, func, or a single field structthat contains any prior types.An atomic value occupies 2 words of memory.Consequently, Storing of non-pointer types always allocates.

Note thatAtomicValue has the ability to report whether it was setwhileMutexValue lacks the ability to detect if the value was setand it happens to be the zero value of T. If such a use case isnecessary, then you could consider wrapping T in [opt.Value].

func (*MutexValue[T])Loadadded inv1.80.0

func (m *MutexValue[T]) Load() T

Load returns a shallow copy of the underlying value.

func (*MutexValue[T])Storeadded inv1.80.0

func (m *MutexValue[T]) Store(v T)

Store stores a shallow copy of the provided value.

func (*MutexValue[T])Swapadded inv1.80.0

func (m *MutexValue[T]) Swap(new T) (old T)

Swap stores new into m and returns the previous value.

func (*MutexValue[T])WithLockadded inv1.80.0

func (m *MutexValue[T]) WithLock(f func(p *T))

WithLock calls f with a pointer to the value while holding the lock.The provided pointer must not leak beyond the scope of the call.

typePooladded inv1.70.0

type Pool[Tany] struct {// New optionally specifies a function to generate// a value when Get would otherwise return the zero value of T.// It may not be changed concurrently with calls to Get.New func() T// contains filtered or unexported fields}

Pool is the generic version ofsync.Pool.

func (*Pool[T])Getadded inv1.70.0

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

Get selects an arbitrary item from the Pool, removes it from the Pool,and returns it to the caller. Seesync.Pool.Get.

func (*Pool[T])Putadded inv1.70.0

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

Put adds x to the pool.

typeRWMutexadded inv1.92.0

type RWMutex =sync.RWMutex

RWMutex is an alias for sync.RWMutex.

It's only not a sync.RWMutex when built with the ts_mutex_debug build tag.

typeSemaphoreadded inv1.8.0

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

Semaphore is a counting semaphore.

Use NewSemaphore to create one.

funcNewSemaphoreadded inv1.8.0

func NewSemaphore(nint)Semaphore

NewSemaphore returns a semaphore with resource count n.

func (Semaphore)Acquireadded inv1.8.0

func (sSemaphore) Acquire()

Acquire blocks until a resource is acquired.

func (Semaphore)AcquireContextadded inv1.8.0

func (sSemaphore) AcquireContext(ctxcontext.Context)bool

AcquireContext reports whether the resource was acquired before the ctx was done.

func (Semaphore)Lenadded inv1.88.0

func (sSemaphore) Len()int

Len reports the number of in-flight acquisitions.It is incremented whenever the semaphore is acquired.It is decremented whenever the semaphore is released.

func (Semaphore)Releaseadded inv1.8.0

func (sSemaphore) Release()

Release releases a resource.

func (Semaphore)TryAcquireadded inv1.8.0

func (sSemaphore) TryAcquire()bool

TryAcquire reports, without blocking, whether the resource was acquired.

typeShardValueadded inv1.80.0

type ShardValue[Tany] struct {// contains filtered or unexported fields}

ShardValue contains a value sharded over a set of shards.In order to be useful, T should be aligned to cache lines.Users must organize that usage in One and All is concurrency safe.The zero value is not safe for use; useNewShardValue.

funcNewShardValueadded inv1.80.0

func NewShardValue[Tany]() *ShardValue[T]

NewShardValue constructs a new ShardValue[T] with a shard per CPU.

func (*ShardValue[T])Alladded inv1.80.0

func (sp *ShardValue[T]) All(yield func(*T)bool)

All yields a pointer to the value in each shard.

func (*ShardValue[T])Lenadded inv1.80.0

func (sp *ShardValue[T]) Len()int

Len returns the number of shards.

func (*ShardValue[T])Oneadded inv1.80.0

func (sp *ShardValue[T]) One(yield func(*T))

One yields a pointer to a single shard value with best-effort P-locality.

typeShardedIntadded inv1.80.0

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

ShardedInt provides a sharded atomic int64 value that optimizes highfrequency (Mhz range and above) writes in highly parallel workloads.The zero value is not safe for use; useNewShardedInt.ShardedInt implements the expvar.Var interface.

funcNewShardedIntadded inv1.80.0

func NewShardedInt() *ShardedInt

NewShardedInt returns a newShardedInt.

func (*ShardedInt)Addadded inv1.80.0

func (m *ShardedInt) Add(deltaint64)

Add adds delta to the value.

func (*ShardedInt)AppendTextadded inv1.80.0

func (m *ShardedInt) AppendText(b []byte) ([]byte,error)

AppendText implements the encoding.TextAppender interface

func (*ShardedInt)GetDistributionadded inv1.80.0

func (m *ShardedInt) GetDistribution() []int64

GetDistribution returns the current value in each shard.This is intended for observability/debugging only.

func (*ShardedInt)Stringadded inv1.80.0

func (m *ShardedInt) String()string

String implements the expvar.Var interface

func (*ShardedInt)Valueadded inv1.80.0

func (m *ShardedInt) Value()int64

Value returns the current value.

typeShardedMapadded inv1.46.0

type ShardedMap[Kcomparable, Vany] struct {// contains filtered or unexported fields}

ShardedMap is a synchronized map[K]V, internally sharded by a user-definedK-sharding function.

The zero value is not safe for use; use NewShardedMap.

funcNewShardedMapadded inv1.46.0

func NewShardedMap[Kcomparable, Vany](shardsint, shard func(K)int) *ShardedMap[K, V]

NewShardedMap returns a new ShardedMap with the given number of shards andsharding function.

The shard func must return a integer in the range [0, shards) purelydeterministically based on the provided K.

func (*ShardedMap[K, V])Containsadded inv1.46.0

func (m *ShardedMap[K, V]) Contains(key K)bool

Contains reports whether m contains key.

func (*ShardedMap[K, V])Deleteadded inv1.46.0

func (m *ShardedMap[K, V]) Delete(key K) (shrunkbool)

Delete removes key from m.

It reports whether the map size shrunk (that is, whether key was present inthe map).

func (*ShardedMap[K, V])Getadded inv1.46.0

func (m *ShardedMap[K, V]) Get(key K) (value V)

Get returns m[key] or the zero value of V if key is not present.

func (*ShardedMap[K, V])GetOkadded inv1.46.0

func (m *ShardedMap[K, V]) GetOk(key K) (value V, okbool)

GetOk returns m[key] and whether it was present.

func (*ShardedMap[K, V])Lenadded inv1.46.0

func (m *ShardedMap[K, V]) Len()int

Len returns the number of elements in m.

It does so by locking shards one at a time, so it's not particularly cheap,nor does it give a consistent snapshot of the map. It's mostly intended formetrics or testing.

func (*ShardedMap[K, V])Mutateadded inv1.46.0

func (m *ShardedMap[K, V]) Mutate(key K, mutator func(oldValue V, oldValueExistedbool) (newValue V, keepbool)) (sizeDeltaint)

Mutate atomically mutates m[k] by calling mutator.

The mutator function is called with the old value (or its zero value) andwhether it existed in the map and it returns the new value and whether itshould be set in the map (true) or deleted from the map (false).

It returns the change in size of the map as a result of the mutation, one of-1 (delete), 0 (change), or 1 (addition).

func (*ShardedMap[K, V])Setadded inv1.46.0

func (m *ShardedMap[K, V]) Set(key K, value V) (grewbool)

Set sets m[key] = value.

present in m).

typeWaitGroupChan

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

WaitGroupChan is like a sync.WaitGroup, but has a chan that closeson completion that you can wait on. (This, you can only use thevalue once)Also, its zero value is not usable. Use the constructor.

funcNewWaitGroupChan

func NewWaitGroupChan() *WaitGroupChan

NewWaitGroupChan returns a new single-use WaitGroupChan.

func (*WaitGroupChan)Add

func (wg *WaitGroupChan) Add(deltaint)

Add adds delta, which may be negative, to the WaitGroupChancounter. If the counter becomes zero, all goroutines blocked onWait or the Done chan are released. If the counter goes negative,Add panics.

Note that calls with a positive delta that occur when the counteris zero must happen before a Wait. Calls with a negative delta, orcalls with a positive delta that start when the counter is greaterthan zero, may happen at any time. Typically this means the callsto Add should execute before the statement creating the goroutineor other event to be waited for.

func (*WaitGroupChan)Decr

func (wg *WaitGroupChan) Decr()

Decr decrements the WaitGroup counter by one.

(It is like sync.WaitGroup's Done method, but we don't use Done inthis type, because it's ambiguous between Context.Done andWaitGroup.Done. So we use DoneChan and Decr instead.)

func (*WaitGroupChan)DoneChan

func (wg *WaitGroupChan) DoneChan() <-chan struct{}

DoneChan returns a channel that's closed on completion.

func (*WaitGroupChan)Wait

func (wg *WaitGroupChan) Wait()

Wait blocks until the WaitGroupChan counter is zero.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp