syncutil

package
v0.25.2 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const DeadlockEnabled = false

DeadlockEnabled is true if the deadlock detector is enabled.

Variables

View Source
var LogExpensiveLogEnabled = func(ctx context.Context, level int32) bool { return false }

LogExpensiveLogEnabled is injected from pkg/util/log to avoid an import cycle. This also allows it to be mocked out in tests.

See log.ExpensiveLogEnabled for more details.

View Source
var LogVEventfDepth = func(ctx context.Context, depth int, level int32, format string, args ...interface{}) {}

LogVEventfDepth is injected from pkg/util/log to avoid an import cycle. This also allows it to be mocked out in tests.

See log.LogVEventfDepth for more details.

Functions

This section is empty.

Types

type AtomicFloat64

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

AtomicFloat64 mimics the atomic types in the sync/atomic standard library, but for the float64 type. If you'd like to implement additional methods, consider checking out the expvar Float type for guidance: https://golang.org/src/expvar/expvar.go?s=2188:2222#L69

func (*AtomicFloat64) Add added in v0.25.2

func (f *AtomicFloat64) Add(delta float64) (new float64)

Add atomically adds delta to the float64 value and returns the new value.

func (*AtomicFloat64) Load added in v0.25.2

func (f *AtomicFloat64) Load() float64

Load atomically loads tha float64 value.

func (*AtomicFloat64) Store added in v0.25.2

func (f *AtomicFloat64) Store(val float64)

Store atomically stores a float64 value.

func (*AtomicFloat64) StoreIfHigher added in v0.25.2

func (f *AtomicFloat64) StoreIfHigher(new float64) (val float64)

StoreIfHigher atomically stores the given value if it is higher than the current value (in which case the given value is returned; otherwise the existing value is returned).

type AtomicString

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

AtomicString gives you atomic-style APIs for string.

func (*AtomicString) Get

func (s *AtomicString) Get() string

Get atomically returns the current value.

func (*AtomicString) Set

func (s *AtomicString) Set(val string)

Set atomically sets str as new value.

type Map added in v0.25.2

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Map is like a Go map[K]*V but is safe for concurrent use by multiple goroutines without additional locking or coordination. Loads, stores, and deletes run in amortized constant time.

The Map type is specialized. Most code should use a plain Go map instead, with separate locking or coordination, for better type safety and to make it easier to maintain other invariants along with the map content.

The Map type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple goroutines read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of an Map may significantly reduce lock contention compared to a Go map paired with a separate Mutex or RWMutex.

Nil values are not supported; to use an Map as a set store a dummy non-nil pointer instead of nil. The Set type is provided for this purpose.

The zero Map is valid and empty.

An Map must not be copied after first use.

In the terminology of the Go memory model, Map arranges that a write operation “synchronizes before” any read operation that observes the effect of the write, where read and write operations are defined as follows. Load, LoadAndDelete, LoadOrStore are read operations; Delete, LoadAndDelete, and Store are write operations; and LoadOrStore is a write operation when it returns loaded set to false.

func (*Map[K, V]) Delete added in v0.25.2

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

Delete deletes the value for a key.

func (*Map[K, V]) Load added in v0.25.2

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

Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.

func (*Map[K, V]) LoadAndDelete added in v0.25.2

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

LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.

func (*Map[K, V]) LoadOrStore added in v0.25.2

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

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*Map[K, V]) Range added in v0.25.2

func (m *Map[K, V]) Range(f func(key K, value *V) bool)

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

Range does not necessarily correspond to any consistent snapshot of the Map's contents: no key will be visited more than once, but if the value for any key is stored or deleted concurrently (including by f), Range may reflect any mapping for that key from any point during the Range call. Range does not block other methods on the receiver; even f itself may call any method on m.

Range may be O(N) with the number of elements in the map even if f returns false after a constant number of calls.

func (*Map[K, V]) Store added in v0.25.2

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

Store sets the value for a key.

type Mutex

type Mutex struct {
	sync.Mutex
}

A Mutex is a mutual exclusion lock.

func (*Mutex) AssertHeld

func (m *Mutex) AssertHeld()

AssertHeld may panic if the mutex is not locked (but it is not required to do so). Functions which require that their callers hold a particular lock may use this to enforce this requirement more directly than relying on the race detector.

Note that we do not require the lock to be held by any particular thread, just that some thread holds the lock. This is both more efficient and allows for rare cases where a mutex is locked in one thread and used in another.

func (*Mutex) TimedLock added in v0.25.2

func (m *Mutex) TimedLock() time.Duration

TimedLock is like Lock, but returns the time it took to acquire the lock.

func (*Mutex) TracedLock added in v0.25.2

func (m *Mutex) TracedLock(ctx context.Context)

TracedLock is like Lock, but logs a trace event using the provided context if the lock acquisition is slow.

type RWMutex

type RWMutex struct {
	sync.RWMutex
}

An RWMutex is a reader/writer mutual exclusion lock.

func (*RWMutex) AssertHeld

func (rw *RWMutex) AssertHeld()

AssertHeld may panic if the mutex is not locked for writing (but it is not required to do so). Functions which require that their callers hold a particular lock may use this to enforce this requirement more directly than relying on the race detector.

Note that we do not require the exclusive lock to be held by any particular thread, just that some thread holds the lock. This is both more efficient and allows for rare cases where a mutex is locked in one thread and used in another.

func (*RWMutex) AssertRHeld

func (rw *RWMutex) AssertRHeld()

AssertRHeld may panic if the mutex is not locked for reading (but it is not required to do so). If the mutex is locked for writing, it is also considered to be locked for reading. Functions which require that their callers hold a particular lock may use this to enforce this requirement more directly than relying on the race detector.

Note that we do not require the shared lock to be held by any particular thread, just that some thread holds the lock. This is both more efficient and allows for rare cases where a mutex is locked in one thread and used in another.

func (*RWMutex) TimedLock added in v0.25.2

func (rw *RWMutex) TimedLock() time.Duration

TimedLock is like Lock, but returns the time it took to acquire the lock.

func (*RWMutex) TimedRLock added in v0.25.2

func (rw *RWMutex) TimedRLock() time.Duration

TimedRLock is like RLock, but returns the time it took to acquire the lock.

func (*RWMutex) TracedLock added in v0.25.2

func (rw *RWMutex) TracedLock(ctx context.Context)

TracedLock is like Lock, but logs a trace event using the provided context if the lock acquisition is slow.

func (*RWMutex) TracedRLock added in v0.25.2

func (rw *RWMutex) TracedRLock(ctx context.Context)

TracedRLock is like RLock, but logs a trace event using the provided context if the lock acquisition is slow.

type Set added in v0.25.2

type Set[V comparable] struct {
	// contains filtered or unexported fields
}

Set is like a Go map[V]struct{} but is safe for concurrent use by multiple goroutines without additional locking or coordination. Loads, stores, and deletes run in amortized constant time.

See the Map type for more details.

func (*Set[V]) Add added in v0.25.2

func (s *Set[V]) Add(value V) bool

Add adds the value to the set.

Returns whether the value was added (true) or was already present (false).

func (*Set[V]) Contains added in v0.25.2

func (s *Set[V]) Contains(value V) bool

Contains returns whether the value is stored in the set.

Both Add and Remove return whether the value previously existed in the set, so Contains is typically not needed when later calling one of those methods. In fact, its use in logic that later mutates the set may be racy if not carefully considered, as there is no synchronization between the Contains call and the subsequent Add or Remove call.

func (*Set[V]) Range added in v0.25.2

func (s *Set[V]) Range(f func(value V) bool)

Range calls f sequentially for each value present in the set. If f returns false, range stops the iteration.

See Map.Range for more details.

func (*Set[V]) Remove added in v0.25.2

func (s *Set[V]) Remove(value V) bool

Remove removes the value from the set.

Returns whether the value was present and removed (true) or was not present and not removed (false).

Jump to

Keyboard shortcuts

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