-
Notifications
You must be signed in to change notification settings - Fork 4
/
int64.go
64 lines (52 loc) · 972 Bytes
/
int64.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
53
54
55
56
57
58
59
60
61
62
63
64
package atomickit
import (
"strconv"
"sync/atomic"
)
func NewInt64(v int64) Int64 {
return Int64{v}
}
type Int64 struct {
v int64
}
func (p *Int64) Load() int64 {
return atomic.LoadInt64(&p.v)
}
func (p *Int64) Store(v int64) {
atomic.StoreInt64(&p.v, v)
}
func (p *Int64) Swap(v int64) int64 {
return atomic.SwapInt64(&p.v, v)
}
func (p *Int64) CompareAndSwap(old, new int64) bool {
return atomic.CompareAndSwapInt64(&p.v, old, new)
}
func (p *Int64) Add(v int64) int64 {
return atomic.AddInt64(&p.v, v)
}
func (p *Int64) Sub(v int64) int64 {
return p.Add(-v)
}
func (p *Int64) String() string {
return strconv.FormatInt(p.Load(), 10)
}
func (p *Int64) SetLesser(v int64) int64 {
for {
switch x := p.Load(); {
case x <= v:
return x
case p.CompareAndSwap(x, v):
return v
}
}
}
func (p *Int64) SetGreater(v int64) int64 {
for {
switch x := p.Load(); {
case x >= v:
return x
case p.CompareAndSwap(x, v):
return v
}
}
}