/
int32.go
64 lines (52 loc) · 979 Bytes
/
int32.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 NewInt32(v int32) Int32 {
return Int32{v}
}
type Int32 struct {
v int32
}
func (p *Int32) Load() int32 {
return atomic.LoadInt32(&p.v)
}
func (p *Int32) Store(v int32) {
atomic.StoreInt32(&p.v, v)
}
func (p *Int32) Swap(v int32) int32 {
return atomic.SwapInt32(&p.v, v)
}
func (p *Int32) CompareAndSwap(old, new int32) bool {
return atomic.CompareAndSwapInt32(&p.v, old, new)
}
func (p *Int32) Add(v int32) int32 {
return atomic.AddInt32(&p.v, v)
}
func (p *Int32) Sub(v int32) int32 {
return p.Add(-v)
}
func (p *Int32) String() string {
return strconv.FormatInt(int64(p.Load()), 10)
}
func (p *Int32) SetLesser(v int32) int32 {
for {
switch x := p.Load(); {
case x <= v:
return x
case p.CompareAndSwap(x, v):
return v
}
}
}
func (p *Int32) SetGreater(v int32) int32 {
for {
switch x := p.Load(); {
case x >= v:
return x
case p.CompareAndSwap(x, v):
return v
}
}
}