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