github.com/goki/ki@v1.1.11/atomctr/atomctr.go (about) 1 // Copyright (c) 2018, The GoKi Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package atomctr implements basic atomic int64 counter, used e.g., for 6 // update counter on Ki Node 7 package atomctr 8 9 import ( 10 "sync/atomic" 11 ) 12 13 // Ctr implements basic atomic int64 counter, used e.g., for update counter on Ki Node 14 type Ctr int64 15 16 // increment counter 17 func (a *Ctr) Add(inc int64) int64 { 18 return atomic.AddInt64((*int64)(a), inc) 19 } 20 21 // decrement counter 22 func (a *Ctr) Sub(dec int64) int64 { 23 return atomic.AddInt64((*int64)(a), -dec) 24 } 25 26 // inc = ++ 27 func (a *Ctr) Inc() int64 { 28 return atomic.AddInt64((*int64)(a), 1) 29 } 30 31 // dec = -- 32 func (a *Ctr) Dec() int64 { 33 return atomic.AddInt64((*int64)(a), -1) 34 } 35 36 // current value 37 func (a *Ctr) Value() int64 { 38 return atomic.LoadInt64((*int64)(a)) 39 } 40 41 // set current value 42 func (a *Ctr) Set(val int64) { 43 atomic.StoreInt64((*int64)(a), val) 44 } 45 46 // swap new value in and return old value 47 func (a *Ctr) Swap(val int64) int64 { 48 return atomic.SwapInt64((*int64)(a), val) 49 }