|
| 1 | +// RUN: %empty-directory(%t) |
| 2 | +// RUN: %{python} %utils/split_file.py -o %t %s |
| 3 | + |
| 4 | +// RUN: %target-swift-frontend -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift -enable-experimental-feature Embedded -parse-as-library |
| 5 | +// RUN: %target-swift-frontend -c -I %t %t/Main.swift -enable-experimental-feature Embedded -o %t/a.o -parse-as-library |
| 6 | +// RUN: %target-clang %t/a.o -o %t/a.out -L%swift_obj_root/lib/swift/embedded/%target-cpu-apple-macos -lswift_Concurrency -lswift_ConcurrencyDefaultExecutor -dead_strip |
| 7 | +// RUN: %target-run %t/a.out | %FileCheck %s |
| 8 | + |
| 9 | +// REQUIRES: swift_in_compiler |
| 10 | +// REQUIRES: executable_test |
| 11 | +// REQUIRES: OS=macosx |
| 12 | +// REQUIRES: swift_feature_Embedded |
| 13 | + |
| 14 | +// BEGIN MyModule.swift |
| 15 | + |
| 16 | +import _Concurrency |
| 17 | + |
| 18 | +nonisolated(unsafe) var glob: UnsafeMutableRawPointer? = nil |
| 19 | +nonisolated(unsafe) var job: UnownedJob? = nil |
| 20 | + |
| 21 | +public final class MyCustomExecutor: SerialExecutor, @unchecked Sendable { |
| 22 | + private init() {} |
| 23 | + |
| 24 | + nonisolated(unsafe) |
| 25 | + public static var shared: MyCustomExecutor = MyCustomExecutor() |
| 26 | + |
| 27 | + public static func installGlobalExecutor() { |
| 28 | + let fn: @convention(thin) () -> () = { |
| 29 | + MyCustomExecutor.shared.unsafeEnqueue(job!) |
| 30 | + } |
| 31 | + glob = unsafeBitCast(fn, to: UnsafeMutableRawPointer?.self) |
| 32 | + } |
| 33 | + |
| 34 | + private func enqueue(_ job: UnownedJob, withDelay nanoseconds: UInt64) {} |
| 35 | + |
| 36 | + private func unsafeEnqueue(_ job: UnownedJob) { |
| 37 | + job.runSynchronously(on: self.asUnownedSerialExecutor()) |
| 38 | + } |
| 39 | + |
| 40 | + public func enqueue(_ job: consuming ExecutorJob) { |
| 41 | + unsafeEnqueue(UnownedJob(job)) |
| 42 | + } |
| 43 | + |
| 44 | + public func asUnownedSerialExecutor() -> UnownedSerialExecutor { |
| 45 | + return UnownedSerialExecutor(ordinary: self) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// BEGIN Main.swift |
| 50 | + |
| 51 | +import MyModule |
| 52 | +import _Concurrency |
| 53 | + |
| 54 | +@main |
| 55 | +struct Entrypoint { |
| 56 | + static func main() async { |
| 57 | + MyCustomExecutor.installGlobalExecutor() |
| 58 | + print("OK!") |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// CHECK: OK! |
0 commit comments