Skip to content

Commit fb0396c

Browse files
committed
[Concurrency] Fall back to allocating from the heap, fix a test.
`CooperativeExecutor` should fall back to heap allocations if the task allocator isn't available when it's enqueuing delayed jobs. Also, remove the reference to `DispatchGlobalExecutor` from the custom executor test. rdar://141348916
1 parent 60fb31c commit fb0396c

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

stdlib/public/Concurrency/CooperativeExecutor.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ extension ExecutorJob {
7171
fileprivate mutating func setupCooperativeExecutorTimestamp() {
7272
// If a Timestamp won't fit, allocate
7373
if cooperativeExecutorTimestampIsIndirect {
74-
let ptr = unsafe allocator!.allocate(as: CooperativeExecutor.Timestamp.self)
74+
let ptr: UnsafeMutablePointer<CooperativeExecutor.Timestamp>
75+
// Try to use the task allocator if it has one
76+
if let allocator {
77+
ptr = unsafe allocator.allocate(as: CooperativeExecutor.Timestamp.self)
78+
} else {
79+
ptr = .allocate(capacity: 1)
80+
}
7581
unsafe self.cooperativeExecutorTimestampPointer = ptr
7682
}
7783
}
@@ -80,7 +86,11 @@ extension ExecutorJob {
8086
// If a Timestamp won't fit, deallocate
8187
if cooperativeExecutorTimestampIsIndirect {
8288
let ptr = unsafe self.cooperativeExecutorTimestampPointer
83-
unsafe allocator!.deallocate(ptr)
89+
if let allocator {
90+
unsafe allocator.deallocate(ptr)
91+
} else {
92+
unsafe ptr.deallocate()
93+
}
8494
}
8595
}
8696
}

test/Concurrency/Runtime/custom_main_executor.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct SimpleExecutorFactory: ExecutorFactory {
2020
}
2121
public static var defaultExecutor: any TaskExecutor {
2222
print("Creating task executor")
23-
return DispatchGlobalTaskExecutor()
23+
return SimpleTaskExecutor()
2424
}
2525
}
2626

@@ -62,9 +62,9 @@ final class SimpleMainExecutor: MainExecutor, @unchecked Sendable {
6262
}
6363

6464
@available(SwiftStdlib 6.2, *)
65-
final class FatalExecutor: TaskExecutor, @unchecked Sendable {
65+
final class SimpleTaskExecutor: TaskExecutor, @unchecked Sendable {
6666
func enqueue(_ job: consuming ExecutorJob) {
67-
fatalError("We should never get here")
67+
MainActor.executor.enqueue(job)
6868
}
6969
}
7070

@@ -84,9 +84,9 @@ func myAsyncFunction() async {
8484
// CHECK: Creating main executor
8585
// CHECK-NEXT: Creating task executor
8686
// CHECK-NEXT: Hello
87-
// CHECK-NEXT: Running
88-
// CHECK-NEXT: Hello World
8987
// CHECK-NEXT: Enqueued job
88+
// CHECK-NEXT: Running
9089
// CHECK-NEXT: Running job
90+
// CHECK-NEXT: Hello World
9191
// CHECK-NEXT: Goodbye
9292

0 commit comments

Comments
 (0)