-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
[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()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, nice :)
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh boy that's a lot of annotations, but love how precisely this expresses stuff :)
} | ||
|
||
return Task.Priority(rawValue: _getCurrentThreadPriority()) ?? .unspecified | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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...async
is 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
stdlib/public/Concurrency/Task.swift
Outdated
/// 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: “operates”
@swift-ci please smoke test |
The
async
operation is a global function that initiates asynchronouswork on behalf of the synchronous code that calls it. Unlike
detach
,async
inherits priority, actor context, and other aspects of thesynchronous code that initiates it, making it a better "default"
operation for creating asynchronous work than
detach
. Thedetach
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.