-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatomic_example_test.go
More file actions
224 lines (205 loc) · 6.1 KB
/
atomic_example_test.go
File metadata and controls
224 lines (205 loc) · 6.1 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
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.
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
}