Skip to content

Commit a3f2984

Browse files
committed
[embedded] Use atomics in swift_isUniquelyReferenced_nonNull_native too
1 parent 436f199 commit a3f2984

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

stdlib/public/core/EmbeddedRuntime.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ public func swift_setDeallocating(object: UnsafeMutablePointer<HeapObject>) {
110110

111111
@_silgen_name("swift_isUniquelyReferenced_nonNull_native")
112112
public func swift_isUniquelyReferenced_nonNull_native(object: UnsafeMutablePointer<HeapObject>) -> Bool {
113-
// TODO/FIXME: Refcounting is not thread-safe, the following only works in single-threaded environments.
114-
return object.pointee.refcount == 1
113+
let refcount = refcountPointer(for: object)
114+
return loadAcquire(refcount) == 1
115115
}
116116

117117
@_silgen_name("swift_retain")
@@ -175,6 +175,10 @@ fileprivate func loadRelaxed(_ refcount: UnsafeMutablePointer<Int>) -> Int {
175175
Int(Builtin.atomicload_monotonic_Word(refcount._rawValue))
176176
}
177177

178+
fileprivate func loadAcquire(_ refcount: UnsafeMutablePointer<Int>) -> Int {
179+
Int(Builtin.atomicload_acquire_Word(refcount._rawValue))
180+
}
181+
178182
fileprivate func subFetchAcquireRelease(_ refcount: UnsafeMutablePointer<Int>, n: Int) -> Int {
179183
let oldValue = Int(Builtin.atomicrmw_sub_acqrel_Word(refcount._rawValue, n._builtinWordValue))
180184
return oldValue - n

0 commit comments

Comments
 (0)