xsync

package module
v0.0.0-...-0f612f7 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: CC0-1.0 Imports: 8 Imported by: 76

Documentation

Overview

Package xsync provides synchronization primitives and helpers.

do.go defines the Doer interface and generic helpers to execute functions of various signatures under a lock.

Index

Constants

This section is empty.

Variables

View Source
var DefaultDeadlockTimeout = time.Minute

Functions

func DoA1

func DoA1[A0 any](
	ctx context.Context,
	m Doer,
	fn func(A0),
	a0 A0,
)

func DoA1R1

func DoA1R1[A0, R0 any](
	ctx context.Context,
	m Doer,
	fn func(A0) R0,
	a0 A0,
) R0

func DoA1R2

func DoA1R2[A0, R0, R1 any](
	ctx context.Context,
	m Doer,
	fn func(A0) (R0, R1),
	a0 A0,
) (R0, R1)

func DoA1R4

func DoA1R4[A0, R0, R1, R2, R3 any](
	ctx context.Context,
	m Doer,
	fn func(A0) (R0, R1, R2, R3),
	a0 A0,
) (R0, R1, R2, R3)

func DoA2

func DoA2[A0, A1 any](
	ctx context.Context,
	m Doer,
	fn func(A0, A1),
	a0 A0,
	a1 A1,
)

func DoA2R1

func DoA2R1[A0, A1, R0 any](
	ctx context.Context,
	m Doer,
	fn func(A0, A1) R0,
	a0 A0,
	a1 A1,
) R0

func DoA2R2

func DoA2R2[A0, A1, R0, R1 any](
	ctx context.Context,
	m Doer,
	fn func(A0, A1) (R0, R1),
	a0 A0,
	a1 A1,
) (R0, R1)

func DoA2R3

func DoA2R3[A0, A1, R0, R1, R2 any](
	ctx context.Context,
	m Doer,
	fn func(A0, A1) (R0, R1, R2),
	a0 A0,
	a1 A1,
) (R0, R1, R2)

func DoA3

func DoA3[A0, A1, A2 any](
	ctx context.Context,
	m Doer,
	fn func(A0, A1, A2),
	a0 A0,
	a1 A1,
	a2 A2,
)

func DoA3R1

func DoA3R1[A0, A1, A2, R0 any](
	ctx context.Context,
	m Doer,
	fn func(A0, A1, A2) R0,
	a0 A0,
	a1 A1,
	a2 A2,
) R0

func DoA3R2

func DoA3R2[A0, A1, A2, R0, R1 any](
	ctx context.Context,
	m Doer,
	fn func(A0, A1, A2) (R0, R1),
	a0 A0,
	a1 A1,
	a2 A2,
) (R0, R1)

func DoA4R1

func DoA4R1[A0, A1, A2, A3, R0 any](
	ctx context.Context,
	m Doer,
	fn func(A0, A1, A2, A3) R0,
	a0 A0,
	a1 A1,
	a2 A2,
	a3 A3,
) R0

func DoR1

func DoR1[R0 any](
	ctx context.Context,
	m Doer,
	fn func() R0,
) R0

func DoR2

func DoR2[R0, R1 any](
	ctx context.Context,
	m Doer,
	fn func() (R0, R1),
) (R0, R1)

func DoR3

func DoR3[R0, R1, R2 any](
	ctx context.Context,
	m Doer,
	fn func() (R0, R1, R2),
) (R0, R1, R2)

func DoR4

func DoR4[R0, R1, R2, R3 any](
	ctx context.Context,
	m Doer,
	fn func() (R0, R1, R2, R3),
) (R0, R1, R2, R3)

func IsAllowUnlockNotLocked

func IsAllowUnlockNotLocked(ctx context.Context) bool

func IsDeadlockDetectorEnabled

func IsDeadlockDetectorEnabled(ctx context.Context) bool

func IsEnableDeadlock

func IsEnableDeadlock(ctx context.Context) bool

IsEnableDeadlock returns whether the deadlock detector is enabled.

DEPRECATED: use IsDeadlockDetectorEnabled

func IsLoggingEnabled

func IsLoggingEnabled(ctx context.Context) bool

func IsNoLogging

func IsNoLogging(ctx context.Context) bool

IsNoLogging returns whether logging is disabled. DEPRECATED: use IsLoggingEnabled

func RDoA1

func RDoA1[A0 any](
	ctx context.Context,
	m RDoer,
	fn func(A0),
	a0 A0,
)

func RDoA1R1

func RDoA1R1[A0, R0 any](
	ctx context.Context,
	m RDoer,
	fn func(A0) R0,
	a0 A0,
) R0

func RDoA1R2

func RDoA1R2[A0, R0, R1 any](
	ctx context.Context,
	m RDoer,
	fn func(A0) (R0, R1),
	a0 A0,
) (R0, R1)

func RDoA1R4

func RDoA1R4[A0, R0, R1, R2, R3 any](
	ctx context.Context,
	m RDoer,
	fn func(A0) (R0, R1, R2, R3),
	a0 A0,
) (R0, R1, R2, R3)

func RDoA2

func RDoA2[A0, A1 any](
	ctx context.Context,
	m RDoer,
	fn func(A0, A1),
	a0 A0,
	a1 A1,
)

func RDoA2R1

func RDoA2R1[A0, A1, R0 any](
	ctx context.Context,
	m RDoer,
	fn func(A0, A1) R0,
	a0 A0,
	a1 A1,
) R0

func RDoA2R2

func RDoA2R2[A0, A1, R0, R1 any](
	ctx context.Context,
	m RDoer,
	fn func(A0, A1) (R0, R1),
	a0 A0,
	a1 A1,
) (R0, R1)

func RDoA2R3

func RDoA2R3[A0, A1, R0, R1, R2 any](
	ctx context.Context,
	m RDoer,
	fn func(A0, A1) (R0, R1, R2),
	a0 A0,
	a1 A1,
) (R0, R1, R2)

func RDoA3R1

func RDoA3R1[A0, A1, A2, R0 any](
	ctx context.Context,
	m RDoer,
	fn func(A0, A1, A2) R0,
	a0 A0,
	a1 A1,
	a2 A2,
) R0

func RDoA4R1

func RDoA4R1[A0, A1, A2, A3, R0 any](
	ctx context.Context,
	m RDoer,
	fn func(A0, A1, A2, A3) R0,
	a0 A0,
	a1 A1,
	a2 A2,
	a3 A3,
) R0

func RDoR1

func RDoR1[R0 any](
	ctx context.Context,
	m RDoer,
	fn func() R0,
) R0

func RDoR2

func RDoR2[R0, R1 any](
	ctx context.Context,
	m RDoer,
	fn func() (R0, R1),
) (R0, R1)

func RDoR3

func RDoR3[R0, R1, R2 any](
	ctx context.Context,
	m RDoer,
	fn func() (R0, R1, R2),
) (R0, R1, R2)

func RDoR4

func RDoR4[R0, R1, R2, R3 any](
	ctx context.Context,
	m RDoer,
	fn func() (R0, R1, R2, R3),
) (R0, R1, R2, R3)

func SetDeadlockDetectorPossiblyEnabled

func SetDeadlockDetectorPossiblyEnabled(v bool)

SetDeadlockDetectorPossiblyEnabled flips the global fast-path gate for IsDeadlockDetectorEnabled. See SetLoggingPossiblyEnabled for rationale; the same auto-flip behaviour applies via WithDeadlockDetectorEnabled.

func SetLoggingPossiblyEnabled

func SetLoggingPossiblyEnabled(v bool)

SetLoggingPossiblyEnabled flips the global fast-path gate for IsLoggingEnabled. The gate auto-flips to true on the first WithLoggingEnabled(ctx, true) / WithNoLogging(ctx, false) call, so this setter is only needed for callers that want to enable logging without constructing a context, or to forcibly reset the gate (e.g. in tests). While the gate is false, per-context overrides via WithLoggingEnabled are ignored and IsLoggingEnabled always returns false without a context.Value chain walk.

func WithAllowUnlockNotLocked

func WithAllowUnlockNotLocked(ctx context.Context, allow bool) context.Context

func WithDeadlockDetectorEnabled

func WithDeadlockDetectorEnabled(ctx context.Context, enableDeadlock bool) context.Context

WithDeadlockDetectorEnabled returns a context whose IsDeadlockDetectorEnabled answer reflects the given enableDeadlock value. As a side effect, calling it with enableDeadlock == true flips the global deadlockDetectorPossiblyEnabled gate on so the per-context override actually takes effect on the hot RWMutex path. The flip is monotonic-on; passing enableDeadlock == false never flips the gate down because other live contexts may still want the detector enabled.

func WithEnableDeadlock

func WithEnableDeadlock(ctx context.Context, enableDeadlock bool) context.Context

WithEnableDeadlock sets whether the deadlock detector is enabled. Like WithDeadlockDetectorEnabled, calling it with enableDeadlock == true flips the global deadlockDetectorPossiblyEnabled gate on (monotonic-on).

DEPRECATED: use WithDeadlockDetectorEnabled

func WithLoggingEnabled

func WithLoggingEnabled(ctx context.Context, logging bool) context.Context

WithLoggingEnabled returns a context whose IsLoggingEnabled / IsNoLogging answer reflects the given logging value. As a side effect, calling it with logging == true flips the global loggingPossiblyEnabled gate on so the per-context override actually takes effect on the hot RWMutex path. The flip is monotonic-on; passing logging == false never flips the gate down because other live contexts may still want logging enabled.

func WithNoLogging

func WithNoLogging(ctx context.Context, noLogging bool) context.Context

WithNoLogging sets whether logging is disabled. As a side effect, calling it with noLogging == false flips the global loggingPossiblyEnabled gate on so the per-context override actually takes effect. The flip is monotonic-on; passing noLogging == true never flips the gate down.

DEPRECATED: use WithLoggingEnabled

Types

type CtxKeyAllowUnlockNotLocked

type CtxKeyAllowUnlockNotLocked struct{}

type CtxKeyEnableDeadlock

type CtxKeyEnableDeadlock struct{}

type CtxKeyLogging

type CtxKeyLogging struct{}

type CtxLocker

type CtxLocker chan struct{}

CtxLocker is a channel-based synchronization primitive that supports context-aware locking. TODO: move to a separate package

func (CtxLocker) Do

func (l CtxLocker) Do(
	ctx context.Context,
	fn func(),
)

func (CtxLocker) ManualLock

func (l CtxLocker) ManualLock(ctx context.Context) bool

func (CtxLocker) ManualTryLock

func (l CtxLocker) ManualTryLock(ctx context.Context) bool

func (CtxLocker) ManualUnlock

func (l CtxLocker) ManualUnlock(ctx context.Context)

type Doer

type Doer interface {
	Do(context.Context, func())
}

type Gorex

type Gorex struct {
	gorex.Mutex
}

func (*Gorex) Do

func (g *Gorex) Do(ctx context.Context, callback func())

type Map

type Map[K comparable, V any] struct {
	sync.Map
}

func (*Map[K, V]) CompareAndDelete

func (m *Map[K, V]) CompareAndDelete(key K, old V) (deleted bool)

func (*Map[K, V]) CompareAndSwap

func (m *Map[K, V]) CompareAndSwap(key K, old, new V) (swapped bool)

func (*Map[K, V]) Delete

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

func (*Map[K, V]) Load

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

func (*Map[K, V]) LoadAndDelete

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

func (*Map[K, V]) LoadOrStore

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

func (*Map[K, V]) Range

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

func (*Map[K, V]) Store

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

func (*Map[K, V]) Swap

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

type Mutex

type Mutex = RWMutex

func (*Mutex) Do

func (m *Mutex) Do(
	ctx context.Context,
	fn func(),
)

func (*Mutex) ManualLock

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

func (*Mutex) ManualRLock

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

func (*Mutex) ManualRUnlock

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

func (*Mutex) ManualTryLock

func (m *Mutex) ManualTryLock(ctx context.Context) bool

func (*Mutex) ManualTryRLock

func (m *Mutex) ManualTryRLock(ctx context.Context) bool

func (*Mutex) ManualUnlock

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

func (*Mutex) RDo

func (m *Mutex) RDo(
	ctx context.Context,
	fn func(),
)

func (*Mutex) UDo

func (m *Mutex) UDo(
	ctx context.Context,
	fn func(),
)

func (*Mutex) URDo

func (m *Mutex) URDo(
	ctx context.Context,
	fn func(),
)

type Pool

type Pool[T any] struct {
	sync.Pool
	ResetFunc func(*T)
}

func NewPool

func NewPool[T any](
	allocFunc func() *T,
	resetFunc func(*T),
	freeFunc func(*T),
) *Pool[T]

func (*Pool[T]) Get

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

func (*Pool[T]) Put

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

type RDoer

type RDoer interface {
	RDo(context.Context, func())
}

type RWMutex

type RWMutex struct {
	OverrideTimeout time.Duration
	PanicOnDeadlock *bool
	// contains filtered or unexported fields
}

type WithMutex

type WithMutex[T any] struct {
	RWMutex
	Value T
}

Jump to

Keyboard shortcuts

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