syncutil

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrExited = errors.New("work exited without returning")

Error of bounded work whose goroutine ended through runtime.Goexit instead of returning or panicking

View Source
var ErrOverRelease = errors.New("semaphore released more often than acquired")

Error of a Release that finds every slot of the semaphore already free

Functions

func GoroutineID

func GoroutineID() uint64

Number the runtime gave the calling goroutine, never 0 and never reused while the process lives

func PollUntil

func PollUntil[T any](ctx context.Context, timeout, interval time.Duration, cond func() (T, bool)) T

Calls cond at once and then every interval until it reports done, timeout has passed or ctx ends, giving back the value of its last call; the last wait is cut to the time left

func RunBounded

func RunBounded[T any](ctx context.Context, timeout time.Duration, fn func(ctx context.Context) (T, error)) (value T, timedOut bool, err error)

Runs fn on a goroutine of its own that keeps ctx's values but not its cancellation and waits at most timeout for it; on expiry the goroutine is left to finish unobserved and timedOut is true, when ctx ends first its error comes back, a result ready by then winning over either, a panic in fn comes back as a *PanicError and a runtime.Goexit as ErrExited

Types

type Event

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

Flag goroutines raise and wait on, lowered again by Clear; the zero Event is lowered and ready to use, and an Event must not be copied after first use

func (*Event) Clear

func (e *Event) Clear()

Lowers the flag so later waits block until the next Set

func (*Event) Done

func (e *Event) Done() <-chan struct{}

Channel closed once the flag is up, for a select arm; after a Clear the next call hands out a fresh channel

func (*Event) IsSet

func (e *Event) IsSet() bool

Whether the flag is up

func (*Event) Set

func (e *Event) Set()

Raises the flag and wakes every waiter

func (*Event) Wait

func (e *Event) Wait()

Blocks until the flag is up

func (*Event) WaitContext

func (e *Event) WaitContext(ctx context.Context) error

Blocks until the flag is up or ctx ends, nil once it is up and the context's error otherwise

func (*Event) WaitTimeout

func (e *Event) WaitTimeout(d time.Duration) bool

Blocks until the flag is up or d has passed, reporting whether it is up; a d of zero or less only looks

type GoroutineLocal

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

Value slot every goroutine sees on its own, keyed by GoroutineID; slots of finished goroutines are dropped as the slot count grows, and the zero value is empty and ready to use

func (*GoroutineLocal[T]) Delete

func (l *GoroutineLocal[T]) Delete()

Forgets the calling goroutine's value

func (*GoroutineLocal[T]) Get

func (l *GoroutineLocal[T]) Get() (T, bool)

Value the calling goroutine stored, false when it stored none

func (*GoroutineLocal[T]) Set

func (l *GoroutineLocal[T]) Set(v T)

Stores v for the calling goroutine only

type PanicError

type PanicError struct {
	Value any
	Stack []byte
}

Panic of work that ran on a goroutine of its own, carried back to the waiting caller with the stack it panicked on

func (*PanicError) Error

func (e *PanicError) Error() string

The recovered value after "panicked: "

type ReentrantMutex

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

Mutex the goroutine holding it may lock again, freed for others once every Lock has had its Unlock; the zero value is unlocked and ready to use, and it must not be copied after first use

func (*ReentrantMutex) Lock

func (m *ReentrantMutex) Lock()

Blocks until the mutex is free or already held by the calling goroutine, then takes one more hold

func (*ReentrantMutex) TryLockFor

func (m *ReentrantMutex) TryLockFor(d time.Duration) bool

Takes one more hold like Lock but gives up once d has passed, reporting whether it got the mutex; a d of zero or less only tries

func (*ReentrantMutex) Unlock

func (m *ReentrantMutex) Unlock()

Drops one hold of the calling goroutine, freeing the mutex when no hold is left; panics when the calling goroutine does not hold it

type Semaphore

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

Counting semaphore with a fixed number of slots; the zero value has none

func NewSemaphore

func NewSemaphore(n int) *Semaphore

Semaphore with n free slots, none when n is not positive

func (*Semaphore) Acquire

func (s *Semaphore) Acquire(ctx context.Context) error

Waits for a free slot and takes it, the context's error when ctx ends first

func (*Semaphore) Release

func (s *Semaphore) Release() error

Gives a slot back, ErrOverRelease when no slot is taken

func (*Semaphore) TryAcquire

func (s *Semaphore) TryAcquire() bool

Takes a slot when one is free, without waiting, reporting whether it got one

Jump to

Keyboard shortcuts

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