14
14
// Bool AtomicValue conformance
15
15
//===----------------------------------------------------------------------===//
16
16
17
- @available ( SwiftStdlib 5 . 10 , * )
17
+ @available ( SwiftStdlib 5 . 11 , * )
18
18
extension Bool : AtomicValue {
19
- @available ( SwiftStdlib 5 . 10 , * )
19
+ /// The storage representation type that `Self` encodes to and decodes from
20
+ /// which is a suitable type when used in atomic operations.
21
+ @available ( SwiftStdlib 5 . 11 , * )
20
22
public typealias AtomicRepresentation = UInt8 . AtomicRepresentation
21
23
22
- @available ( SwiftStdlib 5 . 10 , * )
24
+ /// Destroys a value of `Self` and prepares an `AtomicRepresentation` storage
25
+ /// type to be used for atomic operations.
26
+ ///
27
+ /// - Note: This is not an atomic operation. This simply encodes the logical
28
+ /// type `Self` into its storage representation suitable for atomic
29
+ /// operations, `AtomicRepresentation`.
30
+ ///
31
+ /// - Parameter value: A valid instance of `Self` that's about to be destroyed
32
+ /// to encode an instance of its `AtomicRepresentation`.
33
+ /// - Returns: The newly encoded `AtomicRepresentation` storage.
34
+ @available ( SwiftStdlib 5 . 11 , * )
23
35
@_alwaysEmitIntoClient
24
36
@_transparent
25
37
public static func encodeAtomicRepresentation(
@@ -30,7 +42,17 @@ extension Bool: AtomicValue {
30
42
)
31
43
}
32
44
33
- @available ( SwiftStdlib 5 . 10 , * )
45
+ /// Recovers the logical atomic type `Self` by destroying some
46
+ /// `AtomicRepresentation` storage instance returned from an atomic operation.
47
+ ///
48
+ /// - Note: This is not an atomic operation. This simply decodes the storage
49
+ /// representation used in atomic operations back into the logical type for
50
+ /// normal use, `Self`.
51
+ ///
52
+ /// - Parameter storage: The storage representation for `Self` that's used
53
+ /// within atomic operations.
54
+ /// - Returns: The newly decoded logical type `Self`.
55
+ @available ( SwiftStdlib 5 . 11 , * )
34
56
@_alwaysEmitIntoClient
35
57
@_transparent
36
58
public static func decodeAtomicRepresentation(
@@ -43,10 +65,10 @@ extension Bool: AtomicValue {
43
65
}
44
66
45
67
//===----------------------------------------------------------------------===//
46
- // Bool load then atomic operations
68
+ // Bool atomic operations
47
69
//===----------------------------------------------------------------------===//
48
70
49
- @available ( SwiftStdlib 5 . 10 , * )
71
+ @available ( SwiftStdlib 5 . 11 , * )
50
72
extension Atomic where Value == Bool {
51
73
/// Perform an atomic logical AND operation and return the old and new value,
52
74
/// applying the specified memory ordering.
@@ -55,54 +77,53 @@ extension Atomic where Value == Bool {
55
77
/// - Parameter ordering: The memory ordering to apply on this operation.
56
78
/// - Returns: A tuple with the old value before the operation a the new value
57
79
/// after the operation.
58
- @available ( SwiftStdlib 5 . 10 , * )
80
+ @available ( SwiftStdlib 5 . 11 , * )
59
81
@discardableResult
60
82
@_semantics ( " atomics.requires_constant_orderings " )
61
83
@_alwaysEmitIntoClient
62
84
@_transparent
63
85
public func logicalAnd(
64
- with operand: Bool ,
86
+ _ operand: Bool ,
65
87
ordering: AtomicUpdateOrdering
66
88
) -> ( oldValue: Bool , newValue: Bool ) {
67
- // If we could reinterpret self as `Atomic<UInt8>` then we could just call
68
- // bitwiseAnd...
89
+ let builtinOperand = Bool . encodeAtomicRepresentation ( operand) . _storage
69
90
70
91
let original = switch ordering {
71
92
case . relaxed:
72
93
Builtin . atomicrmw_and_monotonic_Int8 (
73
94
rawAddress,
74
- Bool . encodeAtomicRepresentation ( operand ) . storage
95
+ builtinOperand
75
96
)
76
97
77
98
case . acquiring:
78
99
Builtin . atomicrmw_and_acquire_Int8 (
79
100
rawAddress,
80
- Bool . encodeAtomicRepresentation ( operand ) . storage
101
+ builtinOperand
81
102
)
82
103
83
104
case . releasing:
84
105
Builtin . atomicrmw_and_release_Int8 (
85
106
rawAddress,
86
- Bool . encodeAtomicRepresentation ( operand ) . storage
107
+ builtinOperand
87
108
)
88
109
89
110
case . acquiringAndReleasing:
90
111
Builtin . atomicrmw_and_acqrel_Int8 (
91
112
rawAddress,
92
- Bool . encodeAtomicRepresentation ( operand ) . storage
113
+ builtinOperand
93
114
)
94
115
95
116
case . sequentiallyConsistent:
96
117
Builtin . atomicrmw_and_seqcst_Int8 (
97
118
rawAddress,
98
- Bool . encodeAtomicRepresentation ( operand ) . storage
119
+ builtinOperand
99
120
)
100
121
101
122
default :
102
123
Builtin . unreachable ( )
103
124
}
104
125
105
- let old = Bool . decodeAtomicRepresentation ( _AtomicStorage8 ( original) )
126
+ let old = Bool . decodeAtomicRepresentation ( UInt8 . AtomicRepresentation ( original) )
106
127
107
128
return ( oldValue: old, newValue: old && operand)
108
129
}
@@ -114,51 +135,53 @@ extension Atomic where Value == Bool {
114
135
/// - Parameter ordering: The memory ordering to apply on this operation.
115
136
/// - Returns: A tuple with the old value before the operation a the new value
116
137
/// after the operation.
117
- @available ( SwiftStdlib 5 . 10 , * )
138
+ @available ( SwiftStdlib 5 . 11 , * )
118
139
@discardableResult
119
140
@_semantics ( " atomics.requires_constant_orderings " )
120
141
@_alwaysEmitIntoClient
121
142
@_transparent
122
143
public func logicalOr(
123
- with operand: Bool ,
144
+ _ operand: Bool ,
124
145
ordering: AtomicUpdateOrdering
125
146
) -> ( oldValue: Bool , newValue: Bool ) {
147
+ let builtinOperand = Bool . encodeAtomicRepresentation ( operand) . _storage
148
+
126
149
let original = switch ordering {
127
150
case . relaxed:
128
151
Builtin . atomicrmw_or_monotonic_Int8 (
129
152
rawAddress,
130
- Bool . encodeAtomicRepresentation ( operand ) . storage
153
+ builtinOperand
131
154
)
132
155
133
156
case . acquiring:
134
157
Builtin . atomicrmw_or_acquire_Int8 (
135
158
rawAddress,
136
- Bool . encodeAtomicRepresentation ( operand ) . storage
159
+ builtinOperand
137
160
)
138
161
139
162
case . releasing:
140
163
Builtin . atomicrmw_or_release_Int8 (
141
164
rawAddress,
142
- Bool . encodeAtomicRepresentation ( operand ) . storage
165
+ builtinOperand
143
166
)
144
167
145
168
case . acquiringAndReleasing:
146
169
Builtin . atomicrmw_or_acqrel_Int8 (
147
170
rawAddress,
148
- Bool . encodeAtomicRepresentation ( operand ) . storage
171
+ builtinOperand
149
172
)
150
173
151
174
case . sequentiallyConsistent:
152
175
Builtin . atomicrmw_or_seqcst_Int8 (
153
176
rawAddress,
154
- Bool . encodeAtomicRepresentation ( operand ) . storage
177
+ builtinOperand
155
178
)
156
179
157
180
default :
158
181
Builtin . unreachable ( )
159
182
}
160
183
161
- let old = Bool . decodeAtomicRepresentation ( _AtomicStorage8 ( original) )
184
+ let old = Bool . decodeAtomicRepresentation ( UInt8 . AtomicRepresentation ( original) )
162
185
163
186
return ( oldValue: old, newValue: old || operand)
164
187
}
@@ -170,51 +193,53 @@ extension Atomic where Value == Bool {
170
193
/// - Parameter ordering: The memory ordering to apply on this operation.
171
194
/// - Returns: A tuple with the old value before the operation a the new value
172
195
/// after the operation.
173
- @available ( SwiftStdlib 5 . 10 , * )
196
+ @available ( SwiftStdlib 5 . 11 , * )
174
197
@discardableResult
175
198
@_semantics ( " atomics.requires_constant_orderings " )
176
199
@_alwaysEmitIntoClient
177
200
@_transparent
178
201
public func logicalXor(
179
- with operand: Bool ,
202
+ _ operand: Bool ,
180
203
ordering: AtomicUpdateOrdering
181
204
) -> ( oldValue: Bool , newValue: Bool ) {
205
+ let builtinOperand = Bool . encodeAtomicRepresentation ( operand) . _storage
206
+
182
207
let original = switch ordering {
183
208
case . relaxed:
184
209
Builtin . atomicrmw_xor_monotonic_Int8 (
185
210
rawAddress,
186
- Bool . encodeAtomicRepresentation ( operand ) . storage
211
+ builtinOperand
187
212
)
188
213
189
214
case . acquiring:
190
215
Builtin . atomicrmw_xor_acquire_Int8 (
191
216
rawAddress,
192
- Bool . encodeAtomicRepresentation ( operand ) . storage
217
+ builtinOperand
193
218
)
194
219
195
220
case . releasing:
196
221
Builtin . atomicrmw_xor_release_Int8 (
197
222
rawAddress,
198
- Bool . encodeAtomicRepresentation ( operand ) . storage
223
+ builtinOperand
199
224
)
200
225
201
226
case . acquiringAndReleasing:
202
227
Builtin . atomicrmw_xor_acqrel_Int8 (
203
228
rawAddress,
204
- Bool . encodeAtomicRepresentation ( operand ) . storage
229
+ builtinOperand
205
230
)
206
231
207
232
case . sequentiallyConsistent:
208
233
Builtin . atomicrmw_xor_seqcst_Int8 (
209
234
rawAddress,
210
- Bool . encodeAtomicRepresentation ( operand ) . storage
235
+ builtinOperand
211
236
)
212
237
213
238
default :
214
239
Builtin . unreachable ( )
215
240
}
216
241
217
- let old = Bool . decodeAtomicRepresentation ( _AtomicStorage8 ( original) )
242
+ let old = Bool . decodeAtomicRepresentation ( UInt8 . AtomicRepresentation ( original) )
218
243
219
244
return ( oldValue: old, newValue: old != operand)
220
245
}
0 commit comments