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.
ntool/nsys/atomic/atomic_int64.go

61 lines
1.4 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package atomic
import "sync/atomic"
type AtomicInt64 struct {
val *atomic.Int64
}
func NewAtomicInt64() *AtomicInt64 {
return &AtomicInt64{val: &atomic.Int64{}}
}
func (a *AtomicInt64) Reset() {
a.val.Store(0)
}
// AddAndGet 以原子方式将当前值+delta并返回+delta之后的值
func (a *AtomicInt64) AddAndGet(delta int64) int64 {
return a.val.Add(delta)
}
// GetAndAdd 以原子方式将当前值+delta并返回+delta之前的值
func (a *AtomicInt64) GetAndAdd(delta int64) int64 {
tmp := a.val.Load()
a.val.Add(delta)
return tmp
}
// UpdateAndGet 以原子方式更新值,并返回更新后的值
func (a *AtomicInt64) UpdateAndGet(val int64) int64 {
a.val.Store(val)
return val
}
// GetAndUpdate 以原子方式更新值,并返回更新前的值
func (a *AtomicInt64) GetAndUpdate(val int64) int64 {
tmp := a.val.Load()
a.val.Store(val)
return tmp
}
// GetAndIncrement 以原子方式将当前值+1并返回+1前的值
func (a *AtomicInt64) GetAndIncrement() int64 {
return a.GetAndAdd(1)
}
// GetAndDecrement 以原子方式将当前值-1并返回-1前的值
func (a *AtomicInt64) GetAndDecrement() int64 {
return a.GetAndAdd(-1)
}
// IncrementAndGet 以原子方式将当前值+1并返回+1后的值
func (a *AtomicInt64) IncrementAndGet() int64 {
return a.AddAndGet(1)
}
// DecrementAndGet 以原子方式将当前值-1并返回-1后的值
func (a *AtomicInt64) DecrementAndGet() int64 {
return a.AddAndGet(-1)
}