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