Skip to content

Commit 48fad5a

Browse files
committed
Let builtins & shims expose different primitive signatures
For builtins, we want to emulate the stdlib’s solution where all atomic primitive operations take and return a (sort of) opaque atomic representation value. For C shims, we do not want to atomic primitives to take and return _Atomic(T) values, so let’s revert to the original 1.1.x solution.
1 parent d976dd2 commit 48fad5a

File tree

9 files changed

+783
-368
lines changed

9 files changed

+783
-368
lines changed

Sources/Atomics/Conformances/AtomicBool.swift.gyb

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,37 +43,55 @@ extension Bool: AtomicValue {
4343

4444
@_transparent @_alwaysEmitIntoClient
4545
public init(_ value: Bool) {
46+
#if ATOMICS_NATIVE_BUILTINS
4647
_storage = value._atomicRepresentation
48+
#else
49+
_storage = _sa_prepare_Int8(value._atomicRepresentation)
50+
#endif
4751
}
4852

4953
@_transparent @_alwaysEmitIntoClient
5054
public func dispose() -> Value {
55+
#if ATOMICS_NATIVE_BUILTINS
5156
return _storage._decodeBool
57+
#else
58+
return _sa_dispose_Int8(_storage)._decodeBool
59+
#endif
5260
}
5361
}
62+
}
5463

64+
#if ATOMICS_NATIVE_BUILTINS
65+
extension Bool {
5566
@_transparent @_alwaysEmitIntoClient
5667
internal var _atomicRepresentation: _AtomicInt8Storage {
5768
let v: Int8 = (self ? 1 : 0)
58-
#if ATOMICS_NATIVE_BUILTINS
5969
return .init(v._value)
60-
#else
61-
return _sa_prepare_Int8(v)
62-
#endif
6370
}
6471
}
6572

6673
extension _AtomicInt8Storage {
6774
@_transparent @_alwaysEmitIntoClient
6875
internal var _decodeBool: Bool {
69-
#if ATOMICS_NATIVE_BUILTINS
70-
return (Int8(self._value) & 1) != 0
76+
(Int8(self._value) & 1) != 0
77+
}
78+
}
7179
#else
72-
return (_sa_dispose_Int8(self) & 1) != 0
73-
#endif
80+
extension Bool {
81+
@_transparent @_alwaysEmitIntoClient
82+
internal var _atomicRepresentation: Int8 {
83+
self ? 1 : 0
7484
}
7585
}
7686

87+
extension Int8 {
88+
@_transparent @_alwaysEmitIntoClient
89+
internal var _decodeBool: Bool {
90+
(self & 1) != 0
91+
}
92+
}
93+
#endif
94+
7795
extension UnsafeMutablePointer
7896
where Pointee == Bool.AtomicRepresentation {
7997
@_transparent @_alwaysEmitIntoClient

Sources/Atomics/Conformances/IntegerConformances.swift.gyb

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,20 @@ extension ${swiftType}: AtomicValue {
4848

4949
@_transparent @_alwaysEmitIntoClient
5050
public init(_ value: Value) {
51+
#if ATOMICS_NATIVE_BUILTINS
5152
_storage = Self._encode(value)
53+
#else
54+
_storage = _sa_prepare_${shimType}(Self._encode(value))
55+
#endif
5256
}
5357

5458
@_transparent @_alwaysEmitIntoClient
5559
public func dispose() -> Value {
56-
Self._decode(_storage)
60+
#if ATOMICS_NATIVE_BUILTINS
61+
return Self._decode(_storage)
62+
#else
63+
return Self._decode(_sa_dispose_${shimType}(_storage))
64+
#endif
5765
}
5866
}
5967
}
@@ -68,33 +76,39 @@ where Pointee == ${swiftType}.AtomicRepresentation {
6876
}
6977
}
7078

79+
#if ATOMICS_NATIVE_BUILTINS
7180
extension ${swiftType}.AtomicRepresentation {
7281
@_transparent @_alwaysEmitIntoClient
7382
static func _decode(_ storage: _Storage) -> Value {
74-
#if ATOMICS_NATIVE_BUILTINS
7583
return Value(storage._value)
84+
}
85+
86+
@_transparent @_alwaysEmitIntoClient
87+
static func _encode(_ value: Value) -> _Storage {
88+
return _Storage(value._value)
89+
}
90+
}
7691
#else
92+
extension ${swiftType}.AtomicRepresentation {
93+
@_transparent @_alwaysEmitIntoClient
94+
static func _decode(_ v: ${shimType}) -> Value {
7795
% if swiftType == shimType:
78-
return _sa_dispose_${shimType}(storage)
96+
return v
7997
% else:
80-
return Value(_sa_dispose_${shimType}(storage)._value)
98+
return Value(v._value)
8199
% end
82-
#endif
83100
}
84101

85102
@_transparent @_alwaysEmitIntoClient
86-
static func _encode(_ value: Value) -> _Storage {
87-
#if ATOMICS_NATIVE_BUILTINS
88-
return _Storage(value._value)
89-
#else
103+
static func _encode(_ value: Value) -> ${shimType} {
90104
% if swiftType == shimType:
91-
return _sa_prepare_${shimType}(value)
105+
return value
92106
% else:
93-
return _sa_prepare_${shimType}(${shimType}(value._value))
107+
return ${shimType}(value._value)
94108
% end
95-
#endif
96109
}
97110
}
111+
#endif
98112

99113
extension ${swiftType}.AtomicRepresentation: AtomicStorage {
100114
@_semantics("atomics.requires_constant_orderings")

Sources/Atomics/Conformances/PointerConformances.swift.gyb

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,20 @@ extension ${swiftType}: AtomicValue {
4141

4242
@_transparent @_alwaysEmitIntoClient
4343
public init(_ value: Value) {
44+
#if ATOMICS_NATIVE_BUILTINS
4445
_storage = Self._encode(value)
46+
#else
47+
_storage = _sa_prepare_Int(Self._encode(value))
48+
#endif
4549
}
4650

4751
@_transparent @_alwaysEmitIntoClient
4852
public func dispose() -> Value {
49-
Self._decode(_storage)
53+
#if ATOMICS_NATIVE_BUILTINS
54+
return Self._decode(_storage)
55+
#else
56+
return Self._decode(_sa_dispose_Int(_storage))
57+
#endif
5058
}
5159
}
5260
}
@@ -61,14 +69,13 @@ extension ${swiftType}.AtomicRepresentation {
6169
return UnsafeMutableRawPointer(ptr)
6270
.assumingMemoryBound(to: _Storage.self)
6371
}
72+
}
6473

74+
#if ATOMICS_NATIVE_BUILTINS
75+
extension ${swiftType}.AtomicRepresentation {
6576
@_transparent @_alwaysEmitIntoClient
6677
internal static func _decode(_ storage: _Storage) -> Value {
67-
#if ATOMICS_NATIVE_BUILTINS
6878
let bits = Int(storage._value)
69-
#else
70-
let bits = _sa_dispose_Int(storage)
71-
#endif
7279
% if swiftType == "Unmanaged":
7380
return Unmanaged.fromOpaque(UnsafeRawPointer(bitPattern: bits)!)
7481
% else:
@@ -83,13 +90,30 @@ extension ${swiftType}.AtomicRepresentation {
8390
% else:
8491
let bits = Int(bitPattern: value)
8592
% end
86-
#if ATOMICS_NATIVE_BUILTINS
8793
return _Storage(bits._value)
94+
}
95+
}
8896
#else
89-
return _sa_prepare_Int(bits)
90-
#endif
97+
extension ${swiftType}.AtomicRepresentation {
98+
@_transparent @_alwaysEmitIntoClient
99+
internal static func _decode(_ bits: Int) -> Value {
100+
% if swiftType == "Unmanaged":
101+
return Unmanaged.fromOpaque(UnsafeRawPointer(bitPattern: bits)!)
102+
% else:
103+
return ${swiftType}(bitPattern: bits)!
104+
% end
105+
}
106+
107+
@_transparent @_alwaysEmitIntoClient
108+
internal static func _encode(_ value: Value) -> Int {
109+
% if swiftType == "Unmanaged":
110+
return Int(bitPattern: value.toOpaque())
111+
% else:
112+
return Int(bitPattern: value)
113+
% end
91114
}
92115
}
116+
#endif
93117

94118
extension ${swiftType}.AtomicRepresentation: AtomicStorage {
95119
@_semantics("atomics.requires_constant_orderings")
@@ -189,12 +213,20 @@ extension ${swiftType}: AtomicOptionalWrappable {
189213

190214
@inline(__always) @_alwaysEmitIntoClient
191215
public init(_ value: Value) {
216+
#if ATOMICS_NATIVE_BUILTINS
192217
_storage = Self._encode(value)
218+
#else
219+
_storage = _sa_prepare_Int(Self._encode(value))
220+
#endif
193221
}
194222

195223
@inline(__always) @_alwaysEmitIntoClient
196224
public func dispose() -> Value {
225+
#if ATOMICS_NATIVE_BUILTINS
197226
Self._decode(_storage)
227+
#else
228+
return Self._decode(_sa_dispose_Int(_storage))
229+
#endif
198230
}
199231
}
200232
}
@@ -209,14 +241,13 @@ extension ${swiftType}.AtomicOptionalRepresentation {
209241
return UnsafeMutableRawPointer(ptr)
210242
.assumingMemoryBound(to: _Storage.self)
211243
}
244+
}
212245

246+
#if ATOMICS_NATIVE_BUILTINS
247+
extension ${swiftType}.AtomicOptionalRepresentation {
213248
@_transparent @_alwaysEmitIntoClient
214249
internal static func _decode(_ storage: _Storage) -> Value {
215-
#if ATOMICS_NATIVE_BUILTINS
216250
let bits = Int(storage._value)
217-
#else
218-
let bits = _sa_dispose_Int(storage)
219-
#endif
220251
% if swiftType == "Unmanaged":
221252
guard let opaque = UnsafeRawPointer(bitPattern: bits) else {
222253
return nil
@@ -234,13 +265,33 @@ extension ${swiftType}.AtomicOptionalRepresentation {
234265
% else:
235266
let bits = value.map { Int(bitPattern: $0) } ?? 0
236267
% end
237-
#if ATOMICS_NATIVE_BUILTINS
238268
return _Storage(bits._value)
269+
}
270+
}
239271
#else
240-
return _sa_prepare_Int(bits)
241-
#endif
272+
extension ${swiftType}.AtomicOptionalRepresentation {
273+
@_transparent @_alwaysEmitIntoClient
274+
internal static func _decode(_ bits: Int) -> Value {
275+
% if swiftType == "Unmanaged":
276+
guard let opaque = UnsafeRawPointer(bitPattern: bits) else {
277+
return nil
278+
}
279+
return Unmanaged.fromOpaque(opaque)
280+
% else:
281+
return ${swiftType}(bitPattern: bits)
282+
% end
283+
}
284+
285+
@_transparent @_alwaysEmitIntoClient
286+
internal static func _encode(_ value: Value) -> Int {
287+
% if swiftType == "Unmanaged":
288+
return value.map { Int(bitPattern: $0.toOpaque())} ?? 0
289+
% else:
290+
return value.map { Int(bitPattern: $0) } ?? 0
291+
% end
242292
}
243293
}
294+
#endif
244295

245296
extension ${swiftType}.AtomicOptionalRepresentation: AtomicStorage {
246297
@_semantics("atomics.requires_constant_orderings")

Sources/Atomics/Conformances/autogenerated/AtomicBool.swift

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,37 +45,55 @@ extension Bool: AtomicValue {
4545

4646
@_transparent @_alwaysEmitIntoClient
4747
public init(_ value: Bool) {
48+
#if ATOMICS_NATIVE_BUILTINS
4849
_storage = value._atomicRepresentation
50+
#else
51+
_storage = _sa_prepare_Int8(value._atomicRepresentation)
52+
#endif
4953
}
5054

5155
@_transparent @_alwaysEmitIntoClient
5256
public func dispose() -> Value {
57+
#if ATOMICS_NATIVE_BUILTINS
5358
return _storage._decodeBool
59+
#else
60+
return _sa_dispose_Int8(_storage)._decodeBool
61+
#endif
5462
}
5563
}
64+
}
5665

66+
#if ATOMICS_NATIVE_BUILTINS
67+
extension Bool {
5768
@_transparent @_alwaysEmitIntoClient
5869
internal var _atomicRepresentation: _AtomicInt8Storage {
5970
let v: Int8 = (self ? 1 : 0)
60-
#if ATOMICS_NATIVE_BUILTINS
6171
return .init(v._value)
62-
#else
63-
return _sa_prepare_Int8(v)
64-
#endif
6572
}
6673
}
6774

6875
extension _AtomicInt8Storage {
6976
@_transparent @_alwaysEmitIntoClient
7077
internal var _decodeBool: Bool {
71-
#if ATOMICS_NATIVE_BUILTINS
72-
return (Int8(self._value) & 1) != 0
78+
(Int8(self._value) & 1) != 0
79+
}
80+
}
7381
#else
74-
return (_sa_dispose_Int8(self) & 1) != 0
75-
#endif
82+
extension Bool {
83+
@_transparent @_alwaysEmitIntoClient
84+
internal var _atomicRepresentation: Int8 {
85+
self ? 1 : 0
7686
}
7787
}
7888

89+
extension Int8 {
90+
@_transparent @_alwaysEmitIntoClient
91+
internal var _decodeBool: Bool {
92+
(self & 1) != 0
93+
}
94+
}
95+
#endif
96+
7997
extension UnsafeMutablePointer
8098
where Pointee == Bool.AtomicRepresentation {
8199
@_transparent @_alwaysEmitIntoClient

0 commit comments

Comments
 (0)