Skip to content

[Executors] Fix delegation chain of Excecutor.enqueue for Job specifically #65732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion stdlib/public/Concurrency/Executor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,16 @@ public protocol SerialExecutor: Executor {
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
@available(SwiftStdlib 5.9, *)
extension Executor {

// Delegation goes like this:
// Unowned Job -> Executor Job -> Job -> ---||---

public func enqueue(_ job: UnownedJob) {
self.enqueue(ExecutorJob(job))
}

public func enqueue(_ job: __owned ExecutorJob) {
self.enqueue(UnownedJob(job))
self.enqueue(Job(job))
}

public func enqueue(_ job: __owned Job) {
Expand Down
71 changes: 56 additions & 15 deletions test/Concurrency/Runtime/custom_executors_moveOnly_job.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,51 @@
// UNSUPPORTED: back_deployment_runtime
// REQUIRES: concurrency_runtime

final class InlineExecutor: SerialExecutor, CustomStringConvertible {
final class InlineExecutor_UnownedJob: SerialExecutor, CustomStringConvertible {
public func enqueue(_ job: UnownedJob) {
job.runSynchronously(on: self.asUnownedSerialExecutor())
}

var description: Swift.String {
"\(Self.self)()"
}
}
final class InlineExecutor_Job: SerialExecutor, CustomStringConvertible {
public func enqueue(_ job: __owned Job) {
job.runSynchronously(on: self.asUnownedSerialExecutor())
}

var description: Swift.String {
"\(Self.self)()"
}
}

final class InlineExecutor_ExecutorJob: SerialExecutor, CustomStringConvertible {
public func enqueue(_ job: __owned ExecutorJob) {
job.runSynchronously(on: self.asUnownedSerialExecutor())
}

var description: Swift.String {
"InlineExecutor()"
"\(Self.self)()"
}
}

let inlineExecutor = InlineExecutor()
let inlineExecutor_UnownedJob = InlineExecutor_UnownedJob()
let inlineExecutor_Job = InlineExecutor_Job()
let inlineExecutor_ExecutorJob = InlineExecutor_ExecutorJob()

actor Custom {
var count = 0

let selectedExecutor: any SerialExecutor

nonisolated var unownedExecutor: UnownedSerialExecutor {
print("custom unownedExecutor")
return inlineExecutor.asUnownedSerialExecutor()
print("unownedExecutor: \(self.selectedExecutor)")
return selectedExecutor.asUnownedSerialExecutor()
}

init(selectedExecutor: some SerialExecutor) {
self.selectedExecutor = selectedExecutor
}

func report() async {
Expand All @@ -44,20 +65,40 @@ actor Custom {
@available(SwiftStdlib 5.1, *)
@main struct Main {
static func main() async {
print("begin")
let actor = Custom()
await actor.report()
await actor.report()
await actor.report()
print("begin - unowned")
let one = Custom(selectedExecutor: inlineExecutor_UnownedJob)
await one.report()
await one.report()

print("begin - job")
let two = Custom(selectedExecutor: inlineExecutor_Job)
await two.report()
await two.report()

print("begin - executor job")
let three = Custom(selectedExecutor: inlineExecutor_ExecutorJob)
await three.report()
await three.report()

print("end")
}
}

// CHECK: begin
// CHECK-NEXT: custom unownedExecutor
// CHECK: begin - unowned
// CHECK-NEXT: unownedExecutor: InlineExecutor_UnownedJob
// CHECK-NEXT: custom.count == 0
// CHECK-NEXT: unownedExecutor: InlineExecutor_UnownedJob
// CHECK-NEXT: custom.count == 1

// CHECK: begin - job
// CHECK-NEXT: unownedExecutor: InlineExecutor_Job
// CHECK-NEXT: custom.count == 0
// CHECK-NEXT: unownedExecutor: InlineExecutor_Job
// CHECK-NEXT: custom.count == 1

// CHECK: begin - executor job
// CHECK-NEXT: unownedExecutor: InlineExecutor_ExecutorJob
// CHECK-NEXT: custom.count == 0
// CHECK-NEXT: custom unownedExecutor
// CHECK-NEXT: unownedExecutor: InlineExecutor_ExecutorJob
// CHECK-NEXT: custom.count == 1
// CHECK-NEXT: custom unownedExecutor
// CHECK-NEXT: custom.count == 2
// CHECK-NEXT: end