github.com/zhongdalu/gf@v1.0.0/g/container/gtype/int64.go (about) 1 // Copyright 2018 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/zhongdalu/gf. 6 7 package gtype 8 9 import ( 10 "sync/atomic" 11 ) 12 13 type Int64 struct { 14 value int64 15 } 16 17 // NewInt64 returns a concurrent-safe object for int64 type, 18 // with given initial value <value>. 19 func NewInt64(value ...int64) *Int64 { 20 if len(value) > 0 { 21 return &Int64{ 22 value: value[0], 23 } 24 } 25 return &Int64{} 26 } 27 28 // Clone clones and returns a new concurrent-safe object for int64 type. 29 func (v *Int64) Clone() *Int64 { 30 return NewInt64(v.Val()) 31 } 32 33 // Set atomically stores <value> into t.value and returns the previous value of t.value. 34 func (v *Int64) Set(value int64) (old int64) { 35 return atomic.SwapInt64(&v.value, value) 36 } 37 38 // Val atomically loads t.value. 39 func (v *Int64) Val() int64 { 40 return atomic.LoadInt64(&v.value) 41 } 42 43 // Add atomically adds <delta> to t.value and returns the new value. 44 func (v *Int64) Add(delta int64) (new int64) { 45 return atomic.AddInt64(&v.value, delta) 46 } 47 48 // Cas executes the compare-and-swap operation for value. 49 func (v *Int64) Cas(old, new int64) bool { 50 return atomic.CompareAndSwapInt64(&v.value, old, new) 51 }