check-once.go 348 B

12345678910111213141516171819202122232425262728
  1. package util
  2. import "sync"
  3. // CheckOnce struct
  4. type CheckOnce struct {
  5. mutex sync.Mutex
  6. done bool
  7. }
  8. // IsDone func
  9. func (c *CheckOnce) IsDone() bool {
  10. c.mutex.Lock()
  11. defer c.mutex.Unlock()
  12. return c.done
  13. }
  14. // Done func
  15. func (c *CheckOnce) Done(f func()) {
  16. c.mutex.Lock()
  17. defer c.mutex.Unlock()
  18. if !c.done {
  19. f()
  20. c.done = true
  21. }
  22. }