mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-24 01:30:25 +08:00
31 lines
608 B
Go
31 lines
608 B
Go
package syncx
|
|
|
|
import (
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
type AtomicDuration int64
|
|
|
|
func NewAtomicDuration() *AtomicDuration {
|
|
return new(AtomicDuration)
|
|
}
|
|
|
|
func ForAtomicDuration(val time.Duration) *AtomicDuration {
|
|
d := NewAtomicDuration()
|
|
d.Set(val)
|
|
return d
|
|
}
|
|
|
|
func (d *AtomicDuration) CompareAndSwap(old, val time.Duration) bool {
|
|
return atomic.CompareAndSwapInt64((*int64)(d), int64(old), int64(val))
|
|
}
|
|
|
|
func (d *AtomicDuration) Load() time.Duration {
|
|
return time.Duration(atomic.LoadInt64((*int64)(d)))
|
|
}
|
|
|
|
func (d *AtomicDuration) Set(val time.Duration) {
|
|
atomic.StoreInt64((*int64)(d), int64(val))
|
|
}
|