-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathWeakReference.h
396 lines (332 loc) · 12.2 KB
/
WeakReference.h
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
//===--- WeakReference.h - Swift weak references ----------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
//
//===----------------------------------------------------------------------===//
//
// Swift weak reference implementation.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_RUNTIME_WEAKREFERENCE_H
#define SWIFT_RUNTIME_WEAKREFERENCE_H
#include "swift/shims/Target.h"
#include "swift/shims/Visibility.h"
#include "swift/Runtime/Config.h"
#include "swift/Runtime/HeapObject.h"
#include "swift/Runtime/Metadata.h"
#if SWIFT_OBJC_INTEROP
#include "swift/Runtime/ObjCBridge.h"
#endif
#include "Private.h"
#include <cstdint>
namespace swift {
// Note: This implementation of unknown weak makes several assumptions
// about ObjC's weak variables implementation:
// * Nil is stored verbatim.
// * Tagged pointer objects are stored verbatim with no side table entry.
// * Ordinary objects are stored with the LSB two bits (64-bit) or
// one bit (32-bit) all clear. The stored value otherwise need not be
// the pointed-to object.
//
// The Swift 3 implementation of unknown weak makes the following
// additional assumptions:
// * Ordinary objects are stored *verbatim* with the LSB *three* bits (64-bit)
// or *two* bits (32-bit) all clear.
// Thread-safety:
//
// Reading a weak reference must be thread-safe with respect to:
// * concurrent readers
// * concurrent weak reference zeroing due to deallocation of the
// pointed-to object
// * concurrent ObjC readers or zeroing (for non-native weak storage)
//
// Reading a weak reference is NOT thread-safe with respect to:
// * concurrent writes to the weak variable other than zeroing
// * concurrent destruction of the weak variable
//
// Writing a weak reference must be thread-safe with respect to:
// * concurrent weak reference zeroing due to deallocation of the
// pointed-to object
// * concurrent ObjC zeroing (for non-native weak storage)
//
// Writing a weak reference is NOT thread-safe with respect to:
// * concurrent reads
// * concurrent writes other than zeroing
class WeakReferenceBits {
// On ObjC platforms, a weak variable may be controlled by the ObjC
// runtime or by the Swift runtime. NativeMarkerMask and NativeMarkerValue
// are used to distinguish them.
// if ((ptr & NativeMarkerMask) == NativeMarkerValue) it's Swift
// else it's ObjC
// NativeMarkerMask incorporates the ObjC tagged pointer bits
// plus one more bit that is set in Swift-controlled weak pointer values.
// Non-ObjC platforms don't use any markers.
enum : uintptr_t {
#if !SWIFT_OBJC_INTEROP
NativeMarkerMask = 0,
NativeMarkerValue = 0
#elif defined(__x86_64__) && SWIFT_TARGET_OS_SIMULATOR
NativeMarkerMask = SWIFT_ABI_X86_64_SIMULATOR_OBJC_WEAK_REFERENCE_MARKER_MASK,
NativeMarkerValue = SWIFT_ABI_X86_64_SIMULATOR_OBJC_WEAK_REFERENCE_MARKER_VALUE
#elif defined(__x86_64__)
NativeMarkerMask = SWIFT_ABI_X86_64_OBJC_WEAK_REFERENCE_MARKER_MASK,
NativeMarkerValue = SWIFT_ABI_X86_64_OBJC_WEAK_REFERENCE_MARKER_VALUE
#elif defined(__i386__)
NativeMarkerMask = SWIFT_ABI_I386_OBJC_WEAK_REFERENCE_MARKER_MASK,
NativeMarkerValue = SWIFT_ABI_I386_OBJC_WEAK_REFERENCE_MARKER_VALUE
#elif defined(__arm__) || defined(_M_ARM) || (__arm64__ && __ILP32__)
NativeMarkerMask = SWIFT_ABI_ARM_OBJC_WEAK_REFERENCE_MARKER_MASK,
NativeMarkerValue = SWIFT_ABI_ARM_OBJC_WEAK_REFERENCE_MARKER_VALUE
#elif defined(__s390x__)
NativeMarkerMask = SWIFT_ABI_S390X_OBJC_WEAK_REFERENCE_MARKER_MASK,
NativeMarkerValue = SWIFT_ABI_S390X_OBJC_WEAK_REFERENCE_MARKER_VALUE
#elif defined(__arm64__) || defined(__aarch64__) || defined(_M_ARM64)
NativeMarkerMask = SWIFT_ABI_ARM64_OBJC_WEAK_REFERENCE_MARKER_MASK,
NativeMarkerValue = SWIFT_ABI_ARM64_OBJC_WEAK_REFERENCE_MARKER_VALUE
#else
#error unknown architecture
#endif
};
static_assert((NativeMarkerMask & NativeMarkerValue) == NativeMarkerValue,
"native marker value must fall within native marker mask");
static_assert((NativeMarkerMask & heap_object_abi::SwiftSpareBitsMask)
== NativeMarkerMask,
"native marker mask must fall within Swift spare bits");
#if SWIFT_OBJC_INTEROP
static_assert((NativeMarkerMask & heap_object_abi::ObjCReservedBitsMask)
== heap_object_abi::ObjCReservedBitsMask,
"native marker mask must contain all ObjC tagged pointer bits");
static_assert((NativeMarkerValue & heap_object_abi::ObjCReservedBitsMask)
== 0,
"native marker value must not interfere with ObjC bits");
#endif
uintptr_t bits;
public:
SWIFT_ALWAYS_INLINE
WeakReferenceBits() {}
SWIFT_ALWAYS_INLINE
WeakReferenceBits(HeapObjectSideTableEntry *newValue) {
setNativeOrNull(newValue);
}
SWIFT_ALWAYS_INLINE
bool isNativeOrNull() const {
return bits == 0 || (bits & NativeMarkerMask) == NativeMarkerValue;
}
SWIFT_ALWAYS_INLINE
HeapObjectSideTableEntry *getNativeOrNull() const {
assert(isNativeOrNull());
if (bits == 0)
return nullptr;
return reinterpret_cast<HeapObjectSideTableEntry *>(bits &
~NativeMarkerMask);
}
SWIFT_ALWAYS_INLINE
void setNativeOrNull(HeapObjectSideTableEntry *newValue) {
assert((uintptr_t(newValue) & NativeMarkerMask) == 0);
if (newValue)
bits = uintptr_t(newValue) | NativeMarkerValue;
else
bits = 0;
}
};
class WeakReference {
union {
std::atomic<WeakReferenceBits> nativeValue;
#if SWIFT_OBJC_INTEROP
id nonnativeValue;
#endif
};
void destroyOldNativeBits(WeakReferenceBits oldBits) {
auto oldSide = oldBits.getNativeOrNull();
if (oldSide)
oldSide->decrementWeak();
}
HeapObject *nativeLoadStrongFromBits(WeakReferenceBits bits) {
auto side = bits.getNativeOrNull();
return side ? side->tryRetain() : nullptr;
}
HeapObject *nativeTakeStrongFromBits(WeakReferenceBits bits) {
auto side = bits.getNativeOrNull();
if (side) {
auto obj = side->tryRetain();
side->decrementWeak();
return obj;
} else {
return nullptr;
}
}
void nativeCopyInitFromBits(WeakReferenceBits srcBits) {
auto side = srcBits.getNativeOrNull();
if (side)
side = side->incrementWeak();
nativeValue.store(WeakReferenceBits(side), std::memory_order_relaxed);
}
public:
WeakReference() : nativeValue() {}
WeakReference(std::nullptr_t)
: nativeValue(WeakReferenceBits(nullptr)) { }
WeakReference(const WeakReference& rhs) = delete;
void nativeInit(HeapObject *object) {
auto side = object ? object->refCounts.formWeakReference() : nullptr;
nativeValue.store(WeakReferenceBits(side), std::memory_order_relaxed);
}
void nativeDestroy() {
auto oldBits = nativeValue.load(std::memory_order_relaxed);
nativeValue.store(nullptr, std::memory_order_relaxed);
destroyOldNativeBits(oldBits);
}
void nativeAssign(HeapObject *newObject) {
if (newObject) {
assert(objectUsesNativeSwiftReferenceCounting(newObject) &&
"weak assign native with non-native new object");
}
auto newSide =
newObject ? newObject->refCounts.formWeakReference() : nullptr;
auto newBits = WeakReferenceBits(newSide);
auto oldBits = nativeValue.load(std::memory_order_relaxed);
nativeValue.store(newBits, std::memory_order_relaxed);
assert(oldBits.isNativeOrNull() &&
"weak assign native with non-native old object");
destroyOldNativeBits(oldBits);
}
HeapObject *nativeLoadStrong() {
auto bits = nativeValue.load(std::memory_order_relaxed);
return nativeLoadStrongFromBits(bits);
}
HeapObject *nativeTakeStrong() {
auto bits = nativeValue.load(std::memory_order_relaxed);
nativeValue.store(nullptr, std::memory_order_relaxed);
return nativeTakeStrongFromBits(bits);
}
void nativeCopyInit(WeakReference *src) {
auto srcBits = src->nativeValue.load(std::memory_order_relaxed);
return nativeCopyInitFromBits(srcBits);
}
void nativeTakeInit(WeakReference *src) {
auto srcBits = src->nativeValue.load(std::memory_order_relaxed);
assert(srcBits.isNativeOrNull());
src->nativeValue.store(nullptr, std::memory_order_relaxed);
nativeValue.store(srcBits, std::memory_order_relaxed);
}
void nativeCopyAssign(WeakReference *src) {
if (this == src) return;
nativeDestroy();
nativeCopyInit(src);
}
void nativeTakeAssign(WeakReference *src) {
if (this == src) return;
nativeDestroy();
nativeTakeInit(src);
}
#if SWIFT_OBJC_INTEROP
private:
void nonnativeInit(id object) {
objc_initWeak(&nonnativeValue, object);
}
void initWithNativeness(void *object, bool isNative) {
if (isNative)
nativeInit(static_cast<HeapObject *>(object));
else
nonnativeInit(static_cast<id>(object));
}
void nonnativeDestroy() {
objc_destroyWeak(&nonnativeValue);
}
void destroyWithNativeness(bool isNative) {
if (isNative)
nativeDestroy();
else
nonnativeDestroy();
}
public:
void unknownInit(void *object) {
if (isObjCTaggedPointerOrNull(object)) {
nonnativeValue = static_cast<id>(object);
} else {
bool isNative = objectUsesNativeSwiftReferenceCounting(object);
initWithNativeness(object, isNative);
}
}
void unknownDestroy() {
auto oldBits = nativeValue.load(std::memory_order_relaxed);
destroyWithNativeness(oldBits.isNativeOrNull());
}
void unknownAssign(void *newObject) {
// If the new value is not allocated, simply destroy any old value.
if (isObjCTaggedPointerOrNull(newObject)) {
unknownDestroy();
nonnativeValue = static_cast<id>(newObject);
return;
}
bool newIsNative = objectUsesNativeSwiftReferenceCounting(newObject);
auto oldBits = nativeValue.load(std::memory_order_relaxed);
bool oldIsNative = oldBits.isNativeOrNull();
// If they're both native, use the native function.
if (oldIsNative && newIsNative)
return nativeAssign(static_cast<HeapObject *>(newObject));
// If neither is native, use ObjC.
if (!oldIsNative && !newIsNative)
return (void) objc_storeWeak(&nonnativeValue, static_cast<id>(newObject));
// They don't match. Destroy and re-initialize.
destroyWithNativeness(oldIsNative);
initWithNativeness(newObject, newIsNative);
}
void *unknownLoadStrong() {
auto bits = nativeValue.load(std::memory_order_relaxed);
if (bits.isNativeOrNull())
return nativeLoadStrongFromBits(bits);
else
return objc_loadWeakRetained(&nonnativeValue);
}
void *unknownTakeStrong() {
auto bits = nativeValue.load(std::memory_order_relaxed);
if (bits.isNativeOrNull()) {
nativeValue.store(nullptr, std::memory_order_relaxed);
return nativeTakeStrongFromBits(bits);
}
else {
id result = objc_loadWeakRetained(&nonnativeValue);
objc_destroyWeak(&nonnativeValue);
return result;
}
}
void unknownCopyInit(WeakReference *src) {
auto srcBits = src->nativeValue.load(std::memory_order_relaxed);
if (srcBits.isNativeOrNull())
nativeCopyInitFromBits(srcBits);
else
objc_copyWeak(&nonnativeValue, &src->nonnativeValue);
}
void unknownTakeInit(WeakReference *src) {
auto srcBits = src->nativeValue.load(std::memory_order_relaxed);
if (srcBits.isNativeOrNull())
nativeTakeInit(src);
else
objc_moveWeak(&nonnativeValue, &src->nonnativeValue);
}
void unknownCopyAssign(WeakReference *src) {
if (this == src) return;
unknownDestroy();
unknownCopyInit(src);
}
void unknownTakeAssign(WeakReference *src) {
if (this == src) return;
unknownDestroy();
unknownTakeInit(src);
}
// SWIFT_OBJC_INTEROP
#endif
};
static_assert(sizeof(WeakReference) == sizeof(void*),
"incorrect WeakReference size");
static_assert(alignof(WeakReference) == alignof(void*),
"incorrect WeakReference alignment");
// namespace swift
}
#endif