@@ -20,11 +20,13 @@ import SwiftShims
20
20
// Atomics
21
21
//===----------------------------------------------------------------------===//
22
22
23
+ public typealias _PointerToPointer = UnsafeMutablePointer < UnsafeRawPointer ? >
24
+
23
25
@_transparent
24
26
public // @testable
25
27
func _stdlib_atomicCompareExchangeStrongPtr(
26
- object target: UnsafeMutablePointer < UnsafeRawPointer ? > ,
27
- expected: UnsafeMutablePointer < UnsafeRawPointer ? > ,
28
+ object target: _PointerToPointer ,
29
+ expected: _PointerToPointer ,
28
30
desired: UnsafeRawPointer ? ) -> Bool {
29
31
30
32
// We use Builtin.Word here because Builtin.RawPointer can't be nil.
@@ -69,16 +71,11 @@ public // @testable
69
71
func _stdlib_atomicCompareExchangeStrongPtr< T > (
70
72
object target: UnsafeMutablePointer < UnsafeMutablePointer < T > ${ optional} > ,
71
73
expected: UnsafeMutablePointer < UnsafeMutablePointer < T > ${ optional} > ,
72
- desired: UnsafeMutablePoin ter < T > ${ optional}
73
- ) -> Bool {
74
- let rawTarget = UnsafeMutableRawPointer ( target) . assumingMemoryBound (
75
- to: Optional< UnsafeRawPointer> . self )
76
- let rawExpected = UnsafeMutableRawPointer ( expected) . assumingMemoryBound (
77
- to: Optional< UnsafeRawPointer> . self )
74
+ desired: UnsafeMutablePoin ter < T > ${ optional} ) -> Bool {
78
75
return _stdlib_atomicCompareExchangeStrongPtr (
79
- object: rawTarget ,
80
- expected: rawExpected ,
81
- desired: UnsafeRawPointer ( desired) )
76
+ object: unsafeBitCast ( target , to : _PointerToPointer . self ) ,
77
+ expected: unsafeBitCast ( expected , to : _PointerToPointer . self ) ,
78
+ desired: unsafeBitCast ( desired, to : Optional < UnsafeRawPointer > . self ) )
82
79
}
83
80
% end # optional
84
81
@@ -90,10 +87,10 @@ func _stdlib_atomicInitializeARCRef(
90
87
desired: AnyObject ) -> Bool {
91
88
var expected : UnsafeRawPointer ?
92
89
let desiredPtr = Unmanaged . passRetained ( desired) . toOpaque ( )
93
- let rawTarget = UnsafeMutableRawPointer ( target) . assumingMemoryBound (
94
- to: Optional< UnsafeRawPointer> . self )
95
90
let wonRace = _stdlib_atomicCompareExchangeStrongPtr (
96
- object: rawTarget, expected: & expected, desired: desiredPtr)
91
+ object: unsafeBitCast ( target, to: _PointerToPointer. self) ,
92
+ expected: & expected,
93
+ desired: desiredPtr)
97
94
if !wonRace {
98
95
// Some other thread initialized the value. Balance the retain that we
99
96
// performed on 'desired'.
@@ -226,14 +223,25 @@ public func _swift_stdlib_atomicLoadInt(
226
223
#endif
227
224
}
228
225
226
+ @_transparent
227
+ public // @testable
228
+ func _swift_stdlib_atomicLoadPtrImpl(
229
+ object target: UnsafeMutablePointer < OpaquePointer >
230
+ ) -> OpaquePointer ? {
231
+ let value = Builtin . atomicload_seqcst_Word ( target. _rawValue)
232
+ return OpaquePointer ( bitPattern: Int ( value) )
233
+ }
234
+
229
235
@_transparent
230
236
public // @testable
231
237
func _stdlib_atomicLoadARCRef(
232
238
object target: UnsafeMutablePointer < AnyObject ? >
233
239
) -> AnyObject ? {
234
- let value = Builtin . atomicload_seqcst_Word ( target. _rawValue)
235
- if let unwrapped = UnsafeRawPointer ( bitPattern: Int ( value) ) {
236
- return Unmanaged < AnyObject > . fromOpaque ( unwrapped) . takeUnretainedValue ( )
240
+ let result = _swift_stdlib_atomicLoadPtrImpl (
241
+ object: unsafeBitCast ( target, to: UnsafeMutablePointer< OpaquePointer> . self ) )
242
+ if let unwrapped = result {
243
+ return Unmanaged < AnyObject > . fromOpaque (
244
+ UnsafePointer ( unwrapped) ) . takeUnretainedValue ( )
237
245
}
238
246
return nil
239
247
}
@@ -243,17 +251,17 @@ func _stdlib_atomicLoadARCRef(
243
251
public func _swift_stdlib_atomicFetch${ operation} Int(
244
252
object target: UnsafeMutablePointer < Int > ,
245
253
operand: Int) - > Int {
246
- let rawTarget = UnsafeMutableRawPointer ( target)
247
254
#if arch(i386) || arch(arm)
248
- let value = _swift_stdlib_atomicFetch ${ operation} Int32(
249
- object: rawTarget. assumingMemoryBound ( to: Int32 . self) ,
250
- operand: Int32 ( operand) )
255
+ return Int ( Int32 ( bitPattern:
256
+ _swift_stdlib_atomicFetch ${ operation} UInt32(
257
+ object: unsafeBitCast ( target, to: UnsafeMutablePointer< UInt32> . self ) ,
258
+ operand: UInt32 ( bitPattern: Int32 ( operand) ) ) ) )
251
259
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
252
- let value = _swift_stdlib_atomicFetch ${ operation} Int64(
253
- object: rawTarget. assumingMemoryBound ( to: Int64 . self) ,
254
- operand: Int64 ( operand) )
260
+ return Int ( Int64 ( bitPattern:
261
+ _swift_stdlib_atomicFetch ${ operation} UInt64(
262
+ object: unsafeBitCast ( target, to: UnsafeMutablePointer< UInt64> . self ) ,
263
+ operand: UInt64 ( bitPattern: Int64 ( operand) ) ) ) )
255
264
#endif
256
- return Int ( value)
257
265
}
258
266
% end
259
267
0 commit comments