-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatomic.go
More file actions
278 lines (241 loc) · 8 KB
/
atomic.go
File metadata and controls
278 lines (241 loc) · 8 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
// SPDX-License-Identifier: EUPL-1.2
// Typed atomic primitives — wrappers over sync/atomic typed types.
//
// Each type is a value-type wrapper; zero value is the natural zero
// (false / 0 / nil). Must not be copied after first use.
//
// Mirrors Go 1.19+ stdlib typed atomics one-for-one. Method names and
// semantics are pass-through.
//
// Usage:
//
// var ready core.AtomicBool
// ready.Store(true)
// if ready.Load() { /* signal received */ }
//
// var counter core.AtomicInt64
// counter.Add(1)
// value := counter.Load()
//
// var current core.AtomicPointer[Config]
// current.Store(&Config{...})
// cfg := current.Load()
package core
import "sync/atomic"
// AtomicBool is a race-free atomic boolean. Zero value is false.
//
// var ready core.AtomicBool
// ready.Store(true)
// if ready.Load() { core.Println("agent ready") }
type AtomicBool struct{ inner atomic.Bool }
// Load returns the current value.
//
// var ready core.AtomicBool
// ready.Store(true)
// current := ready.Load()
// core.Println(current)
func (a *AtomicBool) Load() bool { return a.inner.Load() }
// Store sets the value.
//
// var ready core.AtomicBool
// ready.Store(true)
func (a *AtomicBool) Store(val bool) { a.inner.Store(val) }
// Swap stores new and returns the previous value.
//
// var ready core.AtomicBool
// previous := ready.Swap(true)
// core.Println(previous)
func (a *AtomicBool) Swap(new bool) bool { return a.inner.Swap(new) }
// CompareAndSwap stores new only if the current value equals old.
//
// if a.CompareAndSwap(false, true) { /* claimed */ }
func (a *AtomicBool) CompareAndSwap(old, new bool) bool {
return a.inner.CompareAndSwap(old, new)
}
// AtomicInt32 is a race-free atomic int32. Zero value is 0.
//
// var queueDepth core.AtomicInt32
// queueDepth.Store(3)
// core.Println(queueDepth.Load())
type AtomicInt32 struct{ inner atomic.Int32 }
// Load returns the current value.
//
// var queueDepth core.AtomicInt32
// queueDepth.Store(3)
// depth := queueDepth.Load()
// core.Println(depth)
func (a *AtomicInt32) Load() int32 { return a.inner.Load() }
// Store sets the value.
//
// var queueDepth core.AtomicInt32
// queueDepth.Store(3)
func (a *AtomicInt32) Store(val int32) { a.inner.Store(val) }
// Add atomically adds delta and returns the new value.
//
// new := a.Add(1)
func (a *AtomicInt32) Add(delta int32) int32 { return a.inner.Add(delta) }
// Swap stores new and returns the previous value.
//
// var queueDepth core.AtomicInt32
// previous := queueDepth.Swap(5)
// core.Println(previous)
func (a *AtomicInt32) Swap(new int32) int32 { return a.inner.Swap(new) }
// CompareAndSwap stores new only if the current value equals old.
//
// var queueDepth core.AtomicInt32
// if queueDepth.CompareAndSwap(0, 1) {
// core.Println("worker claimed queue")
// }
func (a *AtomicInt32) CompareAndSwap(old, new int32) bool {
return a.inner.CompareAndSwap(old, new)
}
// AtomicInt64 is a race-free atomic int64. Zero value is 0.
//
// var taskID core.AtomicInt64
// next := taskID.Add(1)
// core.Println(next)
type AtomicInt64 struct{ inner atomic.Int64 }
// Load returns the current value.
//
// var taskID core.AtomicInt64
// taskID.Store(42)
// current := taskID.Load()
// core.Println(current)
func (a *AtomicInt64) Load() int64 { return a.inner.Load() }
// Store sets the value.
//
// var taskID core.AtomicInt64
// taskID.Store(42)
func (a *AtomicInt64) Store(val int64) { a.inner.Store(val) }
// Add atomically adds delta and returns the new value.
//
// var taskID core.AtomicInt64
// next := taskID.Add(1)
// core.Println(next)
func (a *AtomicInt64) Add(delta int64) int64 { return a.inner.Add(delta) }
// Swap stores new and returns the previous value.
//
// var taskID core.AtomicInt64
// previous := taskID.Swap(100)
// core.Println(previous)
func (a *AtomicInt64) Swap(new int64) int64 { return a.inner.Swap(new) }
// CompareAndSwap stores new only if the current value equals old.
//
// var taskID core.AtomicInt64
// if taskID.CompareAndSwap(0, 1) {
// core.Println("first task")
// }
func (a *AtomicInt64) CompareAndSwap(old, new int64) bool {
return a.inner.CompareAndSwap(old, new)
}
// AtomicUint32 is a race-free atomic uint32. Zero value is 0.
//
// var workers core.AtomicUint32
// workers.Add(1)
// core.Println(workers.Load())
type AtomicUint32 struct{ inner atomic.Uint32 }
// Load returns the current value.
//
// var workers core.AtomicUint32
// workers.Store(4)
// core.Println(workers.Load())
func (a *AtomicUint32) Load() uint32 { return a.inner.Load() }
// Store sets the value.
//
// var workers core.AtomicUint32
// workers.Store(4)
func (a *AtomicUint32) Store(val uint32) { a.inner.Store(val) }
// Add atomically adds delta and returns the new value.
//
// var workers core.AtomicUint32
// workers.Add(1)
func (a *AtomicUint32) Add(delta uint32) uint32 { return a.inner.Add(delta) }
// Swap stores new and returns the previous value.
//
// var workers core.AtomicUint32
// previous := workers.Swap(8)
// core.Println(previous)
func (a *AtomicUint32) Swap(new uint32) uint32 { return a.inner.Swap(new) }
// CompareAndSwap stores new only if the current value equals old.
//
// var workers core.AtomicUint32
// if workers.CompareAndSwap(0, 4) {
// core.Println("pool opened")
// }
func (a *AtomicUint32) CompareAndSwap(old, new uint32) bool {
return a.inner.CompareAndSwap(old, new)
}
// AtomicUint64 is a race-free atomic uint64. Zero value is 0.
//
// var bytesSeen core.AtomicUint64
// bytesSeen.Add(4096)
// core.Println(bytesSeen.Load())
type AtomicUint64 struct{ inner atomic.Uint64 }
// Load returns the current value.
//
// var bytesSeen core.AtomicUint64
// bytesSeen.Store(4096)
// core.Println(bytesSeen.Load())
func (a *AtomicUint64) Load() uint64 { return a.inner.Load() }
// Store sets the value.
//
// var bytesSeen core.AtomicUint64
// bytesSeen.Store(4096)
func (a *AtomicUint64) Store(val uint64) { a.inner.Store(val) }
// Add atomically adds delta and returns the new value.
//
// var bytesSeen core.AtomicUint64
// bytesSeen.Add(4096)
func (a *AtomicUint64) Add(delta uint64) uint64 { return a.inner.Add(delta) }
// Swap stores new and returns the previous value.
//
// var bytesSeen core.AtomicUint64
// previous := bytesSeen.Swap(8192)
// core.Println(previous)
func (a *AtomicUint64) Swap(new uint64) uint64 { return a.inner.Swap(new) }
// CompareAndSwap stores new only if the current value equals old.
//
// var bytesSeen core.AtomicUint64
// if bytesSeen.CompareAndSwap(0, 4096) {
// core.Println("first payload")
// }
func (a *AtomicUint64) CompareAndSwap(old, new uint64) bool {
return a.inner.CompareAndSwap(old, new)
}
// AtomicPointer is a race-free atomic *T. Zero value is nil.
//
// var current core.AtomicPointer[Config]
// current.Store(&Config{...})
// cfg := current.Load()
type AtomicPointer[T any] struct{ inner atomic.Pointer[T] }
// Load returns the current pointer.
//
// var current core.AtomicPointer[core.ConfigOptions]
// cfg := core.ConfigOptions{Settings: map[string]any{"config.host": "homelab.lthn.sh"}}
// current.Store(&cfg)
// loaded := current.Load()
// _ = loaded
func (a *AtomicPointer[T]) Load() *T { return a.inner.Load() }
// Store sets the pointer.
//
// var current core.AtomicPointer[core.ConfigOptions]
// cfg := core.ConfigOptions{Settings: map[string]any{"config.host": "homelab.lthn.sh"}}
// current.Store(&cfg)
func (a *AtomicPointer[T]) Store(val *T) { a.inner.Store(val) }
// Swap stores new and returns the previous pointer.
//
// var current core.AtomicPointer[core.ConfigOptions]
// next := core.ConfigOptions{Settings: map[string]any{"config.host": "homelab.lthn.sh"}}
// previous := current.Swap(&next)
// _ = previous
func (a *AtomicPointer[T]) Swap(new *T) *T { return a.inner.Swap(new) }
// CompareAndSwap stores new only if the current pointer equals old.
//
// var current core.AtomicPointer[core.ConfigOptions]
// old := core.ConfigOptions{}
// next := core.ConfigOptions{Settings: map[string]any{"config.host": "homelab.lthn.sh"}}
// current.Store(&old)
// _ = current.CompareAndSwap(&old, &next)
func (a *AtomicPointer[T]) CompareAndSwap(old, new *T) bool {
return a.inner.CompareAndSwap(old, new)
}