|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Atomics open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2020 - 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 | + from gyb_utils import ( |
| 14 | + autogenerated_warning, integerOperations, lowerFirst, argLabel) |
| 15 | +}% |
| 16 | +${autogenerated_warning()} |
| 17 | + |
| 18 | +#if compiler(>=5.9) && $RawLayout |
| 19 | +extension Atomic where Value: AtomicInteger { |
| 20 | + % for (name, _, op, label, doc) in integerOperations: |
| 21 | + /// Perform an atomic ${doc} operation and return the original value, applying |
| 22 | + /// the specified memory ordering. |
| 23 | + /// |
| 24 | + % if "Wrapping" in name: |
| 25 | + /// Note: This operation silently wraps around on overflow, like the |
| 26 | + /// `${op}` operator does on `Int` values. |
| 27 | + /// |
| 28 | + % end |
| 29 | + /// - Parameter operand: An integer value. |
| 30 | + /// - Parameter ordering: The memory ordering to apply on this operation. |
| 31 | + /// - Returns: The original value before the operation. |
| 32 | + @_semantics("atomics.requires_constant_orderings") |
| 33 | + @_transparent @_alwaysEmitIntoClient |
| 34 | + public func loadThen${name}( |
| 35 | + ${label} operand: Value${" = 1" if "crement" in name else ""}, |
| 36 | + ordering: AtomicUpdateOrdering |
| 37 | + ) -> Value { |
| 38 | + _Storage.atomicLoadThen${name}( |
| 39 | + ${argLabel(label)}operand, |
| 40 | + at: _ptr, |
| 41 | + ordering: ordering) |
| 42 | + } |
| 43 | + |
| 44 | + % end |
| 45 | + % for (name, _, op, label, doc) in integerOperations: |
| 46 | + /// Perform an atomic ${doc} operation and return the new value, applying |
| 47 | + /// the specified memory ordering. |
| 48 | + /// |
| 49 | + % if "Wrapping" in name: |
| 50 | + /// Note: This operation silently wraps around on overflow, like the |
| 51 | + /// `${op}` operator does on `Int` values. |
| 52 | + /// |
| 53 | + % end |
| 54 | + /// - Parameter operand: An integer value. |
| 55 | + /// - Parameter ordering: The memory ordering to apply on this operation. |
| 56 | + /// - Returns: The new value after the operation. |
| 57 | + @_semantics("atomics.requires_constant_orderings") |
| 58 | + @_transparent @_alwaysEmitIntoClient |
| 59 | + public func ${lowerFirst(name)}ThenLoad( |
| 60 | + ${label} operand: Value${" = 1" if "crement" in name else ""}, |
| 61 | + ordering: AtomicUpdateOrdering |
| 62 | + ) -> Value { |
| 63 | + let original = _Storage.atomicLoadThen${name}( |
| 64 | + ${argLabel(label)}operand, |
| 65 | + at: _ptr, |
| 66 | + ordering: ordering) |
| 67 | + return original ${op} operand |
| 68 | + } |
| 69 | + |
| 70 | + % end |
| 71 | + /// Perform an atomic wrapping increment operation applying the |
| 72 | + /// specified memory ordering. |
| 73 | + /// |
| 74 | + /// Note: This operation silently wraps around on overflow, like the |
| 75 | + /// `&+=` operator does on `Int` values. |
| 76 | + /// |
| 77 | + /// - Parameter operand: The value to add to the current value. |
| 78 | + /// - Parameter ordering: The memory ordering to apply on this operation. |
| 79 | + @_semantics("atomics.requires_constant_orderings") |
| 80 | + @_transparent @_alwaysEmitIntoClient |
| 81 | + public func wrappingIncrement( |
| 82 | + by operand: Value = 1, |
| 83 | + ordering: AtomicUpdateOrdering |
| 84 | + ) { |
| 85 | + _ = _Storage.atomicLoadThenWrappingIncrement( |
| 86 | + by: operand, |
| 87 | + at: _ptr, |
| 88 | + ordering: ordering) |
| 89 | + } |
| 90 | + |
| 91 | + /// Perform an atomic wrapping decrement operation applying the |
| 92 | + /// specified memory ordering. |
| 93 | + /// |
| 94 | + /// Note: This operation silently wraps around on overflow, like the |
| 95 | + /// `&-=` operator does on `Int` values. |
| 96 | + /// |
| 97 | + /// - Parameter operand: The value to subtract from the current value. |
| 98 | + /// - Parameter ordering: The memory ordering to apply on this operation. |
| 99 | + @_semantics("atomics.requires_constant_orderings") |
| 100 | + @_transparent @_alwaysEmitIntoClient |
| 101 | + public func wrappingDecrement( |
| 102 | + by operand: Value = 1, |
| 103 | + ordering: AtomicUpdateOrdering |
| 104 | + ) { |
| 105 | + _ = _Storage.atomicLoadThenWrappingDecrement( |
| 106 | + by: operand, |
| 107 | + at: _ptr, |
| 108 | + ordering: ordering) |
| 109 | + } |
| 110 | +} |
| 111 | +#endif |
| 112 | + |
| 113 | +extension UnsafeAtomic where Value: AtomicInteger { |
| 114 | + % for (name, _, op, label, doc) in integerOperations: |
| 115 | + /// Perform an atomic ${doc} operation and return the original value, applying |
| 116 | + /// the specified memory ordering. |
| 117 | + /// |
| 118 | + % if "Wrapping" in name: |
| 119 | + /// Note: This operation silently wraps around on overflow, like the |
| 120 | + /// `${op}` operator does on `Int` values. |
| 121 | + /// |
| 122 | + % end |
| 123 | + /// - Parameter operand: An integer value. |
| 124 | + /// - Parameter ordering: The memory ordering to apply on this operation. |
| 125 | + /// - Returns: The original value before the operation. |
| 126 | + @_semantics("atomics.requires_constant_orderings") |
| 127 | + @_transparent @_alwaysEmitIntoClient |
| 128 | + public func loadThen${name}( |
| 129 | + ${label} operand: Value${" = 1" if "crement" in name else ""}, |
| 130 | + ordering: AtomicUpdateOrdering |
| 131 | + ) -> Value { |
| 132 | + _Storage.atomicLoadThen${name}( |
| 133 | + ${argLabel(label)}operand, |
| 134 | + at: _ptr, |
| 135 | + ordering: ordering) |
| 136 | + } |
| 137 | + |
| 138 | + % end |
| 139 | + % for (name, _, op, label, doc) in integerOperations: |
| 140 | + /// Perform an atomic ${doc} operation and return the new value, applying |
| 141 | + /// the specified memory ordering. |
| 142 | + /// |
| 143 | + % if "Wrapping" in name: |
| 144 | + /// Note: This operation silently wraps around on overflow, like the |
| 145 | + /// `${op}` operator does on `Int` values. |
| 146 | + /// |
| 147 | + % end |
| 148 | + /// - Parameter operand: An integer value. |
| 149 | + /// - Parameter ordering: The memory ordering to apply on this operation. |
| 150 | + /// - Returns: The new value after the operation. |
| 151 | + @_semantics("atomics.requires_constant_orderings") |
| 152 | + @_transparent @_alwaysEmitIntoClient |
| 153 | + public func ${lowerFirst(name)}ThenLoad( |
| 154 | + ${label} operand: Value${" = 1" if "crement" in name else ""}, |
| 155 | + ordering: AtomicUpdateOrdering |
| 156 | + ) -> Value { |
| 157 | + let original = _Storage.atomicLoadThen${name}( |
| 158 | + ${argLabel(label)}operand, |
| 159 | + at: _ptr, |
| 160 | + ordering: ordering) |
| 161 | + return original ${op} operand |
| 162 | + } |
| 163 | + |
| 164 | + % end |
| 165 | + |
| 166 | + /// Perform an atomic wrapping increment operation applying the |
| 167 | + /// specified memory ordering. |
| 168 | + /// |
| 169 | + /// Note: This operation silently wraps around on overflow, like the |
| 170 | + /// `&+=` operator does on `Int` values. |
| 171 | + /// |
| 172 | + /// - Parameter operand: The value to add to the current value. |
| 173 | + /// - Parameter ordering: The memory ordering to apply on this operation. |
| 174 | + @_semantics("atomics.requires_constant_orderings") |
| 175 | + @_transparent @_alwaysEmitIntoClient |
| 176 | + public func wrappingIncrement( |
| 177 | + by operand: Value = 1, |
| 178 | + ordering: AtomicUpdateOrdering |
| 179 | + ) { |
| 180 | + _ = _Storage.atomicLoadThenWrappingIncrement( |
| 181 | + by: operand, |
| 182 | + at: _ptr, |
| 183 | + ordering: ordering) |
| 184 | + } |
| 185 | + |
| 186 | + /// Perform an atomic wrapping decrement operation applying the |
| 187 | + /// specified memory ordering. |
| 188 | + /// |
| 189 | + /// Note: This operation silently wraps around on overflow, like the |
| 190 | + /// `&-=` operator does on `Int` values. |
| 191 | + /// |
| 192 | + /// - Parameter operand: The value to subtract from the current value. |
| 193 | + /// - Parameter ordering: The memory ordering to apply on this operation. |
| 194 | + @_semantics("atomics.requires_constant_orderings") |
| 195 | + @_transparent @_alwaysEmitIntoClient |
| 196 | + public func wrappingDecrement( |
| 197 | + by operand: Value = 1, |
| 198 | + ordering: AtomicUpdateOrdering |
| 199 | + ) { |
| 200 | + _ = _Storage.atomicLoadThenWrappingDecrement( |
| 201 | + by: operand, |
| 202 | + at: _ptr, |
| 203 | + ordering: ordering) |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +extension ManagedAtomic where Value: AtomicInteger { |
| 208 | + % for (name, _, op, label, doc) in integerOperations: |
| 209 | + /// Perform an atomic ${doc} operation and return the original value, applying |
| 210 | + /// the specified memory ordering. |
| 211 | + /// |
| 212 | + % if "Wrapping" in name: |
| 213 | + /// Note: This operation silently wraps around on overflow, like the |
| 214 | + /// `${op}` operator does on `Int` values. |
| 215 | + /// |
| 216 | + % end |
| 217 | + /// - Parameter operand: An integer value. |
| 218 | + /// - Parameter ordering: The memory ordering to apply on this operation. |
| 219 | + /// - Returns: The original value before the operation. |
| 220 | + @_semantics("atomics.requires_constant_orderings") |
| 221 | + @_transparent @_alwaysEmitIntoClient |
| 222 | + public func loadThen${name}( |
| 223 | + ${label} operand: Value${" = 1" if "crement" in name else ""}, |
| 224 | + ordering: AtomicUpdateOrdering |
| 225 | + ) -> Value { |
| 226 | + _storage.loadThen${name}( |
| 227 | + ${argLabel(label)}operand, |
| 228 | + ordering: ordering) |
| 229 | + } |
| 230 | + |
| 231 | + % end |
| 232 | + |
| 233 | + % for (name, _, op, label, doc) in integerOperations: |
| 234 | + /// Perform an atomic ${doc} operation and return the new value, applying |
| 235 | + /// the specified memory ordering. |
| 236 | + /// |
| 237 | + % if "Wrapping" in name: |
| 238 | + /// Note: This operation silently wraps around on overflow, like the |
| 239 | + /// `${op}` operator does on `Int` values. |
| 240 | + /// |
| 241 | + % end |
| 242 | + /// - Parameter operand: An integer value. |
| 243 | + /// - Parameter ordering: The memory ordering to apply on this operation. |
| 244 | + /// - Returns: The new value after the operation. |
| 245 | + @_semantics("atomics.requires_constant_orderings") |
| 246 | + @_transparent @_alwaysEmitIntoClient |
| 247 | + public func ${lowerFirst(name)}ThenLoad( |
| 248 | + ${label} operand: Value${" = 1" if "crement" in name else ""}, |
| 249 | + ordering: AtomicUpdateOrdering |
| 250 | + ) -> Value { |
| 251 | + _storage.${lowerFirst(name)}ThenLoad( |
| 252 | + ${argLabel(label)}operand, |
| 253 | + ordering: ordering) |
| 254 | + } |
| 255 | + |
| 256 | + % end |
| 257 | + |
| 258 | + /// Perform an atomic wrapping increment operation applying the |
| 259 | + /// specified memory ordering. |
| 260 | + /// |
| 261 | + /// Note: This operation silently wraps around on overflow, like the |
| 262 | + /// `&+=` operator does on `Int` values. |
| 263 | + /// |
| 264 | + /// - Parameter operand: The value to add to the current value. |
| 265 | + /// - Parameter ordering: The memory ordering to apply on this operation. |
| 266 | + @_semantics("atomics.requires_constant_orderings") |
| 267 | + @_transparent @_alwaysEmitIntoClient |
| 268 | + public func wrappingIncrement( |
| 269 | + by operand: Value = 1, |
| 270 | + ordering: AtomicUpdateOrdering |
| 271 | + ) { |
| 272 | + _ = _storage.loadThenWrappingIncrement( |
| 273 | + by: operand, |
| 274 | + ordering: ordering) |
| 275 | + } |
| 276 | + |
| 277 | + /// Perform an atomic wrapping decrement operation applying the |
| 278 | + /// specified memory ordering. |
| 279 | + /// |
| 280 | + /// Note: This operation silently wraps around on overflow, like the |
| 281 | + /// `&-=` operator does on `Int` values. |
| 282 | + /// |
| 283 | + /// - Parameter operand: The value to subtract from the current value. |
| 284 | + /// - Parameter ordering: The memory ordering to apply on this operation. |
| 285 | + @_semantics("atomics.requires_constant_orderings") |
| 286 | + @_transparent @_alwaysEmitIntoClient |
| 287 | + public func wrappingDecrement( |
| 288 | + by operand: Value = 1, |
| 289 | + ordering: AtomicUpdateOrdering |
| 290 | + ) { |
| 291 | + _ = _storage.loadThenWrappingDecrement( |
| 292 | + by: operand, |
| 293 | + ordering: ordering) |
| 294 | + } |
| 295 | +} |
0 commit comments