-
Notifications
You must be signed in to change notification settings - Fork 55
/
atomic.go
213 lines (171 loc) · 3.32 KB
/
atomic.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package zutil
import (
"fmt"
"strconv"
"sync/atomic"
)
type (
Bool struct {
_ Nocmp
b int32
}
Int32 struct {
_ Nocmp
v int32
}
Uint32 struct {
_ Nocmp
v uint32
}
Int64 struct {
_ Nocmp
v int64
}
Uintptr struct {
_ Nocmp
v uintptr
}
)
func NewBool(b bool) *Bool {
ret := &Bool{}
if b {
ret.b = 1
}
return ret
}
func (b *Bool) Store(val bool) bool {
var newV int32
if val {
newV = 1
}
return atomic.SwapInt32(&b.b, newV) == 1
}
func (b *Bool) Load() bool {
return atomic.LoadInt32(&b.b) == 1
}
func (b *Bool) Toggle() (old bool) {
for {
old := b.Load()
if b.CAS(old, !old) {
return old
}
}
}
func (b *Bool) CAS(old, new bool) bool {
var o, n int32
if old {
o = 1
}
if new {
n = 1
}
return atomic.CompareAndSwapInt32(&b.b, o, n)
}
func NewInt32(i int32) *Int32 {
return &Int32{
v: i,
}
}
func (i32 *Int32) Add(i int32) int32 {
return atomic.AddInt32(&i32.v, i)
}
func (i32 *Int32) Sub(i int32) int32 {
return atomic.AddInt32(&i32.v, -i)
}
func (i32 *Int32) Swap(i int32) int32 {
return atomic.SwapInt32(&i32.v, i)
}
func (i32 *Int32) Load() int32 {
return atomic.LoadInt32(&i32.v)
}
func (i32 *Int32) Store(i int32) {
atomic.StoreInt32(&i32.v, i)
}
func (i32 *Int32) CAS(old, new int32) bool {
return atomic.CompareAndSwapInt32(&i32.v, old, new)
}
func (i32 *Int32) String() string {
v := i32.Load()
return strconv.FormatInt(int64(v), 10)
}
func NewUint32(i uint32) *Uint32 {
return &Uint32{
v: i,
}
}
func (ui32 *Uint32) Add(i uint32) uint32 {
return atomic.AddUint32(&ui32.v, i)
}
func (ui32 *Uint32) Sub(i uint32) uint32 {
return atomic.AddUint32(&ui32.v, -i)
}
func (ui32 *Uint32) Swap(i uint32) uint32 {
return atomic.SwapUint32(&ui32.v, i)
}
func (ui32 *Uint32) Load() uint32 {
return atomic.LoadUint32(&ui32.v)
}
func (ui32 *Uint32) Store(i uint32) {
atomic.StoreUint32(&ui32.v, i)
}
func (i32 *Uint32) CAS(old, new uint32) bool {
return atomic.CompareAndSwapUint32(&i32.v, old, new)
}
func (ui32 *Uint32) String() string {
v := ui32.Load()
return strconv.FormatInt(int64(v), 10)
}
func NewInt64(i int64) *Int64 {
return &Int64{
v: i,
}
}
func (i64 *Int64) Add(i int64) int64 {
return atomic.AddInt64(&i64.v, i)
}
func (i64 *Int64) Sub(i int64) int64 {
return atomic.AddInt64(&i64.v, -i)
}
func (i64 *Int64) Swap(i int64) int64 {
return atomic.SwapInt64(&i64.v, i)
}
func (i64 *Int64) Load() int64 {
return atomic.LoadInt64(&i64.v)
}
func (i64 *Int64) Store(i int64) {
atomic.StoreInt64(&i64.v, i)
}
func (i64 *Int64) CAS(old, new int64) bool {
return atomic.CompareAndSwapInt64(&i64.v, old, new)
}
func (i64 *Int64) String() string {
v := i64.Load()
return strconv.FormatInt(v, 10)
}
func NewUintptr(i uintptr) *Uintptr {
return &Uintptr{
v: i,
}
}
func (ptr *Uintptr) Add(i uintptr) uintptr {
return atomic.AddUintptr(&ptr.v, i)
}
func (ptr *Uintptr) Sub(i uintptr) uintptr {
return atomic.AddUintptr(&ptr.v, -i)
}
func (ptr *Uintptr) Swap(i uintptr) uintptr {
return atomic.SwapUintptr(&ptr.v, i)
}
func (ptr *Uintptr) Load() uintptr {
return atomic.LoadUintptr(&ptr.v)
}
func (ptr *Uintptr) Store(i uintptr) {
atomic.StoreUintptr(&ptr.v, i)
}
func (ptr *Uintptr) CAS(old, new uintptr) bool {
return atomic.CompareAndSwapUintptr(&ptr.v, old, new)
}
func (ptr *Uintptr) String() string {
v := ptr.Load()
return fmt.Sprintf("%+v", v)
}