Skip to content

Commit d3ac3d4

Browse files
committed
Use Builtin.hopToActor in _runAsyncMain
If the optimizer is doing it's job, then @mainactor can't be used to force another actor-independent async callee onto the main actor. Instead, explicitly hop, which communicates the actual intent and is robust.
1 parent c4f1f56 commit d3ac3d4

File tree

1 file changed

+3
-16
lines changed

1 file changed

+3
-16
lines changed

stdlib/public/Concurrency/Task.swift

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -739,30 +739,17 @@ public func _asyncMainDrainQueue() -> Never
739739

740740
@available(SwiftStdlib 5.5, *)
741741
public func _runAsyncMain(_ asyncFun: @escaping () async throws -> ()) {
742-
#if os(Windows)
743742
Task.detached {
744743
do {
744+
#if !os(Windows)
745+
Builtin.hopToActor(MainActor.shared)
746+
#endif
745747
try await asyncFun()
746748
exit(0)
747749
} catch {
748750
_errorInMain(error)
749751
}
750752
}
751-
#else
752-
@MainActor @Sendable
753-
func _doMain(_ asyncFun: @escaping () async throws -> ()) async {
754-
do {
755-
try await asyncFun()
756-
} catch {
757-
_errorInMain(error)
758-
}
759-
}
760-
761-
Task.detached {
762-
await _doMain(asyncFun)
763-
exit(0)
764-
}
765-
#endif
766753
_asyncMainDrainQueue()
767754
}
768755

0 commit comments

Comments
 (0)