atomicduration.go 608 B

123456789101112131415161718192021222324252627282930
  1. package syncx
  2. import (
  3. "sync/atomic"
  4. "time"
  5. )
  6. type AtomicDuration int64
  7. func NewAtomicDuration() *AtomicDuration {
  8. return new(AtomicDuration)
  9. }
  10. func ForAtomicDuration(val time.Duration) *AtomicDuration {
  11. d := NewAtomicDuration()
  12. d.Set(val)
  13. return d
  14. }
  15. func (d *AtomicDuration) CompareAndSwap(old, val time.Duration) bool {
  16. return atomic.CompareAndSwapInt64((*int64)(d), int64(old), int64(val))
  17. }
  18. func (d *AtomicDuration) Load() time.Duration {
  19. return time.Duration(atomic.LoadInt64((*int64)(d)))
  20. }
  21. func (d *AtomicDuration) Set(val time.Duration) {
  22. atomic.StoreInt64((*int64)(d), int64(val))
  23. }