syncutil

package
v2.17.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2026 License: GPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package syncutil provides mutex primitives with optional deadlock detection. Use build tag -tags=deadlock to enable deadlock detection during development.

Index

Constants

View Source
const DeadlockEnabled = false

DeadlockEnabled is true if the deadlock detector is enabled.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mutex

type Mutex struct {
	sync.Mutex //nolint:forbidigo // this package wraps sync.Mutex
}

A Mutex is a mutual exclusion lock.

type Pauser added in v2.11.0

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

Pauser is a thread-safe pause/throttle/resume primitive using the closed-channel pattern. When running, Wait returns immediately. When paused, Wait blocks until Resume is called or the context is cancelled. When throttled, Wait enforces a duty cycle: callers run unimpeded for a work quantum, then Wait sleeps for a sleep quantum before the next window.

A nil *Pauser is safe to use: Wait always returns nil.

func NewPauser added in v2.11.0

func NewPauser() *Pauser

NewPauser returns a Pauser in the running state.

func (*Pauser) HasBaselineThrottle

func (p *Pauser) HasBaselineThrottle() bool

HasBaselineThrottle reports whether running work uses baseline pacing.

func (*Pauser) IsPaused added in v2.11.0

func (p *Pauser) IsPaused() bool

IsPaused reports whether the Pauser is currently in the paused state. A nil receiver reports false.

func (*Pauser) IsThrottled added in v2.16.0

func (p *Pauser) IsThrottled() bool

IsThrottled reports whether the Pauser is currently in the throttled state. A nil receiver reports false.

func (*Pauser) Level added in v2.16.0

func (p *Pauser) Level() ThrottleLevel

Level returns the throttle level applied by the most recent Throttle call. Only meaningful while IsThrottled is true. A nil receiver returns ThrottleLight.

func (*Pauser) Pause added in v2.11.0

func (p *Pauser) Pause()

Pause requests a full pause. Idempotent: calling Pause when already paused is a no-op. Pause overrides a throttled state.

func (*Pauser) Resume added in v2.11.0

func (p *Pauser) Resume()

Resume returns to the running state, unblocking all goroutines waiting in Wait. Idempotent: calling Resume when running is a no-op.

func (*Pauser) SetBaselineThrottle

func (p *Pauser) SetBaselineThrottle(level ThrottleLevel)

SetBaselineThrottle enables a duty cycle while the pauser is otherwise in its running state. Explicit Pause and Throttle calls still override it, and Resume returns to this baseline. Baseline pacing does not make IsThrottled true, so status reporting and transaction sizing continue to describe only foreground-media restrictions.

func (*Pauser) SetBaselineThrottleQuanta

func (p *Pauser) SetBaselineThrottleQuanta(work, sleep time.Duration)

SetBaselineThrottleQuanta overrides the running-state baseline duty cycle. Non-positive values are ignored. Intended for configuration and tests.

func (*Pauser) SetThrottleQuanta added in v2.16.0

func (p *Pauser) SetThrottleQuanta(work, sleep time.Duration)

SetThrottleQuanta overrides the explicit throttle duty cycle. Non-positive values are ignored. Intended for configuration and tests.

func (*Pauser) Throttle added in v2.16.0

func (p *Pauser) Throttle(level ThrottleLevel)

Throttle requests the duty-cycled throttled state at the given level. Idempotent: calling Throttle with the level already active is a no-op and does not reset the current work window. Calling Throttle with a different level while already throttled switches the duty cycle immediately. Calling Throttle when paused releases blocked waiters into the throttled state.

func (*Pauser) Wait added in v2.11.0

func (p *Pauser) Wait(ctx context.Context) error

Wait applies the current state to the caller. Running work returns immediately unless a baseline throttle is enabled, paused work blocks until Resume, and explicitly throttled work follows its stronger duty cycle. It returns the context error if the context is cancelled while blocked or sleeping. A nil receiver returns nil.

func (*Pauser) WaitForPacing

func (p *Pauser) WaitForPacing(ctx context.Context) error

WaitForPacing applies baseline and explicit throttle pacing without blocking for a full pause. Use it while a transaction or other resource must be released before the caller can safely honor Pause with Wait.

type RWMutex

type RWMutex struct {
	sync.RWMutex //nolint:forbidigo // this package wraps sync.RWMutex
}

An RWMutex is a reader/writer mutual exclusion lock.

type ThrottleLevel added in v2.16.0

type ThrottleLevel int

ThrottleLevel selects how aggressively the throttled state's duty cycle slows work down.

const (
	// ThrottleLight is the default while ordinary foreground media is
	// active. 50ms/150ms yields ~25% duty, keeping storage and CPU mostly
	// free for the foreground consumer while still making steady progress.
	ThrottleLight ThrottleLevel = iota
	// ThrottleHeavy is for storage-streaming cores (CD-based, etc.) whose
	// continuous reads are sensitive to any competing I/O. 20ms/300ms
	// yields ~6% duty. On-device testing (MiSTer, CD-streaming arcade
	// core) showed the lighter duty cycle still let indexing's storage
	// bursts interfere with playback, so the work window is short and
	// infrequent enough to stay out of a foreground consumer's way.
	ThrottleHeavy
	// ThrottleBackground is a baseline for long-running jobs even when no
	// media is active. Equal work and sleep quanta reserve regular CPU time
	// for UI, API, reader, and audio work without changing process priority.
	ThrottleBackground
)

Jump to

Keyboard shortcuts

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