latch.go 577 B

123456789101112131415161718192021222324252627282930313233
  1. package asyncbuffer
  2. import (
  3. "sync"
  4. )
  5. // Latch is once-releasing semaphore.
  6. type Latch struct {
  7. _ noCopy
  8. once sync.Once
  9. done chan struct{}
  10. }
  11. // NewLatch creates a new Latch.
  12. func NewLatch() *Latch {
  13. return &Latch{done: make(chan struct{})}
  14. }
  15. // Release releases the latch, allowing all waiting goroutines to proceed.
  16. func (g *Latch) Release() {
  17. g.once.Do(func() { close(g.done) })
  18. }
  19. // Wait blocks until the latch is released.
  20. func (g *Latch) Wait() {
  21. <-g.done
  22. }
  23. // checked by 'go vet
  24. type noCopy struct{}
  25. func (*noCopy) Lock() {}
  26. func (*noCopy) Unlock() {}