Skip to content

[Concurrency] Add availability checking when calling voucher_needs_adopt from the SDK. #39163

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 1 commit into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions include/swift/Runtime/VoucherShims.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,18 @@

#if SWIFT_HAS_VOUCHERS

#if SWIFT_HAS_VOUCHER_HEADER

static inline bool swift_voucher_needs_adopt(voucher_t _Nullable voucher) {
if (__builtin_available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)) {
return voucher_needs_adopt(voucher);
}
return true;
}

#else

// If the header isn't available, declare the necessary calls here.
#if !SWIFT_HAS_VOUCHER_HEADER

#include <os/object.h>

Expand All @@ -54,7 +64,7 @@ extern "C" voucher_t _Nullable voucher_copy(void);
// Consumes argument, returns retained value.
extern "C" voucher_t _Nullable voucher_adopt(voucher_t _Nullable voucher);

static inline bool voucher_needs_adopt(voucher_t _Nullable voucher) {
static inline bool swift_voucher_needs_adopt(voucher_t _Nullable voucher) {
return true;
}

Expand All @@ -78,7 +88,7 @@ static inline voucher_t _Nullable voucher_copy(void) { return nullptr; }
static inline voucher_t _Nullable voucher_adopt(voucher_t _Nullable voucher) {
return nullptr;
}
static inline bool voucher_needs_adopt(voucher_t _Nullable voucher) {
static inline bool swift_voucher_needs_adopt(voucher_t _Nullable voucher) {
return true;
}
static inline void swift_voucher_release(voucher_t _Nullable voucher) {}
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/Concurrency/VoucherSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class VoucherManager {
if (OriginalVoucher) {
SWIFT_TASK_DEBUG_LOG("[%p] Restoring original voucher %p", this,
*OriginalVoucher);
if (voucher_needs_adopt(*OriginalVoucher)) {
if (swift_voucher_needs_adopt(*OriginalVoucher)) {
auto previous = voucher_adopt(*OriginalVoucher);
swift_voucher_release(previous);
} else {
Expand All @@ -67,7 +67,7 @@ class VoucherManager {
assert(job->Voucher != SWIFT_DEAD_VOUCHER);

voucher_t previous;
if (voucher_needs_adopt(job->Voucher)) {
if (swift_voucher_needs_adopt(job->Voucher)) {
// If we need to adopt the voucher, do so, and grab the old one.
SWIFT_TASK_DEBUG_LOG("[%p] Swapping jobs to %p, adopting voucher %p",
this, job, job->Voucher);
Expand Down Expand Up @@ -104,7 +104,7 @@ class VoucherManager {
assert(OriginalVoucher);
assert(task->Voucher == SWIFT_DEAD_VOUCHER);

if (voucher_needs_adopt(*OriginalVoucher)) {
if (swift_voucher_needs_adopt(*OriginalVoucher)) {
// Adopt the execution thread's original voucher. The task's voucher is
// the one currently adopted, and is returned by voucher_adopt.
task->Voucher = voucher_adopt(*OriginalVoucher);
Expand Down