-
Notifications
You must be signed in to change notification settings - Fork 150
/
concurrent.go
297 lines (260 loc) · 6.56 KB
/
concurrent.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* Data types and structues for concurrent use:
AtomicInt32
AtomicInt64
AtomicBoolean
ConcurrentMap
*/
package tao
import (
"fmt"
"sync/atomic"
)
// AtomicInt64 provides atomic int64 type.
type AtomicInt64 int64
// NewAtomicInt64 returns an atomic int64 type.
func NewAtomicInt64(initialValue int64) *AtomicInt64 {
a := AtomicInt64(initialValue)
return &a
}
// Get returns the value of int64 atomically.
func (a *AtomicInt64) Get() int64 {
return int64(*a)
}
// Set sets the value of int64 atomically.
func (a *AtomicInt64) Set(newValue int64) {
atomic.StoreInt64((*int64)(a), newValue)
}
// GetAndSet sets new value and returns the old atomically.
func (a *AtomicInt64) GetAndSet(newValue int64) int64 {
for {
current := a.Get()
if a.CompareAndSet(current, newValue) {
return current
}
}
}
// CompareAndSet compares int64 with expected value, if equals as expected
// then sets the updated value, this operation performs atomically.
func (a *AtomicInt64) CompareAndSet(expect, update int64) bool {
return atomic.CompareAndSwapInt64((*int64)(a), expect, update)
}
// GetAndIncrement gets the old value and then increment by 1, this operation
// performs atomically.
func (a *AtomicInt64) GetAndIncrement() int64 {
for {
current := a.Get()
next := current + 1
if a.CompareAndSet(current, next) {
return current
}
}
}
// GetAndDecrement gets the old value and then decrement by 1, this operation
// performs atomically.
func (a *AtomicInt64) GetAndDecrement() int64 {
for {
current := a.Get()
next := current - 1
if a.CompareAndSet(current, next) {
return current
}
}
}
// GetAndAdd gets the old value and then add by delta, this operation
// performs atomically.
func (a *AtomicInt64) GetAndAdd(delta int64) int64 {
for {
current := a.Get()
next := current + delta
if a.CompareAndSet(current, next) {
return current
}
}
}
// IncrementAndGet increments the value by 1 and then gets the value, this
// operation performs atomically.
func (a *AtomicInt64) IncrementAndGet() int64 {
for {
current := a.Get()
next := current + 1
if a.CompareAndSet(current, next) {
return next
}
}
}
// DecrementAndGet decrements the value by 1 and then gets the value, this
// operation performs atomically.
func (a *AtomicInt64) DecrementAndGet() int64 {
for {
current := a.Get()
next := current - 1
if a.CompareAndSet(current, next) {
return next
}
}
}
// AddAndGet adds the value by delta and then gets the value, this operation
// performs atomically.
func (a *AtomicInt64) AddAndGet(delta int64) int64 {
for {
current := a.Get()
next := current + delta
if a.CompareAndSet(current, next) {
return next
}
}
}
func (a *AtomicInt64) String() string {
return fmt.Sprintf("%d", a.Get())
}
// AtomicInt32 provides atomic int32 type.
type AtomicInt32 int32
// NewAtomicInt32 returns an atomoic int32 type.
func NewAtomicInt32(initialValue int32) *AtomicInt32 {
a := AtomicInt32(initialValue)
return &a
}
// Get returns the value of int32 atomically.
func (a *AtomicInt32) Get() int32 {
return int32(*a)
}
// Set sets the value of int32 atomically.
func (a *AtomicInt32) Set(newValue int32) {
atomic.StoreInt32((*int32)(a), newValue)
}
// GetAndSet sets new value and returns the old atomically.
func (a *AtomicInt32) GetAndSet(newValue int32) (oldValue int32) {
for {
oldValue = a.Get()
if a.CompareAndSet(oldValue, newValue) {
return
}
}
}
// CompareAndSet compares int64 with expected value, if equals as expected
// then sets the updated value, this operation performs atomically.
func (a *AtomicInt32) CompareAndSet(expect, update int32) bool {
return atomic.CompareAndSwapInt32((*int32)(a), expect, update)
}
// GetAndIncrement gets the old value and then increment by 1, this operation
// performs atomically.
func (a *AtomicInt32) GetAndIncrement() int32 {
for {
current := a.Get()
next := current + 1
if a.CompareAndSet(current, next) {
return current
}
}
}
// GetAndDecrement gets the old value and then decrement by 1, this operation
// performs atomically.
func (a *AtomicInt32) GetAndDecrement() int32 {
for {
current := a.Get()
next := current - 1
if a.CompareAndSet(current, next) {
return current
}
}
}
// GetAndAdd gets the old value and then add by delta, this operation
// performs atomically.
func (a *AtomicInt32) GetAndAdd(delta int32) int32 {
for {
current := a.Get()
next := current + delta
if a.CompareAndSet(current, next) {
return current
}
}
}
// IncrementAndGet increments the value by 1 and then gets the value, this
// operation performs atomically.
func (a *AtomicInt32) IncrementAndGet() int32 {
for {
current := a.Get()
next := current + 1
if a.CompareAndSet(current, next) {
return next
}
}
}
// DecrementAndGet decrements the value by 1 and then gets the value, this
// operation performs atomically.
func (a *AtomicInt32) DecrementAndGet() int32 {
for {
current := a.Get()
next := current - 1
if a.CompareAndSet(current, next) {
return next
}
}
}
// AddAndGet adds the value by delta and then gets the value, this operation
// performs atomically.
func (a *AtomicInt32) AddAndGet(delta int32) int32 {
for {
current := a.Get()
next := current + delta
if a.CompareAndSet(current, next) {
return next
}
}
}
func (a *AtomicInt32) String() string {
return fmt.Sprintf("%d", a.Get())
}
// AtomicBoolean provides atomic boolean type.
type AtomicBoolean int32
// NewAtomicBoolean returns an atomic boolean type.
func NewAtomicBoolean(initialValue bool) *AtomicBoolean {
var a AtomicBoolean
if initialValue {
a = AtomicBoolean(1)
} else {
a = AtomicBoolean(0)
}
return &a
}
// Get returns the value of boolean atomically.
func (a *AtomicBoolean) Get() bool {
return atomic.LoadInt32((*int32)(a)) != 0
}
// Set sets the value of boolean atomically.
func (a *AtomicBoolean) Set(newValue bool) {
if newValue {
atomic.StoreInt32((*int32)(a), 1)
} else {
atomic.StoreInt32((*int32)(a), 0)
}
}
// CompareAndSet compares boolean with expected value, if equals as expected
// then sets the updated value, this operation performs atomically.
func (a *AtomicBoolean) CompareAndSet(oldValue, newValue bool) bool {
var o int32
var n int32
if oldValue {
o = 1
} else {
o = 0
}
if newValue {
n = 1
} else {
n = 0
}
return atomic.CompareAndSwapInt32((*int32)(a), o, n)
}
// GetAndSet sets new value and returns the old atomically.
func (a *AtomicBoolean) GetAndSet(newValue bool) bool {
for {
current := a.Get()
if a.CompareAndSet(current, newValue) {
return current
}
}
}
func (a *AtomicBoolean) String() string {
return fmt.Sprintf("%t", a.Get())
}