1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking %import-libdispatch -parse-as-library )
// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: libdispatch
// REQUIRES: concurrency_runtime
// UNSUPPORTED: back_deployment_runtime
// rdar://126118749
// UNSUPPORTED: CPU=arm64e
import Dispatch
import StdlibUnittest
import Dispatch
final class MyTaskExecutor: TaskExecutor, @unchecked Sendable {
let queue: DispatchQueue
init(queue: DispatchQueue) {
self.queue = queue
}
func enqueue(_ job: consuming ExecutorJob) {
let job = UnownedJob(job)
queue.async {
job.runSynchronously(on: self.asUnownedTaskExecutor())
}
}
}
func testTaskGroup(_ firstExecutor: MyTaskExecutor,
_ secondExecutor: MyTaskExecutor) async {
// 1 level of child tasks
await withTaskGroup(of: Int.self) { group in
group.addTask(executorPreference: firstExecutor) {
dispatchPrecondition(condition: .onQueue(firstExecutor.queue))
dispatchPrecondition(condition: .notOnQueue(secondExecutor.queue))
return 1
}
}
await withThrowingTaskGroup(of: Int.self) { group in
group.addTask(executorPreference: firstExecutor) {
dispatchPrecondition(condition: .onQueue(firstExecutor.queue))
dispatchPrecondition(condition: .notOnQueue(secondExecutor.queue))
return 2
}
}
await withDiscardingTaskGroup() { group in
group.addTask(executorPreference: firstExecutor) {
dispatchPrecondition(condition: .onQueue(firstExecutor.queue))
dispatchPrecondition(condition: .notOnQueue(secondExecutor.queue))
}
}
try! await withThrowingDiscardingTaskGroup() { group in
group.addTask(executorPreference: firstExecutor) {
dispatchPrecondition(condition: .onQueue(firstExecutor.queue))
dispatchPrecondition(condition: .notOnQueue(secondExecutor.queue))
}
}
// Multiple levels of child tasks
await withTaskGroup(of: Int.self) { group in
group.addTask(executorPreference: firstExecutor) {
dispatchPrecondition(condition: .onQueue(firstExecutor.queue))
dispatchPrecondition(condition: .notOnQueue(secondExecutor.queue))
return await withTaskGroup(of: Int.self) { group in
group.addTask(executorPreference: secondExecutor) {
dispatchPrecondition(condition: .notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition: .onQueue(secondExecutor.queue))
return 12
}
return await group.next()!
}
}
}
// Disabling task preference, in task group child task
_ = await withTaskGroup(of: Int.self) { group in
dispatchPrecondition(condition: .notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition: .notOnQueue(secondExecutor.queue))
group.addTask(executorPreference: secondExecutor) {
dispatchPrecondition(condition: .notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition: .onQueue(secondExecutor.queue))
return await withTaskGroup(of: Int.self) { inner in
inner.addTask(executorPreference: globalConcurrentExecutor) {
// disabled the preference
dispatchPrecondition(condition: .notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition: .notOnQueue(secondExecutor.queue))
return 42
}
return await inner.next()!
}
}
_ = group.addTaskUnlessCancelled(executorPreference: secondExecutor) {
dispatchPrecondition(condition: .notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition: .onQueue(secondExecutor.queue))
return await withTaskGroup(of: Int.self) { inner in
inner.addTask(executorPreference: globalConcurrentExecutor) {
// disabled the preference
dispatchPrecondition(condition: .notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition: .notOnQueue(secondExecutor.queue))
return 42
}
return await inner.next()!
}
}
group.addTask(executorPreference: secondExecutor) {
dispatchPrecondition(condition: .notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition: .onQueue(secondExecutor.queue))
await withDiscardingTaskGroup { inner in
inner.addTask(executorPreference: globalConcurrentExecutor) {
// disabled the preference
dispatchPrecondition(condition: .notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition: .notOnQueue(secondExecutor.queue))
}
_ = inner.addTaskUnlessCancelled(executorPreference: globalConcurrentExecutor) {
// disabled the preference
dispatchPrecondition(condition: .notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition: .notOnQueue(secondExecutor.queue))
}
}
return 0
}
group.addTask(executorPreference: secondExecutor) {
dispatchPrecondition(condition: .notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition: .onQueue(secondExecutor.queue))
try! await withThrowingDiscardingTaskGroup { inner in
inner.addTask(executorPreference: globalConcurrentExecutor) {
// disabled the preference
dispatchPrecondition(condition: .notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition: .notOnQueue(secondExecutor.queue))
}
_ = inner.addTaskUnlessCancelled(executorPreference: globalConcurrentExecutor) {
// disabled the preference
dispatchPrecondition(condition: .notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition: .notOnQueue(secondExecutor.queue))
}
}
return 0
}
return await group.next()!
}
}
func testAsyncLet(_ firstExecutor: MyTaskExecutor,
_ secondExecutor: MyTaskExecutor) async {
await withTaskExecutorPreference(firstExecutor) {
async let first: Int = {
dispatchPrecondition(condition: .onQueue(firstExecutor.queue))
dispatchPrecondition(condition: .notOnQueue(secondExecutor.queue))
return 12
}()
_ = await first
}
await withTaskExecutorPreference(firstExecutor) {
await withTaskExecutorPreference(secondExecutor) {
async let first: Int = {
dispatchPrecondition(condition: .notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition: .onQueue(secondExecutor.queue))
return 12
}()
_ = await first
}
}
await withTaskExecutorPreference(firstExecutor) {
await withTaskExecutorPreference(secondExecutor) {
async let first: Int = {
async let firstInside: Int = {
dispatchPrecondition(condition: .notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition: .onQueue(secondExecutor.queue))
return 12
}()
return await firstInside
}()
_ = await first
}
}
}
func testGroupAsyncLet(_ firstExecutor: MyTaskExecutor,
_ secondExecutor: MyTaskExecutor) async {
await withTaskGroup(of: Void.self) { group in
group.addTask(executorPreference: firstExecutor) {
dispatchPrecondition(condition: .onQueue(firstExecutor.queue))
dispatchPrecondition(condition: .notOnQueue(secondExecutor.queue))
async let first: () = expect(firstExecutor)
_ = await first
await withTaskExecutorPreference(secondExecutor) {
async let second: () = expect(secondExecutor)
_ = await second
}
}
}
}
func expect(_ expected: MyTaskExecutor) {
dispatchPrecondition(condition: .onQueue(expected.queue))
}
@main struct Main {
static func main() async {
let firstExecutor = MyTaskExecutor(queue: DispatchQueue(label: "first"))
let secondExecutor = MyTaskExecutor(queue: DispatchQueue(label: "second"))
await testTaskGroup(firstExecutor, secondExecutor)
await testAsyncLet(firstExecutor, secondExecutor)
await testGroupAsyncLet(firstExecutor, secondExecutor)
}
}
|