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.

45 lines
1.1 KiB

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