mirror of
https://github.com/apple/swift-collections
synced 2026-11-26 04:47:48 +00:00
[Deque]: Fix bogus assert in Deque._Storage._ensureUnique
A non-unique reference to a storage instance can legitimately become unique at any point, due to other references (say, held, by another thread) getting destroyed. `Deque`’s `ensureUnique` implementation currently invokes the uniqueness check two times in quick succession, expecting it to return false both times. In rare circumstances, this can lead to a spurious trap in concurrent environments. Fix this by avoiding calling `isUnique` more than once.
This commit is contained in:
1 parent
28161fc21f
commit
ead6314a1c
1 file changed
+9
-5
@@ -187,25 +187,29 @@ extension Deque._Storage {
|
||||
) {
|
||||
let unique = isUnique()
|
||||
if _slowPath(capacity < minimumCapacity || !unique) {
|
||||
_ensureUnique(minimumCapacity: minimumCapacity, linearGrowth: linearGrowth)
|
||||
_ensureUnique(isUnique: unique, minimumCapacity: minimumCapacity, linearGrowth: linearGrowth)
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@inline(never)
|
||||
internal mutating func _ensureUnique(
|
||||
isUnique: Bool,
|
||||
minimumCapacity: Int,
|
||||
linearGrowth: Bool
|
||||
) {
|
||||
if capacity >= minimumCapacity {
|
||||
assert(!self.isUnique())
|
||||
assert(!isUnique)
|
||||
self = self.read { $0.copyElements() }
|
||||
} else if isUnique() {
|
||||
let minimumCapacity = _growCapacity(to: minimumCapacity, linearly: linearGrowth)
|
||||
return
|
||||
}
|
||||
|
||||
let minimumCapacity = _growCapacity(to: minimumCapacity, linearly: linearGrowth)
|
||||
if isUnique {
|
||||
self = self.update { source in
|
||||
source.moveElements(minimumCapacity: minimumCapacity)
|
||||
}
|
||||
} else {
|
||||
let minimumCapacity = _growCapacity(to: minimumCapacity, linearly: linearGrowth)
|
||||
self = self.read { source in
|
||||
source.copyElements(minimumCapacity: minimumCapacity)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user