package zsync
import "zgo.at/zstd/zsync"
Package zsync adds functions for synchronization.
Index ¶
- func Wait(ctx context.Context, wg *sync.WaitGroup) error
- func WithLock(mu *sync.Mutex, f func())
- type AtMost
- type AtomicInt
- func NewAtomicInt(value int32) *AtomicInt
- func (i *AtomicInt) Add(n int32) int32
- func (i *AtomicInt) Set(value int32)
- func (i *AtomicInt) Value() int32
- type AtomicInt64
- func NewAtomicInt64(value int64) *AtomicInt64
- func (i *AtomicInt64) Add(n int64) int64
- func (i *AtomicInt64) Set(value int64)
- func (i *AtomicInt64) Value() int64
- type Buffer
- func NewBuffer(buf []byte) *Buffer
- func NewBufferString(s string) *Buffer
- func (b Buffer) Bytes() []byte
- func (b *Buffer) Cap() int
- func (b *Buffer) Grow(n int)
- func (b *Buffer) Len() int
- func (b *Buffer) Next(n int) []byte
- func (b *Buffer) Read(p []byte) (int, error)
- func (b *Buffer) ReadByte() (byte, error)
- func (b *Buffer) ReadBytes(delim byte) (line []byte, err error)
- func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error)
- func (b *Buffer) ReadRune() (r rune, size int, err error)
- func (b *Buffer) ReadString(delim byte) (line string, err error)
- func (b *Buffer) Reset()
- func (b Buffer) String() string
- func (b *Buffer) Truncate(n int)
- func (b *Buffer) UnreadByte() error
- func (b *Buffer) UnreadRune() error
- func (b *Buffer) Write(p []byte) (int, error)
- func (b *Buffer) WriteByte(c byte) error
- func (b *Buffer) WriteRune(r rune) (n int, err error)
- func (b *Buffer) WriteString(s string) (n int, err error)
- func (b *Buffer) WriteTo(w io.Writer) (n int64, err error)
- type LogMutex
- type LogRWMutex
- func (d *LogRWMutex) Lock()
- func (d *LogRWMutex) RLock()
- func (d *LogRWMutex) RUnlock()
- func (d *LogRWMutex) Unlock()
- type Once
- func (o *Once) Did(key string) bool
- func (o *Once) Do(key string, f func()) bool
- func (o *Once) Forget(key string)
- type Synced
Functions ¶
func Wait ¶
Wait for a sync.WaitGroup with support for timeout/cancellations from context.
func WithLock ¶
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 ¶
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 ¶
NewAtomicInt creates an new AtomicInt.
func (*AtomicInt) Add ¶
func (*AtomicInt) Set ¶
func (*AtomicInt) Value ¶
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 NewBufferString ¶
func (Buffer) Bytes ¶
func (*Buffer) Cap ¶
func (*Buffer) Grow ¶
func (*Buffer) Len ¶
func (*Buffer) Next ¶
func (*Buffer) Read ¶
func (*Buffer) ReadByte ¶
func (*Buffer) ReadBytes ¶
func (*Buffer) ReadFrom ¶
func (*Buffer) ReadRune ¶
func (*Buffer) ReadString ¶
func (*Buffer) Reset ¶
func (b *Buffer) Reset()
func (Buffer) String ¶
func (*Buffer) Truncate ¶
func (*Buffer) UnreadByte ¶
func (*Buffer) UnreadRune ¶
func (*Buffer) Write ¶
func (*Buffer) WriteByte ¶
func (*Buffer) WriteRune ¶
func (*Buffer) WriteString ¶
func (*Buffer) WriteTo ¶
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 ¶
Did reports if something has been run for the given key.
func (*Once) Do ¶
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 ¶
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 ¶
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 ¶
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.