sync

package
v0.0.0-...-fb9d71b Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2022 License: Apache-2.0 Imports: 7 Imported by: 19

Documentation

Overview

Package sync contains a mutex that can be used in a select statement.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsAlreadyClosedError

func IsAlreadyClosedError(err error) bool

IsAlreadyClosedError checks whether an error is an AlreadyClosedError.

Types

type Closer

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

Closer is a utility type for implementing an "onclose" event. It supports registering handlers, waiting for the event, and status checking. A default-initialised Closer is a valid value.

func (*Closer) Close

func (c *Closer) Close() error

Close executes all registered callbacks. If Close was already called before, returns an AlreadyClosedError, otherwise, returns nil.

func (*Closer) Closed

func (c *Closer) Closed() <-chan struct{}

Closed returns a channel to be used in a select statement.

func (*Closer) Ctx

func (c *Closer) Ctx() context.Context

Ctx returns a context that is canceled when the Closer is closed.

func (*Closer) IsClosed

func (c *Closer) IsClosed() bool

IsClosed returns whether the Closer is currently closed.

func (*Closer) OnClose

func (c *Closer) OnClose(handler func()) bool

OnClose registers the passed callback to be executed when the Closer is closed. If the closer is already closed, the callback will be ignored.

Returns true if the callback was successfully registered and false if the closer was already closed and callback was ignored.

func (*Closer) OnCloseAlways

func (c *Closer) OnCloseAlways(handler func()) bool

OnCloseAlways registers the passed callback to be executed when the Closer is closed. If the closer is already closed, the callback will be executed immediately.

Returns true if the callback was successfully registered and false if the closer was already closed and callback was executed immediately.

type Mutex

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

Mutex is a replacement of the standard mutex type. It supports the additional TryLock() function, as well as a variant that can be used in a select statement.

func (*Mutex) Lock

func (m *Mutex) Lock()

Lock blockingly locks the mutex.

func (*Mutex) TryLock

func (m *Mutex) TryLock() bool

TryLock tries to lock the mutex without blocking. Returns whether the mutex was acquired.

func (*Mutex) TryLockCtx

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

TryLockCtx tries to lock the mutex within a timeout provided by a context. For an instant timeout, a nil context has to be passed. Returns whether the mutex was acquired.

func (*Mutex) Unlock

func (m *Mutex) Unlock()

Unlock unlocks the mutex. If the mutex was not locked, panics.

type OnCloser

type OnCloser interface {
	OnClose(func()) bool
	OnCloseAlways(func()) bool
}

OnCloser contains the OnClose and OnCloseAlways function.

type Signal

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

Signal is a lightweight reusable signal that can be waited on. It can be used to notify coroutines. Coroutines waiting for the signal will be notified once it is triggered, but coroutines starting to wait after triggering will not be notified by the same operation. This means it can be used to repeatably notify coroutines.

func NewSignal

func NewSignal() *Signal

NewSignal creates a new signal.

func (*Signal) Broadcast

func (s *Signal) Broadcast()

Broadcast wakes up all waiting coroutines.

func (*Signal) Done

func (s *Signal) Done() <-chan struct{}

Done returns a channel that will be written to when the signal is next notified. After reading from the returned channel once, the channel should be discarded, and a new channel should be retrieved via a new call to Done.

func (*Signal) Signal

func (s *Signal) Signal()

Signal wakes up a single coroutine, if any are waiting.

func (*Signal) Wait

func (s *Signal) Wait()

Wait waits until the coroutine is woken up by the signal.

func (*Signal) WaitCtx

func (s *Signal) WaitCtx(ctx context.Context) bool

WaitCtx waits until the context expires or the coroutine is woken up by the signal. Returns whether the coroutine was woken up by the signal.

type WaitGroup

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

WaitGroup is an analog to sync.WaitGroup but also features WaitCh, which exposes a channel that is closed once the wait group is fulfilled, and WaitCtx, which allows waiting until either the wait group is fulfilled or the provided context expires.

func (*WaitGroup) Add

func (wg *WaitGroup) Add(n int)

Add adds n waiting elements.

func (*WaitGroup) Done

func (wg *WaitGroup) Done()

Done decrements the wait counter by one.

func (*WaitGroup) Wait

func (wg *WaitGroup) Wait()

Wait waits until the wait group is fulfilled.

func (*WaitGroup) WaitCh

func (wg *WaitGroup) WaitCh() <-chan struct{}

WaitCh returns a channel that will be closed as soon as the wait group is fulfilled.

func (*WaitGroup) WaitCtx

func (wg *WaitGroup) WaitCtx(ctx context.Context) bool

WaitCtx waits until the wait group is fulfilled or the context expires. Returns whether the wait group was fulfilled.

Directories

Path Synopsis
Package atomic contains extensions of "sync/atomic"
Package atomic contains extensions of "sync/atomic"

Jump to

Keyboard shortcuts

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