donechan.go 632 B

1234567891011121314151617181920212223242526272829303132
  1. package syncx
  2. import (
  3. "sync"
  4. "git.i2edu.net/i2/go-zero/core/lang"
  5. )
  6. // A DoneChan is used as a channel that can be closed multiple times and wait for done.
  7. type DoneChan struct {
  8. done chan lang.PlaceholderType
  9. once sync.Once
  10. }
  11. // NewDoneChan returns a DoneChan.
  12. func NewDoneChan() *DoneChan {
  13. return &DoneChan{
  14. done: make(chan lang.PlaceholderType),
  15. }
  16. }
  17. // Close closes dc, it's safe to close more than once.
  18. func (dc *DoneChan) Close() {
  19. dc.once.Do(func() {
  20. close(dc.done)
  21. })
  22. }
  23. // Done returns a channel that can be notified on dc closed.
  24. func (dc *DoneChan) Done() chan lang.PlaceholderType {
  25. return dc.done
  26. }