Skip to content

Commit a9f5107

Browse files
authored
Merge pull request #34776 from DougGregor/concurrency-basic-future
[Concurrency] Introduce a small executable test using asynchronous futures
2 parents 84f92a3 + 95edef7 commit a9f5107

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

stdlib/public/Concurrency/Task.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ extension Task {
146146
public func cancel() {
147147
Builtin.cancelAsyncTask(task)
148148
}
149+
150+
@available(*, deprecated, message: "This is a temporary hack")
151+
public func run() {
152+
runTask(task)
153+
}
149154
}
150155
}
151156

@@ -267,9 +272,6 @@ extension Task {
267272
let (task, context) =
268273
Builtin.createAsyncTaskFuture(flags.bits, nil, operation)
269274

270-
// FIXME: Launch the task on an executor... somewhere....
271-
runTask(task)
272-
273275
return Handle<T>(task: task)
274276
}
275277

@@ -317,11 +319,6 @@ extension Task {
317319
let (task, context) =
318320
Builtin.createAsyncTaskFuture(flags.bits, nil, operation)
319321

320-
print(task)
321-
322-
// FIXME: Launch the task on an executor... somewhere....
323-
runTask(task)
324-
325322
return Handle<T>(task: task)
326323
}
327324
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-concurrency) | %FileCheck %s
2+
// REQUIRES: executable_test
3+
// REQUIRES: concurrency
4+
// REQUIRES: OS=macosx
5+
6+
import Dispatch
7+
8+
extension DispatchQueue {
9+
func async<R>(execute: @escaping () async -> R) -> Task.Handle<R> {
10+
let handle = Task.runDetached(operation: execute)
11+
12+
// Run the task
13+
_ = { self.async { handle.run() } }()
14+
15+
return handle
16+
}
17+
}
18+
19+
func formGreeting(name: String) async -> String {
20+
return "Hello \(name) from async world"
21+
}
22+
23+
func test(name: String) {
24+
let taskHandle = DispatchQueue.main.async { () async -> String in
25+
let greeting = await formGreeting(name: name)
26+
return greeting + "!"
27+
}
28+
29+
_ = DispatchQueue.main.async { () async in
30+
// CHECK: Sleeping
31+
print("Sleeping...")
32+
sleep(2)
33+
let result = await try! taskHandle.get()
34+
// CHECK: Hello Ted from async world
35+
print(result)
36+
assert(result == "Hello Ted from async world!")
37+
exit(0)
38+
}
39+
40+
print("Main task")
41+
}
42+
43+
test(name: "Ted")
44+
45+
dispatchMain()

0 commit comments

Comments
 (0)