-
Notifications
You must be signed in to change notification settings - Fork 10.2k
/
UnsafeAtomicLazyReference.swift
135 lines (123 loc) · 4.49 KB
/
UnsafeAtomicLazyReference.swift
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020 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 Swift
/// An atomic optional strong reference that can be set (initialized) exactly
/// once, but read many times.
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
@frozen
public struct UnsafeAtomicLazyReference<Instance: AnyObject> {
public typealias Value = Instance?
@usableFromInline
internal let _ptr: UnsafeMutablePointer<Storage>
@_transparent // Debug performance
public init(@_nonEphemeral at pointer: UnsafeMutablePointer<Storage>) {
self._ptr = pointer
}
}
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
extension UnsafeAtomicLazyReference {
@frozen
public struct Storage {
@usableFromInline
internal let _word: Builtin.Word
@inlinable @inline(__always)
internal init(_word: Builtin.Word) {
self._word = _word
}
@inlinable @inline(__always)
public init() {
// Note: this assumes nil is mapped to 0
_word = 0._builtinWordValue
}
@inlinable @inline(__always)
@discardableResult
public mutating func dispose() -> Value {
guard let ptr = UnsafeRawPointer(bitPattern: Int(_word)) else {
return nil
}
self = Self.init()
return Unmanaged.fromOpaque(ptr).takeRetainedValue()
}
}
}
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
extension UnsafeAtomicLazyReference {
@inlinable
public static func create() -> Self {
let ptr = UnsafeMutablePointer<Storage>.allocate(capacity: 1)
ptr.initialize(to: Storage())
return Self(at: ptr)
}
@discardableResult
@inlinable
public func destroy() -> Value {
let result = _ptr.pointee.dispose()
_ptr.deallocate()
return result
}
}
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
extension UnsafeAtomicLazyReference {
/// Atomically initializes this reference if its current value is nil, then
/// returns the initialized value. If this reference is already initialized,
/// then `initialize(to:)` discards its supplied argument and returns the
/// current value without updating it.
///
/// The following example demonstrates how this can be used to implement a
/// thread-safe lazily initialized reference:
///
/// ```
/// class Image {
/// var _histogram: UnsafeAtomicLazyReference<Histogram> = ...
///
/// // This is safe to call concurrently from multiple threads.
/// var atomicLazyHistogram: Histogram {
/// if let histogram = _histogram.load() { return foo }
/// // Note that code here may run concurrently on
/// // multiple threads, but only one of them will get to
/// // succeed setting the reference.
/// let histogram = ...
/// return _histogram.storeIfNil(foo)
/// }
/// ```
///
/// This operation uses acquiring-and-releasing memory ordering.
public func storeIfNil(_ desired: __owned Instance) -> Instance {
let desiredUnmanaged = Unmanaged.passRetained(desired)
let desiredInt = Int(bitPattern: desiredUnmanaged.toOpaque())
let (current, won) = Builtin.cmpxchg_acqrel_acquire_Word(
_ptr._rawValue,
Storage()._word,
desiredInt._builtinWordValue)
if !Bool(_builtinBooleanLiteral: won) {
// The reference has already been initialized. Balance the retain that
// we performed on `desired`.
desiredUnmanaged.release()
let raw = UnsafeRawPointer(bitPattern: Int(current)).unsafelyUnwrapped
let result = Unmanaged<Instance>.fromOpaque(raw)
return result.takeUnretainedValue()
}
return desiredUnmanaged.takeUnretainedValue()
}
}
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
extension UnsafeAtomicLazyReference {
/// Atomically loads and returns the current value of this reference.
///
/// The load operation is performed with the memory ordering
/// `AtomicLoadOrdering.acquiring`.
public func load() -> Instance? {
let value = Builtin.atomicload_acquire_Word(_ptr._rawValue)
guard let ptr = UnsafeRawPointer(bitPattern: Int(value)) else { return nil }
return Unmanaged.fromOpaque(ptr).takeUnretainedValue()
}
}