utils.go 857 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package mhayaTimeWheel
  2. import (
  3. "sync"
  4. "sync/atomic"
  5. "time"
  6. )
  7. // truncate returns the result of rounding x toward zero to a multiple of m.
  8. // If m <= 0, Truncate returns x unchanged.
  9. func truncate(x, m int64) int64 {
  10. if m <= 0 {
  11. return x
  12. }
  13. return x - x%m
  14. }
  15. // TimeToMS returns an integer number, which represents t in milliseconds.
  16. func TimeToMS(t time.Time) int64 {
  17. return t.UnixNano() / int64(time.Millisecond)
  18. }
  19. // MSToTime returns the UTC time corresponding to the given Unix time,
  20. // t milliseconds since January 1, 1970 UTC.
  21. func MSToTime(t int64) time.Time {
  22. return time.Unix(0, t*int64(time.Millisecond))
  23. }
  24. type waitGroupWrapper struct {
  25. sync.WaitGroup
  26. }
  27. func (w *waitGroupWrapper) Wrap(cb func()) {
  28. w.Add(1)
  29. go func() {
  30. cb()
  31. w.Done()
  32. }()
  33. }
  34. var _nextId uint64
  35. func NextId() uint64 {
  36. return atomic.AddUint64(&_nextId, 1)
  37. }