lock

package
v0.0.0-...-32c7374 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lock

type Lock chan struct{}

Lock is a channel-based lock, which be used directly as a channel, or via the Lock/Unlock methods. To lock a lock receive an empty struct from it, to unlock a lock send an empty struct to it.

Example:

lock := async.NewLock()
select {
case <-lock:
case <-cancel:
	return status.Cancelled
}
defer lock.Unlock()

func NewLock

func NewLock() Lock

NewLock returns a new unlocked lock.

func (Lock) Lock

func (l Lock) Lock()

Lock locks the lock.

func (Lock) LockContext

func (l Lock) LockContext(ctx context.Context) status.Status

LockContext awaits and locks the lock, or awaits the context cancellation.

func (Lock) Unlock

func (l Lock) Unlock()

Unlock unlocks the lock, or panics if the lock is already unlocked.

func (Lock) UnlockIfLocked

func (l Lock) UnlockIfLocked()

UnlockIfLocked unlocks the lock if it is locked, otherwise does nothing.

type WaitLock

type WaitLock interface {
	// Lock returns a channel receiving from which locks the lock.
	Lock() <-chan struct{}

	// Unlock unlocks the lock and notifies all waiters.
	Unlock()

	// Wait returns a channel which is closed when the lock is unlocked.
	Wait() <-chan struct{}
}

WaitLock is a lock which allows others to wait until it is unlocked.

WaitLock does not guarantee that the lock is not acquired by another writer after its waiters are notified.

WaitLock can be used, for example, to execute a single operation by one writer, while other writers wait until the operation is completed.

Example:

lock := async.NewWaitLock()

func flush(cancel <-chan struct{}) {
	select {
	case <-lock.Lock():
		// Acquired lock
	default:
		// Await flushing end
		select {
		case <-lock.Wait():
		case <-cancel:
			return status.Cancelled
		}
	}
	defer lock.Unlock()

	// ... Do work ...
}

func NewWaitLock

func NewWaitLock() WaitLock

NewWaitLock returns a new unlocked lock.

Jump to

Keyboard shortcuts

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