Fix MultiThreadDictionary crash on iOS 15 due to using existential types

See #178
This commit is contained in:
Shadowfacts 2022-10-09 21:53:58 -04:00
parent b43f0d5bd9
commit d1b4b39e86
1 changed files with 38 additions and 43 deletions

View File

@ -13,24 +13,26 @@ import os
// to make the lock semantics more clear // to make the lock semantics more clear
@available(iOS, obsoleted: 16.0) @available(iOS, obsoleted: 16.0)
class MultiThreadDictionary<Key: Hashable & Sendable, Value: Sendable> { class MultiThreadDictionary<Key: Hashable & Sendable, Value: Sendable> {
private let lock: any Lock<[Key: Value]> private let lock: LockHolder<[AnyHashable: Any]>
init() { init() {
if #available(iOS 16.0, *) { if #available(iOS 16.0, *) {
self.lock = OSAllocatedUnfairLock(initialState: [:]) let lock = OSAllocatedUnfairLock(initialState: [:])
self.lock = LockHolder(withLock: lock.withLock(_:))
} else { } else {
self.lock = MutexLock(initialState: [:]) let lock = UnfairLock(initialState: [:])
self.lock = LockHolder(withLock: lock.withLock(_:))
} }
} }
subscript(key: Key) -> Value? { subscript(key: Key) -> Value? {
get { get {
return lock.withLock { dict in return try! lock.withLock { dict in
dict[key] dict[key]
} } as! Value?
} }
set(value) { set(value) {
lock.withLock { dict in _ = try! lock.withLock { dict in
dict[key] = value dict[key] = value
} }
} }
@ -38,20 +40,32 @@ class MultiThreadDictionary<Key: Hashable & Sendable, Value: Sendable> {
/// If the result of this function is unused, it is preferable to use `removeValueWithoutReturning` as it executes asynchronously and doesn't block the calling thread. /// If the result of this function is unused, it is preferable to use `removeValueWithoutReturning` as it executes asynchronously and doesn't block the calling thread.
func removeValue(forKey key: Key) -> Value? { func removeValue(forKey key: Key) -> Value? {
return lock.withLock { dict in return try! lock.withLock { dict in
dict.removeValue(forKey: key) dict.removeValue(forKey: key)
} } as! Value?
} }
func contains(key: Key) -> Bool { func contains(key: Key) -> Bool {
return lock.withLock { dict in return try! lock.withLock { dict in
dict.keys.contains(key) dict.keys.contains(key)
} as! Bool
}
// TODO: this should really be throws/rethrows but the stupid type-erased lock makes that not possible
func withLock<R>(_ body: @Sendable (inout [Key: Value]) -> R) -> R where R: Sendable {
return try! lock.withLock { dict in
var downcasted = dict as! [Key: Value]
defer { dict = downcasted }
return body(&downcasted)
} as! R
} }
} }
func withLock<R>(_ body: @Sendable (inout [Key: Value]) throws -> R) rethrows -> R where R: Sendable { // this type erased struct is necessary due to a compiler bug with stored constrained existential types
return try lock.withLock(body) // see https://github.com/apple/swift/issues/61403
} // see #178
fileprivate struct LockHolder<State> {
let withLock: (_ body: @Sendable (inout State) throws -> any Sendable) throws -> any Sendable
} }
// TODO: replace this only with OSAllocatedUnfairLock // TODO: replace this only with OSAllocatedUnfairLock
@ -65,41 +79,22 @@ fileprivate protocol Lock<State> {
extension OSAllocatedUnfairLock: Lock { extension OSAllocatedUnfairLock: Lock {
} }
// something is wrong with the UnfairLock impl and it results in segv_accerrs // from http://www.russbishop.net/the-law
fileprivate class MutexLock<State>: Lock { fileprivate class UnfairLock<State>: Lock {
private var lock: UnsafeMutablePointer<os_unfair_lock>
private var state: State private var state: State
private var lock = NSLock()
init(initialState: State) { init(initialState: State) {
self.state = initialState self.state = initialState
self.lock = .allocate(capacity: 1)
self.lock.initialize(to: os_unfair_lock())
} }
deinit {
func withLock<R>(_ body: @Sendable (inout State) throws -> R) rethrows -> R where R : Sendable { self.lock.deinitialize(count: 1)
if !lock.lock(before: Date(timeIntervalSinceNow: 1)) { self.lock.deallocate()
// if we can't acquire the lock after 1 second, something has gone catastrophically wrong
fatalError()
} }
defer { lock.unlock() } func withLock<R>(_ body: (inout State) throws -> R) rethrows -> R where R: Sendable {
os_unfair_lock_lock(lock)
defer { os_unfair_lock_unlock(lock) }
return try body(&state) return try body(&state)
} }
} }
//// from http://www.russbishop.net/the-law
//fileprivate class UnfairLock<State>: Lock {
// private var lock: UnsafeMutablePointer<os_unfair_lock>
// private var state: State
// init(initialState: State) {
// self.state = initialState
// self.lock = .allocate(capacity: 1)
// self.lock.initialize(to: os_unfair_lock())
// }
// deinit {
// self.lock.deinitialize(count: 1)
// self.lock.deallocate()
// }
// func withLock<R>(_ body: (inout State) throws -> R) rethrows -> R where R: Sendable {
// os_unfair_lock_lock(lock)
// defer { os_unfair_lock_unlock(lock) }
// return try body(&state)
// }
//}