Skip to content

Commit a1284d0

Browse files
committed
[Concurrency] Stop runDetached from actually printing/running the task.
Rather than immediately running the task synchronously within runDetached, return the handle to the newly-created task. Add a method task.Handle.run() to execute the task. This is just a temporary hack that should not persist in the API, but it lets us launch tasks on a particular Dispatch queue: ```swift extension DispatchQueue { func async<R>(execute: @escaping () async -> R) -> Task.Handle<R> { let handle = Task.runDetached(operation: execute) // Run the task _ = { self.async { handle.run() } }() return handle } } ``` One can pass asynchronous work to DispatchQueue.async, which will schedule that work on the dispatch queue and return a handle. Another asynchronous task can then read the result. Yay for rdar://71125519.
1 parent 0e6d4f7 commit a1284d0

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 test(name: String) {
20+
let taskHandle = DispatchQueue.main.async { () async -> String in
21+
return "Hello \(name) from async world"
22+
}
23+
24+
_ = DispatchQueue.main.async { () async in
25+
// CHECK: Sleeping
26+
print("Sleeping...")
27+
sleep(2)
28+
let result = await try! taskHandle.get()
29+
// CHECK: Hello Ted from async world
30+
print(result)
31+
assert(result == "Hello Ted from async world")
32+
exit(0)
33+
}
34+
35+
print("Main task")
36+
}
37+
38+
test(name: "Ted")
39+
40+
dispatchMain()

0 commit comments

Comments
 (0)