|
| 1 | +// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking %import-libdispatch -parse-as-library) | %FileCheck %s |
| 2 | + |
| 3 | +// REQUIRES: concurrency |
| 4 | +// REQUIRES: executable_test |
| 5 | +// REQUIRES: libdispatch |
| 6 | +// UNSUPPORTED: freestanding |
| 7 | + |
| 8 | +// UNSUPPORTED: back_deployment_runtime |
| 9 | +// REQUIRES: concurrency_runtime |
| 10 | + |
| 11 | +import Dispatch |
| 12 | + |
| 13 | +func checkIfMainQueue(expectedAnswer expected: Bool) { |
| 14 | + dispatchPrecondition(condition: expected ? .onQueue(DispatchQueue.main) |
| 15 | + : .notOnQueue(DispatchQueue.main)) |
| 16 | +} |
| 17 | + |
| 18 | +protocol WithSpecifiedExecutor: Actor { |
| 19 | + nonisolated var executor: SpecifiedExecutor { get } |
| 20 | +} |
| 21 | + |
| 22 | +protocol SpecifiedExecutor: SerialExecutor {} |
| 23 | + |
| 24 | +extension WithSpecifiedExecutor { |
| 25 | + /// Establishes the WithSpecifiedExecutorExecutor as the serial |
| 26 | + /// executor that will coordinate execution for the actor. |
| 27 | + nonisolated var unownedExecutor: UnownedSerialExecutor { |
| 28 | + executor.asUnownedSerialExecutor() |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +final class InlineExecutor: SpecifiedExecutor, Swift.CustomStringConvertible { |
| 33 | + let name: String |
| 34 | + |
| 35 | + init(_ name: String) { |
| 36 | + self.name = name |
| 37 | + } |
| 38 | + |
| 39 | + public func enqueue(_ job: UnownedJob) { |
| 40 | + print("\(self): enqueue") |
| 41 | + job._runSynchronously(on: self.asUnownedSerialExecutor()) |
| 42 | + print("\(self): after run") |
| 43 | + } |
| 44 | + |
| 45 | + public func asUnownedSerialExecutor() -> UnownedSerialExecutor { |
| 46 | + return UnownedSerialExecutor(ordinary: self) |
| 47 | + } |
| 48 | + |
| 49 | + var description: Swift.String { |
| 50 | + "InlineExecutor(\(name))" |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +actor MyActor: WithSpecifiedExecutor { |
| 55 | + |
| 56 | + nonisolated let executor: SpecifiedExecutor |
| 57 | + |
| 58 | + // Note that we don't have to provide the unownedExecutor in the actor itself. |
| 59 | + // We obtain it from the extension on `WithSpecifiedExecutor`. |
| 60 | + |
| 61 | + init(executor: SpecifiedExecutor) { |
| 62 | + self.executor = executor |
| 63 | + } |
| 64 | + |
| 65 | + func test(expectedExecutor: some SerialExecutor) { |
| 66 | + precondition(_taskIsOnExecutor(expectedExecutor), "Expected to be on: \(expectedExecutor)") |
| 67 | + checkIfMainQueue(expectedAnswer: true) |
| 68 | + print("\(Self.self): on executor \(expectedExecutor)") |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +@main struct Main { |
| 73 | + static func main() async { |
| 74 | + print("begin") |
| 75 | + let one = InlineExecutor("one") |
| 76 | + let actor = MyActor(executor: one) |
| 77 | + await actor.test(expectedExecutor: one) |
| 78 | + await actor.test(expectedExecutor: one) |
| 79 | + await actor.test(expectedExecutor: one) |
| 80 | + print("end") |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// CHECK: begin |
| 85 | +// CHECK-NEXT: InlineExecutor(one): enqueue |
| 86 | +// CHECK-NEXT: MyActor: on executor InlineExecutor(one) |
| 87 | +// CHECK-NEXT: MyActor: on executor InlineExecutor(one) |
| 88 | +// CHECK-NEXT: MyActor: on executor InlineExecutor(one) |
| 89 | +// CHECK-NEXT: InlineExecutor(one): after run |
| 90 | +// CHECK-NEXT: end |
0 commit comments