Skip to content

[5.5][Concurrency] Propagate Darwin vouchers across async tasks. #39160

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

Merged
merged 4 commits into from
Sep 8, 2021

Conversation

mikeash
Copy link
Contributor

@mikeash mikeash commented Sep 3, 2021

Cherry pick #39115 to release/5.5.

Darwin OSes support vouchers, which are key/value sets that can be adopted on a thread to influence its execution, or sent to another process. APIs like Dispatch propagate vouchers to worker threads when running async code. This change makes Swift Concurrency do the same.

The change consists of a few different parts:

  1. A set of shims (in VoucherShims.h) which provides declarations for the necessary calls when they're not available from the SDK, and stub implementations for non-Darwin platforms.
  2. One of Job's reserved fields is now used to store the voucher associated with a job.
  3. Jobs grab the current thread's voucher when they're created.
  4. A VoucherManager class manages adoption of vouchers when running a Job, and replacing vouchers in suspended tasks.
  5. A VoucherManager instance is maintained in ExecutionTrackingInfo, and is updated as necessary throughout a Job/Task's lifecycle.

This cherry-pick also incorporates:

  • d5c0469 [Concurrency] Replace SWIFT_TASK_PRINTF_DEBUG with a SWIFT_TASK_DEBUG_LOG macro.
  • 09f3d99 [Concurrency] Add availability checking when calling voucher_needs_adopt from the SDK.
  • c216d91 [Concurrency] Use overload resolution to stub voucher_needs_adopt.

rdar://76080222

Darwin OSes support vouchers, which are key/value sets that can be adopted on a thread to influence its execution, or sent to another process. APIs like Dispatch propagate vouchers to worker threads when running async code. This change makes Swift Concurrency do the same.

The change consists of a few different parts:

1. A set of shims (in VoucherShims.h) which provides declarations for the necessary calls when they're not available from the SDK, and stub implementations for non-Darwin platforms.
2. One of Job's reserved fields is now used to store the voucher associated with a job.
3. Jobs grab the current thread's voucher when they're created.
4. A VoucherManager class manages adoption of vouchers when running a Job, and replacing vouchers in suspended tasks.
5. A VoucherManager instance is maintained in ExecutionTrackingInfo, and is updated as necessary throughout a Job/Task's lifecycle.

rdar://76080222
(cherry picked from commit a80a19b)
@mikeash mikeash requested a review from a team as a code owner September 3, 2021 15:19
@mikeash
Copy link
Contributor Author

mikeash commented Sep 3, 2021

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Sep 3, 2021

Build failed
Swift Test Linux Platform
Git Sha - 670944c

@swift-ci
Copy link
Contributor

swift-ci commented Sep 3, 2021

Build failed
Swift Test OS X Platform
Git Sha - 670944c

…_LOG macro.

This macro takes the string and parameters directly, and is conditionally defined to either call fprintf or ignore its arguments. This makes the call sites a little more pleasant (no #if scattered about) and ensures every log includes the thread ID and a newline automatically.

(cherry picked from commit d5c0469)
@mikeash
Copy link
Contributor Author

mikeash commented Sep 3, 2021

@swift-ci please test

@mikeash
Copy link
Contributor Author

mikeash commented Sep 7, 2021

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Sep 7, 2021

Build failed
Swift Test Linux Platform
Git Sha - 657b70c8e5a24e1a6d2c7753333152c49ea93dcd

@swift-ci
Copy link
Contributor

swift-ci commented Sep 7, 2021

Build failed
Swift Test OS X Platform
Git Sha - 657b70c8e5a24e1a6d2c7753333152c49ea93dcd

Using has_include to conditionalize the stubbing of voucher_needs_adopt doesn't work when the SDK provides an older header that doesn't declare the function. Instead, always provide a stub, but use overload resolution to prefer the SDK's declaration. Declare the stub as taking `void *`. When calling it with `voucher_t`, the implicit conversion is allowed, but causes the overload to be disfavored when the SDK's declaration takes `voucher_t`.

rdar://82797720
@mikeash mikeash force-pushed the voucher-propagation-5.5 branch from 657b70c to 601c29a Compare September 7, 2021 16:28
@mikeash
Copy link
Contributor Author

mikeash commented Sep 7, 2021

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Sep 7, 2021

Build failed
Swift Test OS X Platform
Git Sha - 601c29a

@mikeash
Copy link
Contributor Author

mikeash commented Sep 7, 2021

@swift-ci please test macos platform

@mikeash mikeash merged commit 0b57c76 into swiftlang:release/5.5 Sep 8, 2021
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.

4 participants