Files
Brandon Williams 2c8b1c0cad Fix potential deadlock in Shared (#3356)
* Fixed shared deadlock.

* Fix deadlock>

* wip

* wip

* Improve test.
2024-09-06 12:11:40 -04:00

37 lines
818 B
Swift

import Dispatch
func mainActorNow<R: Sendable>(execute block: @MainActor @Sendable () -> R) -> R {
if DispatchQueue.getSpecific(key: key) == value {
return MainActor._assumeIsolated {
block()
}
} else {
return DispatchQueue.main.sync {
MainActor._assumeIsolated {
block()
}
}
}
}
func mainActorASAP(execute block: @escaping @MainActor @Sendable () -> Void) {
if DispatchQueue.getSpecific(key: key) == value {
MainActor._assumeIsolated {
block()
}
} else {
DispatchQueue.main.async {
MainActor._assumeIsolated {
block()
}
}
}
}
private let key: DispatchSpecificKey<UInt8> = {
let key = DispatchSpecificKey<UInt8>()
DispatchQueue.main.setSpecific(key: key, value: value)
return key
}()
private let value: UInt8 = 0