File: AtomicRepresentable.swift

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 (365 lines) | stat: -rw-r--r-- 14,961 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2023 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
//
//===----------------------------------------------------------------------===//

/// A type that supports atomic operations through a separate atomic storage
/// representation.
///
/// Types that conform to the `AtomicRepresentable` protocol can be used as the
/// `Value` type parameter with the `Atomic` type. Conformances that utilize
/// existing atomic storage representations as their own representation will get
/// the primitive atomic operations available on `Atomic` for free. Such
/// operations include `load`, `store`, `exchange`, `compareExchange`, and
/// `weakCompareExchange`.
///
/// Conforming to the AtomicRepresentable protocol
/// --------------------------------------
///
/// Conforming your own custom types allow them to be used in the `Atomic` type
/// and get access to all of the primitive atomic operations explained above.
/// There are two main ways to conform your type to `AtomicRepresentable`:
///
/// 1. Using a predefined `RawRepresentable` conformance
/// 2. Manually conforming to `AtomicRepresentable`
///
/// If you custom type already conforms to `RawRepresentable`, then adding the
/// `AtomicRepresentable` conformance may be really simple! If the `RawValue`
/// associated type of your type is already itself an `AtomicRepresentable`,
/// then all you need to do is add the conformance and you're done!
///
///     enum TrafficLight: UInt8 {
///       case red
///       case yellow
///       case green
///     }
///
///     extension TrafficLight: AtomicRepresentable {}
///
/// And that's it! Here, we're utilizing Swift's automatic `RawRepresentable`
/// conformance synthesis for enums by declaring our "raw value" to be a
/// `UInt8`. By adding the `AtomicRepresentable` conformance, we automatically
/// figure out how to do the conformance from the `RawRepresentable`
/// implementation and do all of th necessary work for you. However, it is still
/// possible to customize this behavior using the manual method explained below.
///
/// Defining your own `AtomicRepresentable` conformance is pretty simple. All
/// you have to do is decide what atomic storage representation fits best for
/// your type, and create the bidirectional relationship between the two.
///
///     // A point in an x-y coordinate system.
///     struct GridPoint {
///       var x: Int
///       var y: Int
///     }
///
///     extension GridPoint: AtomicRepresentable {
///       typealias AtomicRepresentation = WordPair.AtomicRepresentation
///
///       static func encodeAtomicRepresentation(
///         _ value: consuming GridPoint
///       ) -> AtomicRepresentation {
///         let wordPair = WordPair(
///           first: UInt(bitPattern: value.x),
///           second: UInt(bitPattern: value.y)
///         )
///
///         return WordPair.encodeAtomicRepresentation(wordPair)
///       }
///
///       static func decodeAtomicRepresentation(
///         _ representation: consuming AtomicRepresentation
///       ) -> GridPoint {
///         let wordPair = WordPair.decodeAtomicRepresentation(representation)
///
///         return GridPoint(
///           x: Int(bitPattern: wordPair.first),
///           y: Int(bitPattern: wordPair.second)
///         )
///       }
///     }
///
/// Here, we're going to select `WordPair`'s atomic storage representation as
/// our own. This is very important because we only get the atomic operations
/// like `load` and `store` if our representation is one of the _fundamental_
/// storage representations. Luckily for us, `WordPair` does use one of these
/// types as its storage type.
///
/// In addition to selecting what storage representation our type will use, we
/// define two static functions that go from both our custom type to its
/// representation and the representation back to our own type. Because our
/// representation is the same as `WordPair.AtomicRepresentation`, we will
/// actually go through `WordPair`'s `AtomicRepresentable` conformance to help
/// define our own.
///
/// This is all you need to do to conform your custom type to the
/// `AtomicRepresentable` protocol. From here, you can use this type in all of
/// the primitive atomic operations like shown below:
///
///     func atomicGridPoint(_ gridPoint: Atomic<GridPoint>) {
///       let newGridPoint = GridPoint(x: 123, y: -456)
///
///       let oldGridPoint1 = gridPoint.load(ordering: .relaxed)
///
///       gridPoint.store(newGridPoint, ordering: .releasing)
///
///       let oldGridPoint2 = gridPoint.exchange(
///         desired: oldGridPoint1,
///         ordering: .acquiringAndReleasing
///       )
///
///       let (exchanged1, oldGridPoint2) = gridPoint.compareExchange(
///         expected: oldGridPoint1,
///         desired: newGridPoint,
///         ordering: .sequentiallyConsistent
///       )
///
///       let (exchanged2, oldGridPoint3) = gridPoint.weakCompareExchange(
///         expected: newGridPoint,
///         desired: oldGridPoint2,
///         ordering: .relaxed
///       )
///     }
///
/// List of Fundamental Atomic Representations
/// ------------------------------------------
///
/// When defining your own `AtomicRepresentable` conformance, it is critical
/// that your custom type should choose from the following list of types as its
/// own `AtomicRepresentation`:
///
/// - `UInt8.AtomicRepresentation`
/// - `UInt16.AtomicRepresentation`
/// - `UInt32.AtomicRepresentation`
/// - `UInt64.AtomicRepresentation`
/// - `UInt.AtomicRepresentation`
/// - `Int8.AtomicRepresentation`
/// - `Int16.AtomicRepresentation`
/// - `Int32.AtomicRepresentation`
/// - `Int64.AtomicRepresentation`
/// - `Int.AtomicRepresentation`
/// - `WordPair.AtomicRepresentation`
///
/// - Note: `Int8.AtomicRepresentation` is the same type as
///   `UInt8.AtomicRepresentation` and the same is true for all of the same
///   sized integer types. If your type wraps an unsigned integer, you should
///   prefer to use an unsigned integer's atomic representation instead of a
///   signed ones and vice versa. `Int` and `UInt`'s representation will be
///   64 bits wide on 64 bit systems and 32 bit wide on 32 bit systems. `Int64`
///   and `UInt64` always conform to `AtomicRepresentable` on 64 bit systems,
///   but on 32 bit systems they will only conform if the platform supports
///   double wide atomics. `WordPair` will only conform to `AtomicRepresentable`
///   on platforms that support double wide atomics, but if they do it will be
///   128 bits wide on 64 bit systems and 64 bits wide on 32 bit systems.
///
@available(SwiftStdlib 6.0, *)
public protocol AtomicRepresentable {
  /// The storage representation type that `Self` encodes to and decodes from
  /// which is a suitable type when used in atomic operations.
  associatedtype AtomicRepresentation: BitwiseCopyable

  /// 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.
  static func encodeAtomicRepresentation(
    _ value: consuming Self
  ) -> AtomicRepresentation

  /// 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`.
  static func decodeAtomicRepresentation(
    _ storage: consuming AtomicRepresentation
  ) -> Self
}

//===----------------------------------------------------------------------===//
// RawRepresentable AtomicRepresentable conformance
//===----------------------------------------------------------------------===//

@available(SwiftStdlib 6.0, *)
extension RawRepresentable
where
  Self: AtomicRepresentable,
  RawValue: AtomicRepresentable
{
  /// The storage representation type that `Self` encodes to and decodes from
  /// which is a suitable type when used in atomic operations.
  @available(SwiftStdlib 6.0, *)
  public typealias AtomicRepresentation = RawValue.AtomicRepresentation


  /// 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: consuming Self
  ) -> RawValue.AtomicRepresentation {
    RawValue.encodeAtomicRepresentation(value.rawValue)
  }


  /// 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 RawValue.AtomicRepresentation
  ) -> Self {
    Self(rawValue: RawValue.decodeAtomicRepresentation(representation))!
  }
}

//===----------------------------------------------------------------------===//
// Never AtomicRepresentable conformance
//===----------------------------------------------------------------------===//

@available(SwiftStdlib 6.0, *)
extension Never: AtomicRepresentable {
  /// The storage representation type that `Self` encodes to and decodes from
  /// which is a suitable type when used in atomic operations.
  @available(SwiftStdlib 6.0, *)
  public typealias AtomicRepresentation = Never


  /// 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: consuming Never
  ) -> Never {}


  /// 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 Never
  ) -> Never {}
}

//===----------------------------------------------------------------------===//
// Duration AtomicRepresentable conformance
//===----------------------------------------------------------------------===//

#if _pointerBitWidth(_64) && _hasAtomicBitWidth(_128)

@available(SwiftStdlib 6.0, *)
extension Duration: AtomicRepresentable {
  /// The storage representation type that `Self` encodes to and decodes from
  /// which is a suitable type when used in atomic operations.
  @available(SwiftStdlib 6.0, *)
  public typealias AtomicRepresentation = WordPair.AtomicRepresentation


  /// 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: consuming Duration
  ) -> AtomicRepresentation {
    WordPair.encodeAtomicRepresentation(
      WordPair(
        first: UInt(truncatingIfNeeded: value._high),
        second: UInt(truncatingIfNeeded: value._low)
      )
    )
  }


  /// 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
  ) -> Duration {
    let wp = WordPair.decodeAtomicRepresentation(representation)

    return Duration(
      _high: Int64(truncatingIfNeeded: wp.first),
      low: UInt64(truncatingIfNeeded: wp.second)
    )
  }
}

#endif