-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter.go
52 lines (42 loc) · 1.11 KB
/
counter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package atomic
import "sync/atomic"
// Counter is an atomic counter
type Counter struct {
value int64
}
// Set sets counter
func (counter *Counter) Set(value int64) {
atomic.StoreInt64(&counter.value, value)
}
// Increment increments counter
func (counter *Counter) Increment() int64 {
return atomic.AddInt64(&counter.value, 1)
}
// Add adds value to counter
func (counter *Counter) Add(value int64) int64 {
return atomic.AddInt64(&counter.value, value)
}
// Decrement decrements counter
func (counter *Counter) Decrement() int64 {
return atomic.AddInt64(&counter.value, -1)
}
// Subtract subtracts value from counter
func (counter *Counter) Subtract(value int64) int64 {
return atomic.AddInt64(&counter.value, -value)
}
// Get gets counter
func (counter *Counter) Get() int64 {
return atomic.LoadInt64(&counter.value)
}
// Reset resets counter and returns the previous value
func (counter *Counter) Reset() int64 {
return atomic.SwapInt64(&counter.value, 0)
}
// GetUint64 gets counter as uint64
func (counter *Counter) GetUint64() uint64 {
value := counter.Get()
if value < 0 {
return 0
}
return uint64(value)
}