-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[PoC][Concurrency] Typed throws in Task.init and .detached #74110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -166,7 +166,22 @@ extension Task { | |
/// have that error propagated here upon cancellation. | ||
/// | ||
/// - Returns: The task's result. | ||
@_alwaysEmitIntoClient | ||
public var value: Success { | ||
@_silgen_name("$sScT7valueTTxvg") // "_t" suffix for the typed throws version | ||
get async throws(Failure) { | ||
do { | ||
return try await _taskFutureGetThrowing(_task) | ||
} catch { | ||
throw error as! Failure | ||
} | ||
} | ||
} | ||
|
||
// Legacy non-typed throws computed property | ||
@usableFromInline | ||
internal var __abi_value: Success { | ||
@_silgen_name("$sScT5valuexvg") | ||
get async throws { | ||
return try await _taskFutureGetThrowing(_task) | ||
} | ||
|
@@ -189,7 +204,7 @@ extension Task { | |
do { | ||
return .success(try await value) | ||
} catch { | ||
return .failure(error as! Failure) // as!-safe, guaranteed to be Failure | ||
return .failure(error) | ||
} | ||
} | ||
} | ||
|
@@ -790,6 +805,104 @@ extension Task where Failure == Error { | |
#endif | ||
} | ||
|
||
// ==== Typed throws Task.init overloads --------------------------------------- | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we decide we want to do this, I'd see if i can DRY this up in any way. |
||
@available(SwiftStdlib 6.0, *) | ||
extension Task { | ||
#if SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY | ||
@discardableResult | ||
@_alwaysEmitIntoClient | ||
@_allowFeatureSuppression(IsolatedAny) | ||
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model") | ||
public init( | ||
priority: TaskPriority? = nil, | ||
@_inheritActorContext @_implicitSelfCapture operation: __owned @Sendable @escaping @isolated(any) () async throws(Failure) -> Success | ||
) { | ||
fatalError("Unavailable in task-to-thread concurrency model") | ||
} | ||
#elseif $Embedded | ||
@discardableResult | ||
@_alwaysEmitIntoClient | ||
public init( | ||
priority: TaskPriority? = nil, | ||
@_inheritActorContext @_implicitSelfCapture operation: __owned @Sendable @escaping @isolated(any) () async throws(Failure) -> Success | ||
) { | ||
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup | ||
// Set up the task flags for a new task. | ||
let flags = taskCreateFlags( | ||
priority: priority, isChildTask: false, copyTaskLocals: true, | ||
inheritContext: true, enqueueJob: true, | ||
addPendingGroupTaskUnconditionally: false, | ||
isDiscardingTask: false) | ||
|
||
// Create the asynchronous task future. | ||
let (task, _) = Builtin.createAsyncTask(flags, operation) | ||
|
||
self._task = task | ||
#else | ||
fatalError("Unsupported Swift compiler") | ||
#endif | ||
} | ||
#else // if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY | ||
/// Runs the given operation asynchronously | ||
/// as part of a new top-level task on behalf of the current actor. | ||
/// | ||
/// Use this function when creating asynchronous work | ||
/// that operates on behalf of the synchronous function that calls it. | ||
/// Like `Task.detached(priority:operation:)`, | ||
/// this function creates a separate, top-level task. | ||
/// Unlike `detach(priority:operation:)`, | ||
/// the task created by `Task.init(priority:operation:)` | ||
/// inherits the priority and actor context of the caller, | ||
/// so the operation is treated more like an asynchronous extension | ||
/// to the synchronous operation. | ||
/// | ||
/// You need to keep a reference to the task | ||
/// if you want to cancel it by calling the `Task.cancel()` method. | ||
/// Discarding your reference to a detached task | ||
/// doesn't implicitly cancel that task, | ||
/// it only makes it impossible for you to explicitly cancel the task. | ||
/// | ||
/// - Parameters: | ||
/// - priority: The priority of the task. | ||
/// Pass `nil` to use the priority from `Task.currentPriority`. | ||
/// - operation: The operation to perform. | ||
@discardableResult | ||
@_alwaysEmitIntoClient | ||
@_allowFeatureSuppression(IsolatedAny) | ||
public init( | ||
priority: TaskPriority? = nil, | ||
@_inheritActorContext @_implicitSelfCapture operation: __owned @Sendable @escaping @isolated(any) () async throws(Failure) -> Success | ||
) { | ||
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup | ||
// Set up the task flags for a new task. | ||
let flags = taskCreateFlags( | ||
priority: priority, isChildTask: false, copyTaskLocals: true, | ||
inheritContext: true, enqueueJob: true, | ||
addPendingGroupTaskUnconditionally: false, | ||
isDiscardingTask: false) | ||
|
||
// Create the asynchronous task future. | ||
#if $BuiltinCreateTask | ||
let builtinSerialExecutor = | ||
Builtin.extractFunctionIsolation(operation)?.unownedExecutor.executor | ||
|
||
let (task, _) = Builtin.createTask(flags: flags, | ||
initialSerialExecutor: | ||
builtinSerialExecutor, | ||
operation: operation) | ||
#else | ||
let (task, _) = Builtin.createAsyncTask(flags, operation) | ||
#endif | ||
|
||
self._task = task | ||
#else | ||
fatalError("Unsupported Swift compiler") | ||
#endif | ||
} | ||
#endif | ||
} | ||
|
||
// ==== Detached Tasks --------------------------------------------------------- | ||
|
||
@available(SwiftStdlib 5.1, *) | ||
|
@@ -980,6 +1093,103 @@ extension Task where Failure == Error { | |
#endif | ||
} | ||
|
||
// ==== Typed throws Task.detached overloads ----------------------------------- | ||
|
||
@available(SwiftStdlib 6.0, *) | ||
extension Task { | ||
#if SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY | ||
@discardableResult | ||
@_alwaysEmitIntoClient | ||
@_allowFeatureSuppression(IsolatedAny) | ||
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model") | ||
public static func detached( | ||
priority: TaskPriority? = nil, | ||
operation: __owned @Sendable @escaping @isolated(any) () async throws(Failure) -> Success | ||
) -> Task<Success, Failure> { | ||
fatalError("Unavailable in task-to-thread concurrency model") | ||
} | ||
#elseif $Embedded | ||
@discardableResult | ||
@_alwaysEmitIntoClient | ||
public static func detached( | ||
priority: TaskPriority? = nil, | ||
operation: __owned @Sendable @escaping () async throws(Failure) -> Success | ||
) -> Task<Success, Failure> { | ||
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup | ||
// Set up the job flags for a new task. | ||
let flags = taskCreateFlags( | ||
priority: priority, isChildTask: false, copyTaskLocals: false, | ||
inheritContext: false, enqueueJob: true, | ||
addPendingGroupTaskUnconditionally: false, | ||
isDiscardingTask: false) | ||
|
||
// Create the asynchronous task future. | ||
let (task, _) = Builtin.createAsyncTask(flags, operation) | ||
|
||
return Task(task) | ||
#else | ||
fatalError("Unsupported Swift compiler") | ||
#endif | ||
} | ||
#else | ||
/// Runs the given throwing operation asynchronously | ||
/// as part of a new top-level task. | ||
/// | ||
/// If the operation throws an error, this method propagates that error. | ||
/// | ||
/// Don't use a detached task if it's possible | ||
/// to model the operation using structured concurrency features like child tasks. | ||
/// Child tasks inherit the parent task's priority and task-local storage, | ||
/// and canceling a parent task automatically cancels all of its child tasks. | ||
/// You need to handle these considerations manually with a detached task. | ||
/// | ||
/// You need to keep a reference to the detached task | ||
/// if you want to cancel it by calling the `Task.cancel()` method. | ||
/// Discarding your reference to a detached task | ||
/// doesn't implicitly cancel that task, | ||
/// it only makes it impossible for you to explicitly cancel the task. | ||
/// | ||
/// - Parameters: | ||
/// - priority: The priority of the task. | ||
/// - operation: The operation to perform. | ||
/// | ||
/// - Returns: A reference to the task. | ||
@discardableResult | ||
@_alwaysEmitIntoClient | ||
@_allowFeatureSuppression(IsolatedAny) | ||
public static func detached( | ||
priority: TaskPriority? = nil, | ||
operation: __owned @Sendable @escaping @isolated(any) () async throws(Failure) -> Success | ||
) -> Task<Success, Failure> { | ||
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup | ||
// Set up the job flags for a new task. | ||
let flags = taskCreateFlags( | ||
priority: priority, isChildTask: false, copyTaskLocals: false, | ||
inheritContext: false, enqueueJob: true, | ||
addPendingGroupTaskUnconditionally: false, | ||
isDiscardingTask: false) | ||
|
||
// Create the asynchronous task future. | ||
#if $BuiltinCreateTask | ||
let builtinSerialExecutor = | ||
Builtin.extractFunctionIsolation(operation)?.unownedExecutor.executor | ||
|
||
let (task, _) = Builtin.createTask(flags: flags, | ||
initialSerialExecutor: | ||
builtinSerialExecutor, | ||
operation: operation) | ||
#else | ||
let (task, _) = Builtin.createAsyncTask(flags, operation) | ||
#endif | ||
|
||
return Task(task) | ||
#else | ||
fatalError("Unsupported Swift compiler") | ||
#endif | ||
} | ||
#endif | ||
} | ||
|
||
// ==== Voluntary Suspension ----------------------------------------------------- | ||
|
||
@available(SwiftStdlib 5.1, *) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.