File: AtomicIntegers.swift.gyb

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (243 lines) | stat: -rw-r--r-- 7,983 bytes parent folder | download
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2023-2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import Builtin

% from SwiftAtomics import *

% for U in ["", "U"]:
%  for bits in atomicBits:
%  intType = U + "Int" + bits
%  intStorage = "_Atomic" + bits + "BitStorage"

//===----------------------------------------------------------------------===//
// ${intType} AtomicRepresentable conformance
//===----------------------------------------------------------------------===//

%  if bits != "":
%   # Assume that everyone has word-sized atomics, but check for the rest
#if _hasAtomicBitWidth(_${bits})
%  end

@available(SwiftStdlib 6.0, *)
extension ${intType}: AtomicRepresentable {
  /// The storage representation type that `Self` encodes to and decodes from
  /// which is a suitable type when used in atomic operations.
%   if intType == "Int" or intType == "UInt":
#if _pointerBitWidth(_64)
  @available(SwiftStdlib 6.0, *)
  public typealias AtomicRepresentation = _Atomic64BitStorage
#elseif _pointerBitWidth(_32)
  @available(SwiftStdlib 6.0, *)
  public typealias AtomicRepresentation = _Atomic32BitStorage
#else
#error("Unsupported platform")
#endif
%   else:
  @available(SwiftStdlib 6.0, *)
  public typealias AtomicRepresentation = ${intStorage}
%   end

  /// Destroys a value of `Self` and prepares an `AtomicRepresentation` storage
  /// type to be used for atomic operations.
  ///
  /// - Note: This is not an atomic operation. This simply encodes the logical
  ///   type `Self` into its storage representation suitable for atomic
  ///   operations, `AtomicRepresentation`.
  ///
  /// - Parameter value: A valid instance of `Self` that's about to be destroyed
  ///   to encode an instance of its `AtomicRepresentation`.
  /// - Returns: The newly encoded `AtomicRepresentation` storage.
  @available(SwiftStdlib 6.0, *)
  @_alwaysEmitIntoClient
  @_transparent
  public static func encodeAtomicRepresentation(
    _ value: borrowing ${intType}
  ) -> AtomicRepresentation {
    AtomicRepresentation(value._value)
  }

  /// Recovers the logical atomic type `Self` by destroying some
  /// `AtomicRepresentation` storage instance returned from an atomic operation.
  ///
  /// - Note: This is not an atomic operation. This simply decodes the storage
  ///   representation used in atomic operations back into the logical type for
  ///   normal use, `Self`.
  ///
  /// - Parameter storage: The storage representation for `Self` that's used
  ///   within atomic operations.
  /// - Returns: The newly decoded logical type `Self`.
  @available(SwiftStdlib 6.0, *)
  @_alwaysEmitIntoClient
  @_transparent
  public static func decodeAtomicRepresentation(
    _ representation: consuming AtomicRepresentation
  ) -> ${intType} {
    ${intType}(representation._storage)
  }
}

//===----------------------------------------------------------------------===//
// ${intType} atomic operations
//===----------------------------------------------------------------------===//

@available(SwiftStdlib 6.0, *)
extension Atomic where Value == ${intType} {
% for (name, builtinName, op, doc) in integerOperations:
  /// Perform an atomic ${doc} operation and return the old and new value,
  /// applying the specified memory ordering.
  ///
%   if "Wrapping" in name:
  /// - Note: This operation silently wraps around on overflow, like the
  ///   `${op}` operator does on `${intType}` values.
  ///
%   end
  /// - Parameter operand: An integer value.
  /// - Parameter ordering: The memory ordering to apply on this operation.
  /// - Returns: A tuple containing the original value before the operation and
  ///   the new value after the operation.
  @available(SwiftStdlib 6.0, *)
  @discardableResult
  @_semantics("atomics.requires_constant_orderings")
  @_alwaysEmitIntoClient
  @_transparent
  public func ${lowerFirst(name)}(
    _ operand: ${intType},
    ordering: AtomicUpdateOrdering
  ) -> (oldValue: ${intType}, newValue: ${intType}) {
    let original = switch ordering {
%   for (nameOrder, _, _, llvmOrder, _) in updateOrderings:
    case .${nameOrder}:
%     if intType == "Int" or intType == "UInt":
#if _pointerBitWidth(_64)
      Builtin.atomicrmw_${atomicOperationName(intType, builtinName)}_${llvmOrder}_Int64(
        _rawAddress,
        operand._value
      )
#elseif _pointerBitWidth(_32)
      Builtin.atomicrmw_${atomicOperationName(intType, builtinName)}_${llvmOrder}_Int32(
        _rawAddress,
        operand._value
      )
#else
#error("Unsupported platform")
#endif
%     else:
      Builtin.atomicrmw_${atomicOperationName(intType, builtinName)}_${llvmOrder}_Int${bits}(
        _rawAddress,
        operand._value
      )
%     end
%   end

    default:
      Builtin.unreachable()
    }

    return (
      oldValue: ${intType}(original),
%   if name == "Min":
      newValue: Swift.min(${intType}(original), operand)
%   elif name == "Max":
      newValue: Swift.max(${intType}(original), operand)
%   else:
      newValue: ${intType}(original) ${op} operand
%   end
    )
  }
% end

  /// Perform an atomic add operation and return the old and new value,
  /// applying the specified memory ordering.
  ///
  /// - Note: This operation checks for overflow at runtime and will trap if an
  ///   overflow does occur. In `-Ounchecked` builds, overflow checking is not
  ///   performed.
  ///
  /// - Parameter operand: An integer value.
  /// - Parameter ordering: The memory ordering to apply on this operation.
  /// - Returns: A tuple containing the original value before the operation and
  ///   the new value after the operation.
  @available(SwiftStdlib 6.0, *)
  @discardableResult
  @_semantics("atomics.requires_constant_orderings")
  @_alwaysEmitIntoClient
  @_transparent
  public func add(
    _ operand: ${intType},
    ordering: AtomicUpdateOrdering
  ) -> (oldValue: ${intType}, newValue: ${intType}) {
    var result = (
      exchanged: false,
      original: load(ordering: .relaxed)
    )
    var new: ${intType}

    repeat {
      new = result.original + operand

      result = weakCompareExchange(
        expected: result.original,
        desired: new,
        ordering: ordering
      )
    } while !result.exchanged

    return (oldValue: result.original, newValue: new)
  }

  /// Perform an atomic subtract operation and return the old and new value,
  /// applying the specified memory ordering.
  ///
  /// - Note: This operation checks for overflow at runtime and will trap if an
  ///   overflow does occur. In `-Ounchecked` builds, overflow checking is not
  ///   performed.
  ///
  /// - Parameter operand: An integer value.
  /// - Parameter ordering: The memory ordering to apply on this operation.
  /// - Returns: A tuple containing the original value before the operation and
  ///   the new value after the operation.
  @available(SwiftStdlib 6.0, *)
  @discardableResult
  @_semantics("atomics.requires_constant_orderings")
  @_alwaysEmitIntoClient
  @_transparent
  public func subtract(
    _ operand: ${intType},
    ordering: AtomicUpdateOrdering
  ) -> (oldValue: ${intType}, newValue: ${intType}) {
    var result = (
      exchanged: false,
      original: load(ordering: .relaxed)
    )
    var new: ${intType}

    repeat {
      new = result.original - operand

      result = weakCompareExchange(
        expected: result.original,
        desired: new,
        ordering: ordering
      )
    } while !result.exchanged

    return (oldValue: result.original, newValue: new)
  }
}

%  if bits != "":
#endif // _hasAtomicBitWidth
%  end

% end
% end