zstdzgo.at/zstd/zsync Index | Files

package zsync

import "zgo.at/zstd/zsync"

Package zsync adds functions for synchronization.

Index

Functions

func Wait

func Wait(ctx context.Context, wg *sync.WaitGroup) error

Wait for a sync.WaitGroup with support for timeout/cancellations from context.

func WithLock

func WithLock(mu *sync.Mutex, f func())

WithLock locks the passed mutex, runs the function, and unlocks.

WithLock(mu, func() {
    // .. stuff ..
})

This is convenient especially in cases where you don't want to defer the Unlock(), but also want to ensure the Unlock() is always called, regardless of runtime errors.

Types

type AtMost

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

AtMost runs at most a certain number of goroutines in parallel.

func NewAtMost

func NewAtMost(max int) AtMost

NewAtMost creates a new AtMost instance.

func (*AtMost) Run

func (a *AtMost) Run(f func())

Run a function. Blocks if the job queue is full.

func (*AtMost) Wait

func (a *AtMost) Wait()

Wait for all jobs to finish.

type AtomicInt

type AtomicInt int32

AtomicInt uses sync/atomic to store and read the value of an int32.

func NewAtomicInt

func NewAtomicInt(value int32) *AtomicInt

NewAtomicInt creates an new AtomicInt.

func (*AtomicInt) Add

func (i *AtomicInt) Add(n int32) int32

func (*AtomicInt) Set

func (i *AtomicInt) Set(value int32)

func (*AtomicInt) Value

func (i *AtomicInt) Value() int32

type AtomicInt64

type AtomicInt64 int64

AtomicInt64 uses sync/atomic to store and read the value of an int64.

func NewAtomicInt64

func NewAtomicInt64(value int64) *AtomicInt64

NewAtomicInt creates an new AtomicInt.

func (*AtomicInt64) Add

func (i *AtomicInt64) Add(n int64) int64

func (*AtomicInt64) Set

func (i *AtomicInt64) Set(value int64)

func (*AtomicInt64) Value

func (i *AtomicInt64) Value() int64

type Buffer

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

Buffer is a wrapper around bytes.Buffer which protects every operation with a lock, ensuring it can be read/write in a thread-safe manner.

func NewBuffer

func NewBuffer(buf []byte) *Buffer

func NewBufferString

func NewBufferString(s string) *Buffer

func (Buffer) Bytes

func (b Buffer) Bytes() []byte

func (*Buffer) Cap

func (b *Buffer) Cap() int

func (*Buffer) Grow

func (b *Buffer) Grow(n int)

func (*Buffer) Len

func (b *Buffer) Len() int

func (*Buffer) Next

func (b *Buffer) Next(n int) []byte

func (*Buffer) Read

func (b *Buffer) Read(p []byte) (int, error)

func (*Buffer) ReadByte

func (b *Buffer) ReadByte() (byte, error)

func (*Buffer) ReadBytes

func (b *Buffer) ReadBytes(delim byte) (line []byte, err error)

func (*Buffer) ReadFrom

func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error)

func (*Buffer) ReadRune

func (b *Buffer) ReadRune() (r rune, size int, err error)

func (*Buffer) ReadString

func (b *Buffer) ReadString(delim byte) (line string, err error)

func (*Buffer) Reset

func (b *Buffer) Reset()

func (Buffer) String

func (b Buffer) String() string

func (*Buffer) Truncate

func (b *Buffer) Truncate(n int)

func (*Buffer) UnreadByte

func (b *Buffer) UnreadByte() error

func (*Buffer) UnreadRune

func (b *Buffer) UnreadRune() error

func (*Buffer) Write

func (b *Buffer) Write(p []byte) (int, error)

func (*Buffer) WriteByte

func (b *Buffer) WriteByte(c byte) error

func (*Buffer) WriteRune

func (b *Buffer) WriteRune(r rune) (n int, err error)

func (*Buffer) WriteString

func (b *Buffer) WriteString(s string) (n int, err error)

func (*Buffer) WriteTo

func (b *Buffer) WriteTo(w io.Writer) (n int64, err error)

type LogMutex

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

LogMutex is like sync.Mutex, but will log a message to stderr on Lock() and Unlock().

This can be a simple but effective way to debug locking issues.

func (*LogMutex) Lock

func (d *LogMutex) Lock()

func (*LogMutex) Unlock

func (d *LogMutex) Unlock()

type LogRWMutex

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

LogRWMutex is like sync.RWMutex, but will log a message to stderr on Lock(), Unlock(), RLock(), and RUnlock().

This can be a simple but effective way to debug locking issues.

func (*LogRWMutex) Lock

func (d *LogRWMutex) Lock()

func (*LogRWMutex) RLock

func (d *LogRWMutex) RLock()

func (*LogRWMutex) RUnlock

func (d *LogRWMutex) RUnlock()

func (*LogRWMutex) Unlock

func (d *LogRWMutex) Unlock()

type Once

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

Once is an object that will perform exactly one action per key.

This is mix between sync.Once and x/sync/singleflight; like Once, a function is only run once, and like singleflight it allows grouping per-key and has a return value informing whether the function is already run.

This implementation is a bit slower than the stdlib one; the benchmark regresses from ~1.6ns/op to ~52ns/op on my system.

func (*Once) Did

func (o *Once) Did(key string) bool

Did reports if something has been run for the given key.

func (*Once) Do

func (o *Once) Do(key string, f func()) bool

Do calls the function f for the given key only on the first invocation.

In other words, given:

var once Once

If once.Do("x", f) is called multiple times, only the first call will invoke f, even if f has a different value in each invocation. A new key or instance of Once is required for each function to execute.

The return value tells you if f is run; it's true on the first caller, and false on all subsequent calls.

It may be necessary to use a function literal to capture the arguments to a function to be invoked by Do:

config.once.Do(func() { config.init(filename) })

Because no call to Do returns until the one call to f returns, if f causes Do to be called, it will deadlock.

If f panics, Do considers it to have returned; future calls of Do return without calling f.

func (*Once) Forget

func (o *Once) Forget(key string)

Forget about a key, causing the next invocation to Do() to run again.

type Synced

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

Synced provides a thread-safe synced variable.

func NewSynced

func NewSynced[T any](val T) Synced[T]

NewSynced creates a new Synced with the initial value set to val.

func (*Synced[T]) Get

func (s *Synced[T]) Get() T

Get the value.

func (*Synced[T]) Set

func (s *Synced[T]) Set(to T)

Set the value.

func (Synced[T]) String

func (s Synced[T]) String() string

Source Files

buf.go mutex.go once.go synced.go zsync.go

Version
v0.0.0-20240930202209-a63c3335042a (latest)
Published
Sep 30, 2024
Platform
linux/amd64
Imports
8 packages
Last checked
2 days ago

Tools for package owners.