Skip to content

Commit 913da06

Browse files
authored
Merge pull request #37680 from DougGregor/task-api-rework-5.5
[Concurrency] Alternative Task API
2 parents 703a122 + 25da3d9 commit 913da06

17 files changed

+910
-790
lines changed

stdlib/public/Concurrency/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ add_swift_target_library(swift_Concurrency ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} I
6060
AsyncThrowingMapSequence.swift
6161
AsyncThrowingPrefixWhileSequence.swift
6262
PartialAsyncTask.swift
63+
SourceCompatibilityShims.swift
6364
Task.cpp
6465
Task.swift
6566
TaskCancellation.swift

stdlib/public/Concurrency/PartialAsyncTask.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,3 @@ public func withUnsafeThrowingContinuation<T>(
278278
fn(UnsafeContinuation<T, Error>($0))
279279
}
280280
}
281-
282-
@available(SwiftStdlib 5.5, *)
283-
@available(*, deprecated, message: "please use UnsafeContination<..., Error>")
284-
public typealias UnsafeThrowingContinuation<T> = UnsafeContinuation<T, Error>
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
// This file provides source compatibility shims to help migrate code
13+
// using earlier versions of the concurrency library to the latest syntax.
14+
//===----------------------------------------------------------------------===//
15+
16+
import Swift
17+
@_implementationOnly import _SwiftConcurrencyShims
18+
19+
@available(SwiftStdlib 5.5, *)
20+
extension Task where Success == Never, Failure == Never {
21+
@available(*, deprecated, message: "Task.Priority has been removed; use TaskPriority")
22+
public typealias Priority = TaskPriority
23+
24+
@available(*, deprecated, message: "Task.Handle has been removed; use Task")
25+
public typealias Handle = _Concurrency.Task
26+
27+
@available(*, deprecated, message: "Task.CancellationError has been removed; use CancellationError")
28+
@_alwaysEmitIntoClient
29+
public static func CancellationError() -> _Concurrency.CancellationError {
30+
return _Concurrency.CancellationError()
31+
}
32+
}
33+
34+
@available(SwiftStdlib 5.5, *)
35+
extension TaskPriority {
36+
@available(*, deprecated, message: "unspecified priority will be removed; use nil")
37+
@_alwaysEmitIntoClient
38+
public static var unspecified: TaskPriority {
39+
.init(rawValue: 0x00)
40+
}
41+
42+
@available(*, deprecated, message: "userInteractive priority will be removed")
43+
@_alwaysEmitIntoClient
44+
public static var userInteractive: TaskPriority {
45+
.init(rawValue: 0x21)
46+
}
47+
}
48+
49+
@available(SwiftStdlib 5.5, *)
50+
@_alwaysEmitIntoClient
51+
public func withTaskCancellationHandler<T>(
52+
handler: @Sendable () -> (),
53+
operation: () async throws -> T
54+
) async rethrows -> T {
55+
try await withTaskCancellationHandler(operation: operation, onCancel: handler)
56+
}
57+
58+
@available(SwiftStdlib 5.5, *)
59+
extension Task where Success == Never, Failure == Never {
60+
@available(*, deprecated, message: "`Task.withCancellationHandler` has been replaced by `withTaskCancellationHandler` and will be removed shortly.")
61+
@_alwaysEmitIntoClient
62+
public static func withCancellationHandler<T>(
63+
handler: @Sendable () -> (),
64+
operation: () async throws -> T
65+
) async rethrows -> T {
66+
try await withTaskCancellationHandler(handler: handler, operation: operation)
67+
}
68+
}
69+
70+
@available(SwiftStdlib 5.5, *)
71+
extension Task where Failure == Error {
72+
@discardableResult
73+
@_alwaysEmitIntoClient
74+
@available(*, deprecated, message: "`Task.runDetached` was replaced by `Task.detached` and will be removed shortly.")
75+
public static func runDetached(
76+
priority: TaskPriority? = nil,
77+
operation: __owned @Sendable @escaping () async throws -> Success
78+
) -> Task<Success, Failure> {
79+
detached(priority: priority, operation: operation)
80+
}
81+
}
82+
83+
@discardableResult
84+
@available(SwiftStdlib 5.5, *)
85+
@available(*, deprecated, message: "`detach` was replaced by `Task.detached` and will be removed shortly.")
86+
@_alwaysEmitIntoClient
87+
public func detach<T>(
88+
priority: TaskPriority? = nil,
89+
operation: __owned @Sendable @escaping () async -> T
90+
) -> Task<T, Never> {
91+
Task.detached(priority: priority, operation: operation)
92+
}
93+
94+
@discardableResult
95+
@available(SwiftStdlib 5.5, *)
96+
@available(*, deprecated, message: "`detach` was replaced by `Task.detached` and will be removed shortly.")
97+
@_alwaysEmitIntoClient
98+
public func detach<T>(
99+
priority: TaskPriority? = nil,
100+
operation: __owned @Sendable @escaping () async throws -> T
101+
) -> Task<T, Error> {
102+
Task.detached(priority: priority, operation: operation)
103+
}
104+
105+
@discardableResult
106+
@available(SwiftStdlib 5.5, *)
107+
@available(*, deprecated, message: "`asyncDetached` was replaced by `Task.detached` and will be removed shortly.")
108+
@_alwaysEmitIntoClient
109+
public func asyncDetached<T>(
110+
priority: TaskPriority? = nil,
111+
@_implicitSelfCapture operation: __owned @Sendable @escaping () async -> T
112+
) -> Task<T, Never> {
113+
return Task.detached(priority: priority, operation: operation)
114+
}
115+
116+
@discardableResult
117+
@available(SwiftStdlib 5.5, *)
118+
@available(*, deprecated, message: "`asyncDetached` was replaced by `Task.detached` and will be removed shortly.")
119+
@_alwaysEmitIntoClient
120+
public func asyncDetached<T>(
121+
priority: TaskPriority? = nil,
122+
@_implicitSelfCapture operation: __owned @Sendable @escaping () async throws -> T
123+
) -> Task<T, Error> {
124+
return Task.detached(priority: priority, operation: operation)
125+
}
126+
127+
@available(SwiftStdlib 5.5, *)
128+
@available(*, deprecated, message: "`async` was replaced by `Task.init` and will be removed shortly.")
129+
@discardableResult
130+
@_alwaysEmitIntoClient
131+
public func async<T>(
132+
priority: TaskPriority? = nil,
133+
@_inheritActorContext @_implicitSelfCapture operation: __owned @Sendable @escaping () async -> T
134+
) -> Task<T, Never> {
135+
.init(priority: priority, operation: operation)
136+
}
137+
138+
@available(SwiftStdlib 5.5, *)
139+
@available(*, deprecated, message: "`async` was replaced by `Task.init` and will be removed shortly.")
140+
@discardableResult
141+
@_alwaysEmitIntoClient
142+
public func async<T>(
143+
priority: TaskPriority? = nil,
144+
@_inheritActorContext @_implicitSelfCapture operation: __owned @Sendable @escaping () async throws -> T
145+
) -> Task<T, Error> {
146+
.init(priority: priority, operation: operation)
147+
}
148+
149+
@available(SwiftStdlib 5.5, *)
150+
extension Task where Success == Never, Failure == Never {
151+
@available(*, deprecated, message: "`Task.Group` was replaced by `ThrowingTaskGroup` and `TaskGroup` and will be removed shortly.")
152+
public typealias Group<TaskResult> = ThrowingTaskGroup<TaskResult, Error>
153+
154+
@available(*, deprecated, message: "`Task.withGroup` was replaced by `withThrowingTaskGroup` and `withTaskGroup` and will be removed shortly.")
155+
@_alwaysEmitIntoClient
156+
public static func withGroup<TaskResult, BodyResult>(
157+
resultType: TaskResult.Type,
158+
returning returnType: BodyResult.Type = BodyResult.self,
159+
body: (inout Task.Group<TaskResult>) async throws -> BodyResult
160+
) async rethrows -> BodyResult {
161+
try await withThrowingTaskGroup(of: resultType) { group in
162+
try await body(&group)
163+
}
164+
}
165+
}
166+
167+
@available(SwiftStdlib 5.5, *)
168+
extension Task {
169+
@available(*, deprecated, message: "get() has been replaced by .value")
170+
@_alwaysEmitIntoClient
171+
public func get() async throws -> Success {
172+
return try await value
173+
}
174+
175+
@available(*, deprecated, message: "getResult() has been replaced by .result")
176+
@_alwaysEmitIntoClient
177+
public func getResult() async -> Result<Success, Failure> {
178+
return await result
179+
}
180+
}
181+
182+
@available(SwiftStdlib 5.5, *)
183+
extension Task where Failure == Never {
184+
@available(*, deprecated, message: "get() has been replaced by .value")
185+
@_alwaysEmitIntoClient
186+
public func get() async -> Success {
187+
return await value
188+
}
189+
}
190+
191+
@available(SwiftStdlib 5.5, *)
192+
extension TaskGroup {
193+
@available(*, deprecated, renamed: "async(priority:operation:)")
194+
@_alwaysEmitIntoClient
195+
public mutating func add(
196+
priority: TaskPriority? = nil,
197+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
198+
) async -> Bool {
199+
return self.asyncUnlessCancelled(priority: priority) {
200+
await operation()
201+
}
202+
}
203+
204+
@available(*, deprecated, renamed: "async(priority:operation:)")
205+
@_alwaysEmitIntoClient
206+
public mutating func spawn(
207+
priority: TaskPriority? = nil,
208+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
209+
) {
210+
async(priority: priority, operation: operation)
211+
}
212+
213+
@available(*, deprecated, renamed: "asyncUnlessCancelled(priority:operation:)")
214+
@_alwaysEmitIntoClient
215+
public mutating func spawnUnlessCancelled(
216+
priority: TaskPriority? = nil,
217+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
218+
) -> Bool {
219+
asyncUnlessCancelled(priority: priority, operation: operation)
220+
}
221+
}
222+
223+
@available(SwiftStdlib 5.5, *)
224+
extension ThrowingTaskGroup {
225+
@available(*, deprecated, renamed: "async(priority:operation:)")
226+
@_alwaysEmitIntoClient
227+
public mutating func add(
228+
priority: TaskPriority? = nil,
229+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
230+
) async -> Bool {
231+
return self.asyncUnlessCancelled(priority: priority) {
232+
try await operation()
233+
}
234+
}
235+
236+
@available(*, deprecated, renamed: "async(priority:operation:)")
237+
@_alwaysEmitIntoClient
238+
public mutating func spawn(
239+
priority: TaskPriority? = nil,
240+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
241+
) {
242+
async(priority: priority, operation: operation)
243+
}
244+
245+
@available(*, deprecated, renamed: "asyncUnlessCancelled(priority:operation:)")
246+
@_alwaysEmitIntoClient
247+
public mutating func spawnUnlessCancelled(
248+
priority: TaskPriority? = nil,
249+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
250+
) -> Bool {
251+
asyncUnlessCancelled(priority: priority, operation: operation)
252+
}
253+
}
254+
255+
@available(SwiftStdlib 5.5, *)
256+
@available(*, deprecated, message: "please use UnsafeContination<..., Error>")
257+
public typealias UnsafeThrowingContinuation<T> = UnsafeContinuation<T, Error>

0 commit comments

Comments
 (0)