Skip to content

Commit 0c96a25

Browse files
committed
[Concurrency] Fix task-to-thread model linking.
We need to provide some of the additional functions, as stubs, for task-to-thread model. rdar://141348916
1 parent 47fa717 commit 0c96a25

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

stdlib/public/Concurrency/Executor.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,7 @@ public protocol Executor: AnyObject, Sendable {
3636
func enqueue(_ job: consuming ExecutorJob)
3737
#endif // !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
3838

39-
#if !$Embedded && !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
40-
// The functions below could have been added to a separate protocol,
41-
// but doing that would then mean doing an `as?` cast in e.g.
42-
// enqueueOnGlobalExecutor (in ExecutorBridge.swift), which is
43-
// undesirable from a performance perspective.
44-
39+
#if !$Embedded
4540
/// `true` if this is the main executor.
4641
@available(SwiftStdlib 6.2, *)
4742
var isMainExecutor: Bool { get }
@@ -125,7 +120,7 @@ extension Executor where Self: Equatable {
125120

126121
extension Executor {
127122

128-
#if !$Embedded && !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
123+
#if !$Embedded
129124
// This defaults to `false` so that existing third-party Executor
130125
// implementations will work as expected.
131126
@available(SwiftStdlib 6.2, *)

stdlib/public/Concurrency/ExecutorBridge.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ void _swift_exit(int result) {
2727
exit(result);
2828
}
2929

30+
#if SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
31+
extern "C" SWIFT_CC(swift)
32+
SerialExecutorRef swift_getMainExecutor() {
33+
return SerialExecutorRef::generic();
34+
}
35+
#endif
36+
3037
extern "C" SWIFT_CC(swift)
3138
void _swift_task_checkIsolatedSwift(
3239
HeapObject *executor,

stdlib/public/Concurrency/ExecutorBridge.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import Swift
2121
@_silgen_name("_swift_exit")
2222
internal func _exit(result: CInt)
2323

24-
#if !$Embedded && !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
24+
#if !$Embedded
2525
@available(SwiftStdlib 6.2, *)
2626
@_silgen_name("_swift_task_isMainExecutorSwift")
2727
internal func _isMainExecutor<E>(_ executor: E) -> Bool where E: SerialExecutor {
@@ -79,13 +79,20 @@ internal func _jobGetExecutorPrivateData(
7979
_ job: Builtin.Job
8080
) -> UnsafeMutableRawPointer
8181

82-
#if !$Embedded && !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
82+
#if !$Embedded
83+
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
8384
@available(SwiftStdlib 6.2, *)
8485
@_silgen_name("swift_getMainExecutor")
85-
internal func _getMainExecutor() -> any MainExecutor {
86+
internal func _getMainExecutor() -> any SerialExecutor {
8687
return MainActor.executor
8788
}
88-
#endif
89+
#else
90+
// For task-to-thread model, this is implemented in C++
91+
@available(SwiftStdlib 6.2, *)
92+
@_silgen_name("swift_getMainExecutor")
93+
internal func _getMainExecutor() -> any SerialExecutor
94+
#endif // SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
95+
#endif // !$Embedded
8996

9097
@available(SwiftStdlib 6.2, *)
9198
@_silgen_name("swift_dispatchMain")

stdlib/public/Concurrency/ExecutorImpl.swift

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@
2020

2121
import Swift
2222

23-
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
2423
@available(SwiftStdlib 6.2, *)
2524
@_silgen_name("swift_task_asyncMainDrainQueueImpl")
2625
internal func drainMainQueue() {
26+
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
2727
try! MainActor.executor.run()
2828
_exit(result: 0)
29+
#else
30+
fatalError("swift_task_asyncMainDrainQueue() not supported with task-to-thread")
31+
#endif
2932
}
30-
#endif
3133

3234
@available(SwiftStdlib 6.2, *)
3335
@_silgen_name("swift_task_donateThreadToGlobalExecutorUntilImpl")
@@ -42,34 +44,40 @@ internal func dontateToGlobalExecutor(
4244
}
4345
}
4446

45-
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
4647
@available(SwiftStdlib 6.2, *)
4748
@_silgen_name("swift_task_getMainExecutorImpl")
4849
internal func getMainExecutor() -> UnownedSerialExecutor {
49-
return unsafe UnownedSerialExecutor(MainActor.executor)
50+
return unsafe _getMainExecutor().asUnownedSerialExecutor()
5051
}
5152

5253
@available(SwiftStdlib 6.2, *)
5354
@_silgen_name("swift_task_enqueueMainExecutorImpl")
5455
internal func enqueueOnMainExecutor(job unownedJob: UnownedJob) {
56+
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
5557
MainActor.executor.enqueue(unownedJob)
58+
#else
59+
fatalError("swift_task_enqueueMainExecutor() not supported for task-to-thread")
60+
#endif
5661
}
57-
#endif
5862

5963
@available(SwiftStdlib 6.2, *)
6064
@_silgen_name("swift_task_enqueueGlobalImpl")
6165
internal func enqueueOnGlobalExecutor(job unownedJob: UnownedJob) {
6266
Task.defaultExecutor.enqueue(unownedJob)
6367
}
6468

65-
#if !$Embedded && !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
69+
#if !$Embedded
6670
@available(SwiftStdlib 6.2, *)
6771
@_silgen_name("swift_task_enqueueGlobalWithDelayImpl")
6872
internal func enqueueOnGlobalExecutor(delay: CUnsignedLongLong,
6973
job unownedJob: UnownedJob) {
74+
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
7075
Task.defaultExecutor.asSchedulable!.enqueue(ExecutorJob(unownedJob),
7176
after: .nanoseconds(delay),
7277
clock: .continuous)
78+
#else
79+
fatalError("swift_task_enqueueGlobalWithDelay() not supported for task-to-thread")
80+
#endif
7381
}
7482

7583
@available(SwiftStdlib 6.2, *)
@@ -80,6 +88,7 @@ internal func enqueueOnGlobalExecutor(seconds: CLongLong,
8088
leewayNanoseconds: CLongLong,
8189
clock: CInt,
8290
job unownedJob: UnownedJob) {
91+
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
8392
let delay = Duration.seconds(seconds) + Duration.nanoseconds(nanoseconds)
8493
let leeway = Duration.seconds(leewaySeconds) + Duration.nanoseconds(leewayNanoseconds)
8594
switch clock {
@@ -96,5 +105,8 @@ internal func enqueueOnGlobalExecutor(seconds: CLongLong,
96105
default:
97106
fatalError("Unknown clock ID \(clock)")
98107
}
108+
#else
109+
fatalError("swift_task_enqueueGlobalWithDeadline() not supported for task-to-thread")
110+
#endif
99111
}
100112
#endif

0 commit comments

Comments
 (0)