|
| 1 | +// RUN: %target-run-simple-swift(-Xfrontend -disable-availability-checking -g %import-libdispatch -parse-as-library) | %FileCheck %s |
| 2 | + |
| 3 | +// REQUIRES: concurrency |
| 4 | +// REQUIRES: executable_test |
| 5 | + |
| 6 | +// rdar://106849189 move-only types should be supported in freestanding mode |
| 7 | +// UNSUPPORTED: freestanding |
| 8 | + |
| 9 | +// UNSUPPORTED: back_deployment_runtime |
| 10 | +// REQUIRES: concurrency_runtime |
| 11 | +// REQUIRES: synchronization |
| 12 | +// REQUIRES: libdispatch |
| 13 | + |
| 14 | +import StdlibUnittest |
| 15 | +import Synchronization |
| 16 | +import Dispatch |
| 17 | + |
| 18 | +typealias DefaultExecutorFactory = SimpleExecutorFactory |
| 19 | + |
| 20 | +struct SimpleExecutorFactory: ExecutorFactory { |
| 21 | + public static var mainExecutor: any MainExecutor { |
| 22 | + return SimpleMainExecutor() |
| 23 | + } |
| 24 | + public static var defaultExecutor: any TaskExecutor { |
| 25 | + return SimpleTaskExecutor() |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +@available(SwiftStdlib 6.2, *) |
| 30 | +final class SimpleMainExecutor: MainExecutor, @unchecked Sendable { |
| 31 | + public var isRunning: Bool = false |
| 32 | + |
| 33 | + var shouldStop: Bool = false |
| 34 | + let queue = Mutex<[UnownedJob]>([]) |
| 35 | + |
| 36 | + func enqueue(_ job: consuming ExecutorJob) { |
| 37 | + let unownedJob = UnownedJob(job) |
| 38 | + queue.withLock { |
| 39 | + $0.append(unownedJob) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + func run() throws { |
| 44 | + isRunning = true |
| 45 | + while !shouldStop { |
| 46 | + let jobs = queue.withLock { |
| 47 | + let jobs = $0 |
| 48 | + $0.removeAll() |
| 49 | + return jobs |
| 50 | + } |
| 51 | + for job in jobs { |
| 52 | + job.runSynchronously(on: self.asUnownedSerialExecutor()) |
| 53 | + } |
| 54 | + } |
| 55 | + isRunning = false |
| 56 | + } |
| 57 | + |
| 58 | + func stop() { |
| 59 | + shouldStop = true |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +@available(SwiftStdlib 6.2, *) |
| 64 | +final class SimpleTaskExecutor: TaskExecutor, @unchecked Sendable { |
| 65 | + let queue = Mutex<[UnownedJob]>([]) |
| 66 | + |
| 67 | + func enqueue(_ job: consuming ExecutorJob) { |
| 68 | + print("Enqueued") |
| 69 | + let unownedJob = UnownedJob(job) |
| 70 | + queue.withLock { |
| 71 | + $0.append(unownedJob) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + func run() throws { |
| 76 | + while true { |
| 77 | + let jobs = queue.withLock { |
| 78 | + let jobs = $0 |
| 79 | + $0.removeAll() |
| 80 | + return jobs |
| 81 | + } |
| 82 | + for job in jobs { |
| 83 | + job.runSynchronously(on: self.asUnownedTaskExecutor()) |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +@available(SwiftStdlib 6.2, *) |
| 90 | +@main struct Main { |
| 91 | + static func main() async { |
| 92 | + DispatchQueue.global().async { |
| 93 | + try! (Task.defaultExecutor as! SimpleTaskExecutor).run() |
| 94 | + } |
| 95 | + |
| 96 | + await withTaskGroup { group in |
| 97 | + for _ in 0..<3 { |
| 98 | + group.addTask() { |
| 99 | + for _ in 0..<100 { |
| 100 | + await withUnsafeContinuation { cont in |
| 101 | + cont.resume() |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// If we're on the fast path, we'll only enqueue four times (once per group) |
| 112 | + |
| 113 | +// CHECK: Enqueued |
| 114 | +// CHECK: Enqueued |
| 115 | +// CHECK: Enqueued |
| 116 | +// CHECK: Enqueued |
| 117 | +// CHECK-NOT: Enqueued |
0 commit comments