File: AtomicReference.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 (595 lines) | stat: -rw-r--r-- 20,460 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 - 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
//
//===----------------------------------------------------------------------===//

#if !ATOMICS_NATIVE_BUILTINS
import _AtomicsShims
#endif

/// A class type that supports atomic strong references.
///
///     class MyObject: AtomicReference {}
///
///     let object = MyObject()
///     let ref = ManagedAtomic<MyObject>(object)
///
///     ref.load(ordering: .relaxed) // Returns `object`.
///
/// The conforming class is allowed to be non-final, but `ManagedAtomic` and
/// `UnsafeAtomic` do not support using a subclass as their generic argument --
/// the type of an atomic reference must be precisely the same class that
/// originally conformed to the protocol.
///
///
///     class Derived: MyObject {}
///
///     let ref2: ManagedAtomic<Derived>
///     // error: 'ManagedAtomic' requires the types 'Derived' and 'Base' be equivalent
///
/// Note that this limitation only affects the static type of the
/// `ManagedAtomic`/`UnsafeAtomic` variables. Such references still fully
/// support holding instances of subclasses of the conforming class. (Returned
/// may be downcasted from the base type after an `is` check.)
///
///     let child = Derived()
///     ref.store(child, ordering: .relaxed) // OK!
///     let value = ref.load(ordering: .relaxed)
///     // `value` is a variable of type `MyObject`, holding a `Derived` instance.
///     print(value is Derived) // Prints "true"
///
public protocol AtomicReference: AnyObject, AtomicOptionalWrappable
where
  AtomicRepresentation == AtomicReferenceStorage<_AtomicBase>,
  AtomicOptionalRepresentation == AtomicOptionalReferenceStorage<_AtomicBase>
{
  /// This is a utility type that enables non-final classes to conform to
  /// `AtomicReference`.
  ///
  /// This associated type must be left at its default value, `Self`.
  /// `ManagedAtomic` et al. currently require that `Self == _AtomicBase`,
  /// so conformances that set this to anything else will technically build,
  /// but they will not be very practical.
  ///
  /// Ideally we could just require that `Self` should be a subtype of
  /// `AtomicRepresentation.Value`; however we have no good way to
  /// express that requirement.
  ///
  ///     protocol AtomicValue where Self: AtomicRepresentation.Value {
  ///       associatedtype AtomicRepresentation: AtomicStorage
  ///     }
  ///
  /// See https://github.com/apple/swift-atomics/issues/53 for more details.
  associatedtype _AtomicBase: AnyObject = Self
}

/// The maximum number of other threads that can start accessing a
/// strong reference before an in-flight update needs to cancel and
/// retry.
@inlinable @inline(__always)
internal var _concurrencyWindow: Int { 20 }

extension DoubleWord {
  fileprivate init(_raw: UnsafeRawPointer?, readers: Int, version: Int) {
    let r = UInt(bitPattern: readers) & Self._readersMask
    assert(r == readers)
    self.init(
      first: UInt(bitPattern: _raw),
      second: r | (UInt(bitPattern: version) &<< Self._readersBitWidth))
  }

  @inline(__always)
  fileprivate var _raw: UnsafeMutableRawPointer? {
    get { UnsafeMutableRawPointer(bitPattern: first) }
    set { first = UInt(bitPattern: newValue) }
  }

  @inline(__always)
  fileprivate var _unmanaged: Unmanaged<AnyObject>? {
    guard let raw = _raw else { return nil }
    return .fromOpaque(raw)
  }

  @inline(__always)
  fileprivate static var _readersBitWidth: Int {
    // This reserves 8 bits for the accesses-in-flight counter on 32-bit
    // systems, and 16 bits on 64-bit systems.
    Int.bitWidth / 4
  }

  @inline(__always)
  fileprivate static var _readersMask: UInt { (1 &<< _readersBitWidth) - 1  }

  @inline(__always)
  fileprivate var _readers: Int {
    get { Int(bitPattern: second & Self._readersMask) }
    set {
      let n = UInt(bitPattern: newValue) & Self._readersMask
      assert(n == newValue)
      second = (second & ~Self._readersMask) | n
    }
  }

  @inline(__always)
  fileprivate var _version: Int {
    get { Int(bitPattern: second &>> Self._readersBitWidth) }
    set {
      // Silently truncate any high bits we cannot store.
      second = (
        (UInt(bitPattern: newValue) &<< Self._readersBitWidth) |
        second & Self._readersMask)
    }
  }
}

extension Optional where Wrapped == AnyObject {
  fileprivate var _raw: UnsafeMutableRawPointer? {
    guard let value = self else { return nil }
    return Unmanaged.passUnretained(value).toOpaque()
  }
}

extension UnsafeMutablePointer where Pointee == _AtomicReferenceStorage {
  @inlinable @inline(__always)
  internal var _extract: UnsafeMutablePointer<DoubleWord.AtomicRepresentation> {
    UnsafeMutableRawPointer(self)
      .assumingMemoryBound(to: DoubleWord.AtomicRepresentation.self)
  }
}

@usableFromInline
internal struct _AtomicReferenceStorage {
  internal typealias Storage = DoubleWord.AtomicRepresentation
  internal var _storage: Storage

  @usableFromInline
  internal init(_ value: __owned AnyObject?) {
    let dword = DoubleWord(
      _raw: Unmanaged.passRetained(value)?.toOpaque(),
      readers: 0,
      version: 0)
    _storage = Storage(dword)
  }

  @usableFromInline
  internal func dispose() -> AnyObject? {
    let value = _storage.dispose()
    precondition(value._readers == 0,
      "Attempt to dispose of a busy atomic strong reference \(value)")
    return value._unmanaged?.takeRetainedValue()
  }

  private static func _startLoading(
    from pointer: UnsafeMutablePointer<Self>,
    hint: DoubleWord? = nil
  ) -> DoubleWord {
    var old = hint ?? Storage.atomicLoad(at: pointer._extract, ordering: .relaxed)
    if old._raw == nil {
      atomicMemoryFence(ordering: .acquiring)
      return old
    }
    // Increment reader count
    while true {
      let new = DoubleWord(
        _raw: old._raw,
        readers: old._readers &+ 1,
        version: old._version)
      var done: Bool
      (done, old) = Storage.atomicWeakCompareExchange(
        expected: old,
        desired: new,
        at: pointer._extract,
        successOrdering: .acquiring,
        failureOrdering: .acquiring)
      if done { return new }
      if old._raw == nil { return old }
    }
  }

  private static func _finishLoading(
    _ value: DoubleWord,
    from pointer: UnsafeMutablePointer<Self>
  ) -> AnyObject? {
    if value._raw == nil { return nil }

    // Retain result before we exit the access.
    let result = value._unmanaged!.takeUnretainedValue()

    // Decrement reader count, using the version number to prevent any
    // ABA issues.
    var current = value
    var done = false
    repeat {
      assert(current._readers >= 1)
      (done, current) = Storage.atomicWeakCompareExchange(
        expected: current,
        desired: DoubleWord(
          _raw: current._raw,
          readers: current._readers &- 1,
          version: current._version),
        at: pointer._extract,
        successOrdering: .acquiringAndReleasing,
        failureOrdering: .acquiring)
    } while !done && current._raw == value._raw && current._version == value._version
    if !done {
      // The reference changed while we were loading it. Cancel out
      // our part of the refcount bias for the loaded instance.
      value._unmanaged!.release()
    }
    return result
  }

  @usableFromInline
  internal static func atomicLoad(
    at pointer: UnsafeMutablePointer<Self>
  ) -> AnyObject? {
    let new = _startLoading(from: pointer)
    return _finishLoading(new, from: pointer)
  }

  /// Try updating the current value from `old` to `new`. Don't do anything if the
  /// current value differs from `old`.
  ///
  /// Returns a tuple `(exchanged, original)` where `exchanged` indicates whether the
  /// update was successful. If `exchange` true, then `original` contains the original
  /// reference value; otherwise `original` is nil.
  ///
  /// On an unsuccessful exchange, this function updates `old` to the latest known value,
  /// and reenters the current thread as a reader of it.
  private static func _tryExchange(
    old: inout DoubleWord,
    new: Unmanaged<AnyObject>?,
    at pointer: UnsafeMutablePointer<Self>
  ) -> (exchanged: Bool, original: AnyObject?) {
    let new = DoubleWord(_raw: new?.toOpaque(), readers: 0, version: old._version &+ 1)
    guard let ref = old._unmanaged else {
      // Try replacing the current nil value with the desired new value.
      let (done, current) = Storage.atomicCompareExchange(
        expected: old,
        desired: new,
        at: pointer._extract,
        ordering: .acquiringAndReleasing)
      if done {
        return (true, nil)
      }
      // Someone else changed the reference. Give up for now.
      old = _startLoading(from: pointer, hint: current)
      return (false, nil)
    }
    assert(old._readers >= 1)
    var delta = old._readers + _concurrencyWindow
    ref.retain(by: delta)
    while true {
      // Try replacing the current value with the desired new value.
      let (done, current) = Storage.atomicCompareExchange(
        expected: old,
        desired: new,
        at: pointer._extract,
        ordering: .acquiringAndReleasing)
      if done {
        // Successfully replaced the object. Clean up extra retains.
        assert(current._readers == old._readers)
        assert(current._raw == old._raw)
        assert(current._readers <= delta)
        ref.release(by: delta - current._readers + 1) // +1 is for our own role as a reader.
        return (true, ref.takeRetainedValue())
      }
      if current._version != old._version {
        // Someone else changed the reference. Give up for now.
        ref.release(by: delta + 1) // +1 covers our reader bias
        old = _startLoading(from: pointer, hint: current)
        return (false, nil)
      }
      // Some readers entered or left while we were processing things. Try again.
      assert(current._raw == old._raw)
      assert(current._raw != nil)
      assert(current._readers >= 1)
      if current._readers > delta {
        // We need to do more retains to cover readers.
        let d = current._readers + _concurrencyWindow
        ref.retain(by: d - delta)
        delta = d
      }
      old = current
    }
  }

  @usableFromInline
  internal static func atomicExchange(
    _ desired: __owned AnyObject?,
    at pointer: UnsafeMutablePointer<Self>
  ) -> AnyObject? {
    let new = Unmanaged.passRetained(desired)
    var old = _startLoading(from: pointer)
    var (exchanged, original) = _tryExchange(old: &old, new: new, at: pointer)
    while !exchanged {
      (exchanged, original) = _tryExchange(old: &old, new: new, at: pointer)
    }
    return original
  }

  @usableFromInline
  internal static func atomicCompareExchange(
    expected: AnyObject?,
    desired: __owned AnyObject?,
    at pointer: UnsafeMutablePointer<Self>
  ) -> (exchanged: Bool, original: AnyObject?) {
    let expectedRaw = expected._raw
    let new = Unmanaged.passRetained(desired)
    var old = _startLoading(from: pointer)
    while old._raw == expectedRaw {
      let (exchanged, original) = _tryExchange(old: &old, new: new, at: pointer)
      if exchanged {
        assert(original === expected)
        return (true, original)
      }
    }
    // We did not find the expected value. Cancel the retain of the new value
    // and return the old value.
    new?.release()
    return (false, _finishLoading(old, from: pointer))
  }
}

@frozen
public struct AtomicReferenceStorage<Value: AnyObject> {
  @usableFromInline
  internal var _storage: _AtomicReferenceStorage

  @inlinable
  public init(_ value: __owned Value) {
    _storage = .init(value)
  }

  @inlinable
  public func dispose() -> Value {
    return unsafeDowncast(_storage.dispose()!, to: Value.self)
  }
}

extension AtomicReferenceStorage {
  @inlinable @inline(__always)
  @_alwaysEmitIntoClient
  static func _extract(
    _ ptr: UnsafeMutablePointer<Self>
  ) -> UnsafeMutablePointer<_AtomicReferenceStorage> {
    // `Self` is layout-compatible with its only stored property.
    return UnsafeMutableRawPointer(ptr)
      .assumingMemoryBound(to: _AtomicReferenceStorage.self)
  }
}

extension AtomicReferenceStorage: AtomicStorage {
  @inlinable @inline(__always)
  @_alwaysEmitIntoClient
  @_semantics("atomics.requires_constant_orderings")
  public static func atomicLoad(
    at pointer: UnsafeMutablePointer<Self>,
    ordering: AtomicLoadOrdering
  ) -> Value {
    // FIXME: All orderings are treated as acquiring-and-releasing.
    let result = _AtomicReferenceStorage.atomicLoad(at: Self._extract(pointer))
    return unsafeDowncast(result!, to: Value.self)
  }

  @inlinable @inline(__always)
  @_alwaysEmitIntoClient
  @_semantics("atomics.requires_constant_orderings")
  public static func atomicStore(
    _ desired: __owned Value,
    at pointer: UnsafeMutablePointer<Self>,
    ordering: AtomicStoreOrdering
  ) {
    // FIXME: All orderings are treated as acquiring-and-releasing.
    _ = _AtomicReferenceStorage.atomicExchange(
      desired,
      at: _extract(pointer))
  }

  @inlinable @inline(__always)
  @_alwaysEmitIntoClient
  @_semantics("atomics.requires_constant_orderings")
  public static func atomicExchange(
    _ desired: __owned Value,
    at pointer: UnsafeMutablePointer<Self>,
    ordering: AtomicUpdateOrdering
  ) -> Value {
    // FIXME: All orderings are treated as acquiring-and-releasing.
    let result = _AtomicReferenceStorage.atomicExchange(
      desired,
      at: _extract(pointer))
    return unsafeDowncast(result!, to: Value.self)
  }

  @inlinable @inline(__always)
  @_alwaysEmitIntoClient
  @_semantics("atomics.requires_constant_orderings")
  public static func atomicCompareExchange(
    expected: Value,
    desired: __owned Value,
    at pointer: UnsafeMutablePointer<Self>,
    ordering: AtomicUpdateOrdering
  ) -> (exchanged: Bool, original: Value) {
    // FIXME: All orderings are treated as acquiring-and-releasing.
    let result = _AtomicReferenceStorage.atomicCompareExchange(
      expected: expected,
      desired: desired,
      at: _extract(pointer))
    return (result.exchanged, unsafeDowncast(result.original!, to: Value.self))
  }

  @inlinable @inline(__always)
  @_alwaysEmitIntoClient
  @_semantics("atomics.requires_constant_orderings")
  public static func atomicCompareExchange(
    expected: Value,
    desired: __owned Value,
    at pointer: UnsafeMutablePointer<Self>,
    successOrdering: AtomicUpdateOrdering,
    failureOrdering: AtomicLoadOrdering
  ) -> (exchanged: Bool, original: Value) {
    // FIXME: All orderings are treated as acquiring-and-releasing.
    let result = _AtomicReferenceStorage.atomicCompareExchange(
      expected: expected,
      desired: desired,
      at: _extract(pointer))
    return (result.exchanged, unsafeDowncast(result.original!, to: Value.self))
  }

  @inlinable @inline(__always)
  @_alwaysEmitIntoClient
  @_semantics("atomics.requires_constant_orderings")
  public static func atomicWeakCompareExchange(
    expected: Value,
    desired: __owned Value,
    at pointer: UnsafeMutablePointer<Self>,
    successOrdering: AtomicUpdateOrdering,
    failureOrdering: AtomicLoadOrdering
  ) -> (exchanged: Bool, original: Value) {
    // FIXME: All orderings are treated as acquiring-and-releasing.
    let result = _AtomicReferenceStorage.atomicCompareExchange(
      expected: expected,
      desired: desired,
      at: _extract(pointer))
    return (result.exchanged, unsafeDowncast(result.original!, to: Value.self))
  }
}

@frozen
public struct AtomicOptionalReferenceStorage<Instance: AnyObject> {
  @usableFromInline
  internal var _storage: _AtomicReferenceStorage

  @inlinable
  public init(_ value: __owned Instance?) {
    _storage = .init(value)
  }

  @inlinable
  public func dispose() -> Instance? {
    guard let value = _storage.dispose() else { return nil }
    return unsafeDowncast(value, to: Instance.self)
  }
}

extension AtomicOptionalReferenceStorage {
  @inlinable @inline(__always)
  @_alwaysEmitIntoClient
  static func _extract(
    _ ptr: UnsafeMutablePointer<Self>
  ) -> UnsafeMutablePointer<_AtomicReferenceStorage> {
    // `Self` is layout-compatible with its only stored property.
    return UnsafeMutableRawPointer(ptr)
      .assumingMemoryBound(to: _AtomicReferenceStorage.self)
  }
}

extension AtomicOptionalReferenceStorage: AtomicStorage {
  public typealias Value = Instance?

  @inlinable @inline(__always)
  @_alwaysEmitIntoClient
  @_semantics("atomics.requires_constant_orderings")
  public static func atomicLoad(
    at pointer: UnsafeMutablePointer<Self>,
    ordering: AtomicLoadOrdering
  ) -> Instance? {
    // FIXME: All orderings are treated as acquiring-and-releasing.
    let result = _AtomicReferenceStorage.atomicLoad(at: Self._extract(pointer))
    guard let r = result else { return nil }
    return unsafeDowncast(r, to: Instance.self)
  }

  @inlinable @inline(__always)
  @_alwaysEmitIntoClient
  @_semantics("atomics.requires_constant_orderings")
  public static func atomicStore(
    _ desired: __owned Instance?,
    at pointer: UnsafeMutablePointer<Self>,
    ordering: AtomicStoreOrdering
  ) {
    // FIXME: All orderings are treated as acquiring-and-releasing.
    _ = _AtomicReferenceStorage.atomicExchange(
      desired,
      at: _extract(pointer))
  }

  @inlinable @inline(__always)
  @_alwaysEmitIntoClient
  @_semantics("atomics.requires_constant_orderings")
  public static func atomicExchange(
    _ desired: __owned Instance?,
    at pointer: UnsafeMutablePointer<Self>,
    ordering: AtomicUpdateOrdering
  ) -> Instance? {
    // FIXME: All orderings are treated as acquiring-and-releasing.
    let result = _AtomicReferenceStorage.atomicExchange(
      desired,
      at: _extract(pointer))
    guard let r = result else { return nil }
    return unsafeDowncast(r, to: Instance.self)
  }

  @inlinable @inline(__always)
  @_alwaysEmitIntoClient
  @_semantics("atomics.requires_constant_orderings")
  public static func atomicCompareExchange(
    expected: Instance?,
    desired: __owned Instance?,
    at pointer: UnsafeMutablePointer<Self>,
    ordering: AtomicUpdateOrdering
  ) -> (exchanged: Bool, original: Instance?) {
    // FIXME: All orderings are treated as acquiring-and-releasing.
    let result = _AtomicReferenceStorage.atomicCompareExchange(
      expected: expected,
      desired: desired,
      at: _extract(pointer))
    guard let original = result.original else { return (result.exchanged, nil) }
    return (result.exchanged, unsafeDowncast(original, to: Instance.self))
  }

  @inlinable @inline(__always)
  @_alwaysEmitIntoClient
  @_semantics("atomics.requires_constant_orderings")
  public static func atomicCompareExchange(
    expected: Instance?,
    desired: __owned Instance?,
    at pointer: UnsafeMutablePointer<Self>,
    successOrdering: AtomicUpdateOrdering,
    failureOrdering: AtomicLoadOrdering
  ) -> (exchanged: Bool, original: Instance?) {
    // FIXME: All orderings are treated as acquiring-and-releasing.
    let result = _AtomicReferenceStorage.atomicCompareExchange(
      expected: expected,
      desired: desired,
      at: _extract(pointer))
    guard let original = result.original else { return (result.exchanged, nil) }
    return (result.exchanged, unsafeDowncast(original, to: Instance.self))
  }

  @inlinable @inline(__always)
  @_alwaysEmitIntoClient
  @_semantics("atomics.requires_constant_orderings")
  public static func atomicWeakCompareExchange(
    expected: Instance?,
    desired: __owned Instance?,
    at pointer: UnsafeMutablePointer<Self>,
    successOrdering: AtomicUpdateOrdering,
    failureOrdering: AtomicLoadOrdering
  ) -> (exchanged: Bool, original: Instance?) {
    // FIXME: All orderings are treated as acquiring-and-releasing.
    let result = _AtomicReferenceStorage.atomicCompareExchange(
      expected: expected,
      desired: desired,
      at: _extract(pointer))
    guard let original = result.original else { return (result.exchanged, nil) }
    return (result.exchanged, unsafeDowncast(original, to: Instance.self))
  }
}