-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatomic_example_test.go
More file actions
378 lines (340 loc) · 9.67 KB
/
Copy pathatomic_example_test.go
File metadata and controls
378 lines (340 loc) · 9.67 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package core_test
import (
. "dappco.re/go"
)
// ExampleAtomicBool updates a boolean atomically through `AtomicBool` for shared runtime
// state. Concurrent state changes use explicit load, store, swap, and compare-and-swap
// shapes.
func ExampleAtomicBool() {
var ready AtomicBool
if !ready.Load() {
Println("not ready")
}
ready.Store(true)
if ready.Load() {
Println("ready")
}
// Output:
// not ready
// ready
}
// ExampleAtomicBool_Swap swaps a value through `AtomicBool.Swap` and returns the previous
// value for shared runtime state. Concurrent state changes use explicit load, store, swap,
// and compare-and-swap shapes.
func ExampleAtomicBool_Swap() {
var ready AtomicBool
ready.Store(true)
Println(ready.Swap(false))
Println(ready.Load())
// Output:
// true
// false
}
// ExampleAtomicBool_CompareAndSwap updates `AtomicBool.CompareAndSwap` only when the
// previous value matches for shared runtime state. Concurrent state changes use explicit
// load, store, swap, and compare-and-swap shapes.
func ExampleAtomicBool_CompareAndSwap() {
var ready AtomicBool
Println(ready.CompareAndSwap(false, true))
Println(ready.Load())
// Output:
// true
// true
}
// ExampleAtomicInt32 updates a 32-bit integer atomically through `AtomicInt32` for shared
// runtime state. Concurrent state changes use explicit load, store, swap, and
// compare-and-swap shapes.
func ExampleAtomicInt32() {
var counter AtomicInt32
counter.Store(10)
Println(counter.Add(5))
Println(counter.Swap(1))
Println(counter.Load())
// Output:
// 15
// 15
// 1
}
// ExampleAtomicInt64 updates a 64-bit integer atomically through `AtomicInt64` for shared
// runtime state. Concurrent state changes use explicit load, store, swap, and
// compare-and-swap shapes.
func ExampleAtomicInt64() {
var counter AtomicInt64
counter.Add(1)
counter.Add(1)
counter.Add(1)
Println(counter.Load())
// Output:
// 3
}
// ExampleAtomicInt64_Swap swaps a value through `AtomicInt64.Swap` and returns the
// previous value for shared runtime state. Concurrent state changes use explicit load,
// store, swap, and compare-and-swap shapes.
func ExampleAtomicInt64_Swap() {
var counter AtomicInt64
counter.Store(7)
Println(counter.Swap(9))
Println(counter.Load())
// Output:
// 7
// 9
}
// ExampleAtomicInt64_CompareAndSwap updates `AtomicInt64.CompareAndSwap` only when the
// previous value matches for shared runtime state. Concurrent state changes use explicit
// load, store, swap, and compare-and-swap shapes.
func ExampleAtomicInt64_CompareAndSwap() {
var counter AtomicInt64
counter.Store(7)
Println(counter.CompareAndSwap(7, 9))
Println(counter.Load())
// Output:
// true
// 9
}
// ExampleAtomicInt32_CompareAndSwap updates `AtomicInt32.CompareAndSwap` only when the
// previous value matches for shared runtime state. Concurrent state changes use explicit
// load, store, swap, and compare-and-swap shapes.
func ExampleAtomicInt32_CompareAndSwap() {
var state AtomicInt32
if state.CompareAndSwap(0, 1) {
Println("claimed")
}
if !state.CompareAndSwap(0, 2) {
Println("already claimed")
}
// Output:
// claimed
// already claimed
}
// ExampleAtomicUint32 updates an unsigned 32-bit integer atomically through `AtomicUint32`
// for shared runtime state. Concurrent state changes use explicit load, store, swap, and
// compare-and-swap shapes.
func ExampleAtomicUint32() {
var counter AtomicUint32
counter.Store(10)
Println(counter.Add(5))
Println(counter.Swap(1))
Println(counter.Load())
// Output:
// 15
// 15
// 1
}
// ExampleAtomicUint32_CompareAndSwap updates `AtomicUint32.CompareAndSwap` only when the
// previous value matches for shared runtime state. Concurrent state changes use explicit
// load, store, swap, and compare-and-swap shapes.
func ExampleAtomicUint32_CompareAndSwap() {
var counter AtomicUint32
counter.Store(10)
Println(counter.CompareAndSwap(10, 11))
Println(counter.Load())
// Output:
// true
// 11
}
// ExampleAtomicUint64 updates an unsigned 64-bit integer atomically through `AtomicUint64`
// for shared runtime state. Concurrent state changes use explicit load, store, swap, and
// compare-and-swap shapes.
func ExampleAtomicUint64() {
var counter AtomicUint64
counter.Store(10)
Println(counter.Add(5))
Println(counter.Swap(1))
Println(counter.Load())
// Output:
// 15
// 15
// 1
}
// ExampleAtomicUint64_CompareAndSwap updates `AtomicUint64.CompareAndSwap` only when the
// previous value matches for shared runtime state. Concurrent state changes use explicit
// load, store, swap, and compare-and-swap shapes.
func ExampleAtomicUint64_CompareAndSwap() {
var counter AtomicUint64
counter.Store(10)
Println(counter.CompareAndSwap(10, 11))
Println(counter.Load())
// Output:
// true
// 11
}
type config struct {
name string
}
// ExampleAtomicPointer updates a typed pointer atomically through `AtomicPointer` for
// shared runtime state. Concurrent state changes use explicit load, store, swap, and
// compare-and-swap shapes.
func ExampleAtomicPointer() {
var current AtomicPointer[config]
current.Store(&config{name: "v1"})
cfg := current.Load()
Println(cfg.name)
current.Store(&config{name: "v2"})
Println(current.Load().name)
// Output:
// v1
// v2
}
// ExampleAtomicPointer_Swap swaps a value through `AtomicPointer.Swap` and returns the
// previous value for shared runtime state. Concurrent state changes use explicit load,
// store, swap, and compare-and-swap shapes.
func ExampleAtomicPointer_Swap() {
var current AtomicPointer[config]
first := &config{name: "v1"}
second := &config{name: "v2"}
current.Store(first)
Println(current.Swap(second).name)
Println(current.Load().name)
// Output:
// v1
// v2
}
// ExampleAtomicPointer_CompareAndSwap updates `AtomicPointer.CompareAndSwap` only when the
// previous value matches for shared runtime state. Concurrent state changes use explicit
// load, store, swap, and compare-and-swap shapes.
// ExampleAtomicBool_Load reads a boolean atomically through `AtomicBool.Load`.
func ExampleAtomicBool_Load() {
var b AtomicBool
b.Store(true)
Println(b.Load())
// Output: true
}
// ExampleAtomicBool_Store writes a boolean atomically through `AtomicBool.Store`.
func ExampleAtomicBool_Store() {
var b AtomicBool
b.Store(true)
Println(b.Load())
// Output: true
}
// ExampleAtomicInt32_Load reads an int32 atomically through `AtomicInt32.Load`.
func ExampleAtomicInt32_Load() {
var n AtomicInt32
n.Store(5)
Println(n.Load())
// Output: 5
}
// ExampleAtomicInt32_Store writes an int32 atomically through `AtomicInt32.Store`.
func ExampleAtomicInt32_Store() {
var n AtomicInt32
n.Store(42)
Println(n.Load())
// Output: 42
}
// ExampleAtomicInt32_Add atomically adds and returns the new value through `AtomicInt32.Add`.
func ExampleAtomicInt32_Add() {
var n AtomicInt32
n.Store(5)
Println(n.Add(3))
// Output: 8
}
// ExampleAtomicInt32_Swap stores and returns the previous value through `AtomicInt32.Swap`.
func ExampleAtomicInt32_Swap() {
var n AtomicInt32
n.Store(5)
Println(n.Swap(9))
// Output: 5
}
// ExampleAtomicInt64_Load reads an int64 atomically through `AtomicInt64.Load`.
func ExampleAtomicInt64_Load() {
var n AtomicInt64
n.Store(5)
Println(n.Load())
// Output: 5
}
// ExampleAtomicInt64_Store writes an int64 atomically through `AtomicInt64.Store`.
func ExampleAtomicInt64_Store() {
var n AtomicInt64
n.Store(42)
Println(n.Load())
// Output: 42
}
// ExampleAtomicInt64_Add atomically adds and returns the new value through `AtomicInt64.Add`.
func ExampleAtomicInt64_Add() {
var n AtomicInt64
n.Store(5)
Println(n.Add(3))
// Output: 8
}
// ExampleAtomicUint32_Load reads a uint32 atomically through `AtomicUint32.Load`.
func ExampleAtomicUint32_Load() {
var n AtomicUint32
n.Store(5)
Println(n.Load())
// Output: 5
}
// ExampleAtomicUint32_Store writes a uint32 atomically through `AtomicUint32.Store`.
func ExampleAtomicUint32_Store() {
var n AtomicUint32
n.Store(42)
Println(n.Load())
// Output: 42
}
// ExampleAtomicUint32_Add atomically adds and returns the new value through `AtomicUint32.Add`.
func ExampleAtomicUint32_Add() {
var n AtomicUint32
n.Store(5)
Println(n.Add(3))
// Output: 8
}
// ExampleAtomicUint32_Swap stores and returns the previous value through `AtomicUint32.Swap`.
func ExampleAtomicUint32_Swap() {
var n AtomicUint32
n.Store(5)
Println(n.Swap(9))
// Output: 5
}
// ExampleAtomicUint64_Load reads a uint64 atomically through `AtomicUint64.Load`.
func ExampleAtomicUint64_Load() {
var n AtomicUint64
n.Store(5)
Println(n.Load())
// Output: 5
}
// ExampleAtomicUint64_Store writes a uint64 atomically through `AtomicUint64.Store`.
func ExampleAtomicUint64_Store() {
var n AtomicUint64
n.Store(42)
Println(n.Load())
// Output: 42
}
// ExampleAtomicUint64_Add atomically adds and returns the new value through `AtomicUint64.Add`.
func ExampleAtomicUint64_Add() {
var n AtomicUint64
n.Store(5)
Println(n.Add(3))
// Output: 8
}
// ExampleAtomicUint64_Swap stores and returns the previous value through `AtomicUint64.Swap`.
func ExampleAtomicUint64_Swap() {
var n AtomicUint64
n.Store(5)
Println(n.Swap(9))
// Output: 5
}
// ExampleAtomicPointer_Load reads a pointer atomically through `AtomicPointer.Load`.
func ExampleAtomicPointer_Load() {
var p AtomicPointer[int]
v := 42
p.Store(&v)
Println(*p.Load())
// Output: 42
}
// ExampleAtomicPointer_Store writes a pointer atomically through `AtomicPointer.Store`.
func ExampleAtomicPointer_Store() {
var p AtomicPointer[string]
s := "agent"
p.Store(&s)
Println(*p.Load())
// Output: agent
}
func ExampleAtomicPointer_CompareAndSwap() {
var current AtomicPointer[config]
first := &config{name: "v1"}
second := &config{name: "v2"}
current.Store(first)
Println(current.CompareAndSwap(first, second))
Println(current.Load().name)
// Output:
// true
// v2
}