You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.0 KiB

  1. // +build !deadlock
  2. package sync
  3. import "sync"
  4. // A Mutex is a mutual exclusion lock.
  5. type Mutex struct {
  6. sync.Mutex
  7. }
  8. // An RWMutex is a reader/writer mutual exclusion lock.
  9. type RWMutex struct {
  10. sync.RWMutex
  11. }
  12. // Closer implements a primitive to close a channel that signals process
  13. // termination while allowing a caller to call Close multiple times safely. It
  14. // should be used in cases where guarantees cannot be made about when and how
  15. // many times closure is executed.
  16. type Closer struct {
  17. closeOnce sync.Once
  18. doneCh chan struct{}
  19. }
  20. // NewCloser returns a reference to a new Closer.
  21. func NewCloser() *Closer {
  22. return &Closer{doneCh: make(chan struct{})}
  23. }
  24. // Done returns the internal done channel allowing the caller either block or wait
  25. // for the Closer to be terminated/closed.
  26. func (c *Closer) Done() <-chan struct{} {
  27. return c.doneCh
  28. }
  29. // Close gracefully closes the Closer. A caller should only call Close once, but
  30. // it is safe to call it successive times.
  31. func (c *Closer) Close() {
  32. c.closeOnce.Do(func() {
  33. close(c.doneCh)
  34. })
  35. }