1
2
3
4
5 package atomic
6
7 import "unsafe"
8
9
10
11
12
13 type Bool struct {
14 _ noCopy
15 v uint32
16 }
17
18
19 func (x *Bool) Load() bool { return LoadUint32(&x.v) != 0 }
20
21
22 func (x *Bool) Store(val bool) { StoreUint32(&x.v, b32(val)) }
23
24
25 func (x *Bool) Swap(new bool) (old bool) { return SwapUint32(&x.v, b32(new)) != 0 }
26
27
28 func (x *Bool) CompareAndSwap(old, new bool) (swapped bool) {
29 return CompareAndSwapUint32(&x.v, b32(old), b32(new))
30 }
31
32
33 func b32(b bool) uint32 {
34 if b {
35 return 1
36 }
37 return 0
38 }
39
40
41
42 var _ = &Pointer[int]{}
43
44
45
46
47 type Pointer[T any] struct {
48
49
50
51 _ [0]*T
52
53 _ noCopy
54 v unsafe.Pointer
55 }
56
57
58 func (x *Pointer[T]) Load() *T { return (*T)(LoadPointer(&x.v)) }
59
60
61 func (x *Pointer[T]) Store(val *T) { StorePointer(&x.v, unsafe.Pointer(val)) }
62
63
64 func (x *Pointer[T]) Swap(new *T) (old *T) { return (*T)(SwapPointer(&x.v, unsafe.Pointer(new))) }
65
66
67 func (x *Pointer[T]) CompareAndSwap(old, new *T) (swapped bool) {
68 return CompareAndSwapPointer(&x.v, unsafe.Pointer(old), unsafe.Pointer(new))
69 }
70
71
72
73
74 type Int32 struct {
75 _ noCopy
76 v int32
77 }
78
79
80 func (x *Int32) Load() int32 { return LoadInt32(&x.v) }
81
82
83 func (x *Int32) Store(val int32) { StoreInt32(&x.v, val) }
84
85
86 func (x *Int32) Swap(new int32) (old int32) { return SwapInt32(&x.v, new) }
87
88
89 func (x *Int32) CompareAndSwap(old, new int32) (swapped bool) {
90 return CompareAndSwapInt32(&x.v, old, new)
91 }
92
93
94 func (x *Int32) Add(delta int32) (new int32) { return AddInt32(&x.v, delta) }
95
96
97
98 func (x *Int32) And(mask int32) (old int32) { return AndInt32(&x.v, mask) }
99
100
101
102 func (x *Int32) Or(mask int32) (old int32) { return OrInt32(&x.v, mask) }
103
104
105
106
107 type Int64 struct {
108 _ noCopy
109 _ align64
110 v int64
111 }
112
113
114 func (x *Int64) Load() int64 { return LoadInt64(&x.v) }
115
116
117 func (x *Int64) Store(val int64) { StoreInt64(&x.v, val) }
118
119
120 func (x *Int64) Swap(new int64) (old int64) { return SwapInt64(&x.v, new) }
121
122
123 func (x *Int64) CompareAndSwap(old, new int64) (swapped bool) {
124 return CompareAndSwapInt64(&x.v, old, new)
125 }
126
127
128 func (x *Int64) Add(delta int64) (new int64) { return AddInt64(&x.v, delta) }
129
130
131
132 func (x *Int64) And(mask int64) (old int64) { return AndInt64(&x.v, mask) }
133
134
135
136 func (x *Int64) Or(mask int64) (old int64) { return OrInt64(&x.v, mask) }
137
138
139
140
141 type Uint32 struct {
142 _ noCopy
143 v uint32
144 }
145
146
147 func (x *Uint32) Load() uint32 { return LoadUint32(&x.v) }
148
149
150 func (x *Uint32) Store(val uint32) { StoreUint32(&x.v, val) }
151
152
153 func (x *Uint32) Swap(new uint32) (old uint32) { return SwapUint32(&x.v, new) }
154
155
156 func (x *Uint32) CompareAndSwap(old, new uint32) (swapped bool) {
157 return CompareAndSwapUint32(&x.v, old, new)
158 }
159
160
161 func (x *Uint32) Add(delta uint32) (new uint32) { return AddUint32(&x.v, delta) }
162
163
164
165 func (x *Uint32) And(mask uint32) (old uint32) { return AndUint32(&x.v, mask) }
166
167
168
169 func (x *Uint32) Or(mask uint32) (old uint32) { return OrUint32(&x.v, mask) }
170
171
172
173
174 type Uint64 struct {
175 _ noCopy
176 _ align64
177 v uint64
178 }
179
180
181 func (x *Uint64) Load() uint64 { return LoadUint64(&x.v) }
182
183
184 func (x *Uint64) Store(val uint64) { StoreUint64(&x.v, val) }
185
186
187 func (x *Uint64) Swap(new uint64) (old uint64) { return SwapUint64(&x.v, new) }
188
189
190 func (x *Uint64) CompareAndSwap(old, new uint64) (swapped bool) {
191 return CompareAndSwapUint64(&x.v, old, new)
192 }
193
194
195 func (x *Uint64) Add(delta uint64) (new uint64) { return AddUint64(&x.v, delta) }
196
197
198
199 func (x *Uint64) And(mask uint64) (old uint64) { return AndUint64(&x.v, mask) }
200
201
202
203 func (x *Uint64) Or(mask uint64) (old uint64) { return OrUint64(&x.v, mask) }
204
205
206
207
208 type Uintptr struct {
209 _ noCopy
210 v uintptr
211 }
212
213
214 func (x *Uintptr) Load() uintptr { return LoadUintptr(&x.v) }
215
216
217 func (x *Uintptr) Store(val uintptr) { StoreUintptr(&x.v, val) }
218
219
220 func (x *Uintptr) Swap(new uintptr) (old uintptr) { return SwapUintptr(&x.v, new) }
221
222
223 func (x *Uintptr) CompareAndSwap(old, new uintptr) (swapped bool) {
224 return CompareAndSwapUintptr(&x.v, old, new)
225 }
226
227
228 func (x *Uintptr) Add(delta uintptr) (new uintptr) { return AddUintptr(&x.v, delta) }
229
230
231
232 func (x *Uintptr) And(mask uintptr) (old uintptr) { return AndUintptr(&x.v, mask) }
233
234
235
236 func (x *Uintptr) Or(mask uintptr) (old uintptr) { return OrUintptr(&x.v, mask) }
237
238
239
240
241
242
243
244
245 type noCopy struct{}
246
247
248 func (*noCopy) Lock() {}
249 func (*noCopy) Unlock() {}
250
251
252
253
254 type align64 struct{}
255
View as plain text