[Concurrency] Add "async" operation for continuing work asynchronously.#37007
Conversation
This new attribute can be used on parameters of `@Sendable async` type to indicate that the closures arguments passed to such parameters should inherit the actor context where they are formed, which is not the normal behavior for `@Sendable` closures. Another part of rdar://76927008.
The `async` operation is a global function that initiates asynchronous work on behalf of the synchronous code that calls it. Unlike `detach`, `async` inherits priority, actor context, and other aspects of the synchronous code that initiates it, making it a better "default" operation for creating asynchronous work than `detach`. The `detach` operation is still important for creating truly detached tasks that can later be `await`'d or cancelled if needed. Implements the main entry point for rdar://76927008.
|
@swift-ci please test |
| return JobPriority::UserInitiated; | ||
|
|
||
| return static_cast<JobPriority>(qos_class_self()); | ||
| } |
| @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) | ||
| public func async( | ||
| priority: Task.Priority = .unspecified, | ||
| @_inheritActorContext @_implicitSelfCapture operation: __owned @Sendable @escaping () async -> Void |
There was a problem hiding this comment.
oh boy that's a lot of annotations, but love how precisely this expresses stuff :)
| } | ||
|
|
||
| return Task.Priority(rawValue: _getCurrentThreadPriority()) ?? .unspecified | ||
| } |
ktoso
left a comment
There was a problem hiding this comment.
Ah cool, not much magic at all then. Very nice 👍
I'll implement propagating locals through these once it merges 👍
| /// which thread is executing. | ||
| /// - operation: the operation to execute | ||
| @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) | ||
| public func async( |
There was a problem hiding this comment.
Man... I'd really like a verb here; Isn't this a send?
spawn- make child task, will await itdetach- completely separate; could wait if one wanted to, carries no values/things implicitlysend- fire and forget, cannot await on the result; usable from sync code, does carry priority and locals <- this is exactly what doing a "fire and forget" message send would want to be, so I'm so tempted to call it a send...asyncis a bit weird and it's dispatch wording inheritance too..
It's three concepts but really seems like the "trinity" of things we'll ever need: waiting, not waiting, completely splitting off computation.
There was a problem hiding this comment.
Man... I'd really like a verb here; Isn't this a
send?
Nah, it's a dispatch ;-)
(yeah, gcd connotations I know, but one could say the same for send(2))
|
Build failed |
| /// Run given `operation` as asynchronously in its own top-level task. | ||
| /// | ||
| /// The `async` function should be used when creating asynchronous work | ||
| /// that operations on behalf of the synchronous function that calls it. |
|
@swift-ci please smoke test |
The
asyncoperation is a global function that initiates asynchronouswork on behalf of the synchronous code that calls it. Unlike
detach,asyncinherits priority, actor context, and other aspects of thesynchronous code that initiates it, making it a better "default"
operation for creating asynchronous work than
detach. Thedetachoperation is still important for creating truly detached tasks that
can later be
await'd or cancelled if needed.Implements the main entry point for rdar://76927008.