You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/syncx/atomicduration.go

37 lines
933 B
Go

4 years ago
package syncx
import (
"sync/atomic"
"time"
)
// An AtomicDuration is an implementation of atomic duration.
4 years ago
type AtomicDuration int64
// NewAtomicDuration returns an AtomicDuration.
4 years ago
func NewAtomicDuration() *AtomicDuration {
return new(AtomicDuration)
}
// ForAtomicDuration returns an AtomicDuration with given value.
4 years ago
func ForAtomicDuration(val time.Duration) *AtomicDuration {
d := NewAtomicDuration()
d.Set(val)
return d
}
// CompareAndSwap compares current value with old, if equals, set the value to val.
4 years ago
func (d *AtomicDuration) CompareAndSwap(old, val time.Duration) bool {
return atomic.CompareAndSwapInt64((*int64)(d), int64(old), int64(val))
}
// Load loads the current duration.
4 years ago
func (d *AtomicDuration) Load() time.Duration {
return time.Duration(atomic.LoadInt64((*int64)(d)))
}
// Set sets the value to val.
4 years ago
func (d *AtomicDuration) Set(val time.Duration) {
atomic.StoreInt64((*int64)(d), int64(val))
}