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 (Lock) LockContext ¶
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 ... }