syncutil

package
v0.0.2-0...-993051b Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const (
	VersionedListenAtEarliest versionedListenAt = -1
	VersionedListenAtLatest   versionedListenAt = -2
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AsyncTaskNotifier

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

AsyncTaskNotifier is a notifier for async task.

func NewAsyncTaskNotifier

func NewAsyncTaskNotifier[T any]() *AsyncTaskNotifier[T]

NewAsyncTaskNotifier creates a new async task notifier.

func (*AsyncTaskNotifier[T]) BlockAndGetResult

func (n *AsyncTaskNotifier[T]) BlockAndGetResult() T

BlockAndGetResult returns the result of the async task.

func (*AsyncTaskNotifier[T]) BlockUntilFinish

func (n *AsyncTaskNotifier[T]) BlockUntilFinish()

BlockUntilFinish blocks until the async task is finished.

func (*AsyncTaskNotifier[T]) Cancel

func (n *AsyncTaskNotifier[T]) Cancel()

Cancel cancels the async task, the async task can receive the cancel signal from Context.

func (*AsyncTaskNotifier[T]) Context

func (n *AsyncTaskNotifier[T]) Context() context.Context

Context returns the context of the async task.

func (*AsyncTaskNotifier[T]) Finish

func (n *AsyncTaskNotifier[T]) Finish(result T)

Finish finishes the async task with a result.

func (*AsyncTaskNotifier[T]) FinishChan

func (n *AsyncTaskNotifier[T]) FinishChan() <-chan struct{}

FinishChan returns a channel that will be closed when the async task is finished.

type ClosableLock

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

func (*ClosableLock) Close

func (l *ClosableLock) Close()

func (*ClosableLock) LockIfNotClosed

func (l *ClosableLock) LockIfNotClosed() bool

func (*ClosableLock) Unlock

func (l *ClosableLock) Unlock()

type ContextCond

type ContextCond struct {
	L sync.Locker
	// contains filtered or unexported fields
}

ContextCond is a condition variable implementation that can be used with context.

func NewContextCond

func NewContextCond(l sync.Locker) *ContextCond

NewContextCond creates a new condition variable that can be used with context. Broadcast is implemented using a channel, so the performance may not be as good as sync.Cond.

func (*ContextCond) LockAndBroadcast

func (cv *ContextCond) LockAndBroadcast()

LockAndBroadcast locks the underlying locker and performs a broadcast. It notifies all goroutines waiting on the condition variable.

c.LockAndBroadcast()
... make some change ...
c.L.Unlock()

func (*ContextCond) UnsafeBroadcast

func (cv *ContextCond) UnsafeBroadcast()

UnsafeBroadcast performs a broadcast without locking. !!! Must be called with the lock held !!!

func (*ContextCond) Wait

func (cv *ContextCond) Wait(ctx context.Context) error

Wait waits for a broadcast or context timeout. It blocks until either a broadcast is received or the context is canceled or times out. Returns an error if the context is canceled or times out.

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
c.L.Lock()
for !condition() {
    if err := c.Wait(ctx); err != nil {
           return err
       }
   }
... make use of condition ...
c.L.Unlock()

func (*ContextCond) WaitChan

func (cv *ContextCond) WaitChan() <-chan struct{}

WaitChan returns a channel that can be used to wait for a broadcast. Should be called after Lock. The channel is closed when a broadcast is received.

type Future

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

Future is a future value that can be set and retrieved.

func NewFuture

func NewFuture[T any]() *Future[T]

NewFuture creates a new future.

func (*Future[T]) Done

func (f *Future[T]) Done() <-chan struct{}

Done returns a channel that is closed when the future is set.

func (*Future[T]) Get

func (f *Future[T]) Get() T

Get retrieves the value of the future if set, otherwise block until set.

func (*Future[T]) Ready

func (f *Future[T]) Ready() bool

Ready returns true if the future is set.

func (*Future[T]) Set

func (f *Future[T]) Set(value T)

Set sets the value of the future.

type VersionedListener

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

VersionedListener is a listener with version.

func (*VersionedListener) Sync

func (vl *VersionedListener) Sync()

Sync syncs the listener to the latest version.

func (*VersionedListener) Wait

func (vl *VersionedListener) Wait(ctx context.Context) error

Wait waits for the next notification. If the context is canceled, it returns the error. Otherwise it will block until the next notification.

func (*VersionedListener) WaitChan

func (vl *VersionedListener) WaitChan() <-chan struct{}

WaitChan returns a channel that will be closed when the next notification comes. Use Sync to sync the listener to the latest version to avoid redundant notify.

ch := vl.WaitChan()
<-ch
vl.Sync()
... make use of the notification ...

type VersionedNotifier

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

VersionedNotifier is a notifier with version. A version-based notifier, any change of version could be seen by all listeners without lost.

func NewVersionedNotifier

func NewVersionedNotifier() *VersionedNotifier

NewVersionedNotifier creates a new VersionedNotifier.

func (*VersionedNotifier) Listen

func (vn *VersionedNotifier) Listen(at versionedListenAt) *VersionedListener

Listen creates a listener at given position.

func (*VersionedNotifier) NotifyAll

func (vn *VersionedNotifier) NotifyAll()

NotifyAll notifies all listeners.

Jump to

Keyboard shortcuts

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