Skip to content

Commit f239ce8

Browse files
Don't hop to @mainactor if already running on the main thread.
1 parent 396cc94 commit f239ce8

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

stdlib/public/Concurrency/Task.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,10 @@ static void swift_task_performOnExecutorImpl(void *context,
14381438
// If the current executor is compatible with running the new executor,
14391439
// we can just immediately continue running with the resume function
14401440
// we were passed in.
1441-
if (!currentExecutor.mustSwitchToRun(newExecutor)) {
1441+
//
1442+
// Note that swift_task_isCurrentExecutor() return true for @MainActor
1443+
// when running on the main thread without any executor
1444+
if (swift_task_isCurrentExecutor(newExecutor)) {
14421445
return work(context); // 'return' forces tail call
14431446
}
14441447

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %target-run-simple-swift(-Xfrontend -disable-availability-checking) | %FileCheck %s
2+
3+
var isDead: Bool = false
4+
5+
public class Foo {
6+
@MainActor
7+
deinit {
8+
print("DEINIT")
9+
isDead = true
10+
}
11+
}
12+
13+
func main() {
14+
print("isDead = \(isDead)")
15+
do {
16+
_ = Foo()
17+
}
18+
print("isDead = \(isDead)")
19+
}
20+
21+
// CHECK: isDead = false
22+
// CHECK: DEINIT
23+
// CHECK: isDead = true
24+
main()

0 commit comments

Comments
 (0)