1
0

latch.go 465 B

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