done.go 513 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package signal
  2. import (
  3. "sync"
  4. )
  5. type Done struct {
  6. access sync.Mutex
  7. c chan struct{}
  8. closed bool
  9. }
  10. func NewDone() *Done {
  11. return &Done{
  12. c: make(chan struct{}),
  13. }
  14. }
  15. func (d *Done) Done() bool {
  16. select {
  17. case <-d.c:
  18. return true
  19. default:
  20. return false
  21. }
  22. }
  23. func (d *Done) C() chan struct{} {
  24. return d.c
  25. }
  26. func (d *Done) Wait() {
  27. <-d.c
  28. }
  29. func (d *Done) Close() error {
  30. d.access.Lock()
  31. defer d.access.Unlock()
  32. if d.closed {
  33. return nil
  34. }
  35. d.closed = true
  36. close(d.c)
  37. return nil
  38. }