Skip to content

[Concurrency] Add "async" operation for continuing work asynchronously.#37007

Merged
DougGregor merged 5 commits into
swiftlang:mainfrom
DougGregor:async-operation
Apr 22, 2021
Merged

[Concurrency] Add "async" operation for continuing work asynchronously.#37007
DougGregor merged 5 commits into
swiftlang:mainfrom
DougGregor:async-operation

Conversation

@DougGregor
Copy link
Copy Markdown
Member

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.

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.
@DougGregor
Copy link
Copy Markdown
Member Author

@swift-ci please test

return JobPriority::UserInitiated;

return static_cast<JobPriority>(qos_class_self());
}
Copy link
Copy Markdown
Contributor

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
Copy link
Copy Markdown
Contributor

@ktoso ktoso Apr 22, 2021

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
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Copy Markdown
Contributor

@ktoso ktoso left a 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(
Copy link
Copy Markdown
Contributor

@ktoso ktoso Apr 22, 2021

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 it
  • detach - completely separate; could wait if one wanted to, carries no values/things implicitly
  • send - 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.

Copy link
Copy Markdown

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))

@swift-ci
Copy link
Copy Markdown
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 3c3f216

Comment thread 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.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: “operates”

@DougGregor
Copy link
Copy Markdown
Member Author

@swift-ci please smoke test

@DougGregor DougGregor merged commit 623e54f into swiftlang:main Apr 22, 2021
@DougGregor DougGregor deleted the async-operation branch April 22, 2021 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants