Files
swift-mirror/stdlib/public/Concurrency/ExecutorImpl.cpp
Konrad 'ktoso' Malawski eaf0b15ea3 [Concurrency] Change isIsolatingCurrent... to return Bool?
This changes the isIsolatingCurrentContext function to return `Bool?`
and removes all the witness table trickery we did previously to detect
if it was implemented or not. This comes at a cost of trying to invoke
it always, before `checkIsolated`, but it makes for an simpler
implementation and more checkable even by third party Swift code which
may want to ask this question.

Along with the `withSerialExecutor` function, this now enables us to
check the isolation at runtime when we have an `any Actor` e.g. from
`#isolation`.

Updates SE-0471 according to
https://forums.swift.org/t/se-0471-improved-custom-serialexecutor-isolation-checking-for-concurrency-runtime/78834/
review discussions
2025-04-28 19:17:58 +09:00

91 lines
3.1 KiB
C++

//===--- ExecutorImpl.cpp - C++ side of executor impl ---------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#if SWIFT_CONCURRENCY_USES_DISPATCH
#include <dispatch/dispatch.h>
#endif
#include "Error.h"
#include "ExecutorBridge.h"
using namespace swift;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
extern "C" SWIFT_CC(swift)
void _swift_task_checkIsolatedSwift(
HeapObject *executor,
const Metadata *executorType,
const SerialExecutorWitnessTable *witnessTable
);
extern "C" SWIFT_CC(swift) bool _swift_task_isMainExecutorSwift(
HeapObject *executor, const Metadata *executorType,
const SerialExecutorWitnessTable *witnessTable);
extern "C" SWIFT_CC(swift) void swift_task_checkIsolatedImpl(
SerialExecutorRef executor) {
HeapObject *identity = executor.getIdentity();
// We might be being called with an actor rather than a "proper"
// SerialExecutor; in that case, we won't have a SerialExecutor witness
// table.
if (executor.hasSerialExecutorWitnessTable()) {
_swift_task_checkIsolatedSwift(identity, swift_getObjectType(identity),
executor.getSerialExecutorWitnessTable());
} else {
const Metadata *objectType = swift_getObjectType(executor.getIdentity());
auto typeName = swift_getTypeName(objectType, true);
swift_Concurrency_fatalError(
0, "Incorrect actor executor assumption; expected '%.*s' executor.\n",
(int)typeName.length, typeName.data);
}
}
extern "C" SWIFT_CC(swift)
int8_t _swift_task_isIsolatingCurrentContextSwift(
HeapObject *executor,
const Metadata *executorType,
const SerialExecutorWitnessTable *witnessTable
);
extern "C" SWIFT_CC(swift) IsIsolatingCurrentContextDecision swift_task_isIsolatingCurrentContextImpl(
SerialExecutorRef executor) {
HeapObject *identity = executor.getIdentity();
// We might be being called with an actor rather than a "proper"
// SerialExecutor; in that case, we won't have a SerialExecutor witness
// table.
if (!executor.hasSerialExecutorWitnessTable()) {
return IsIsolatingCurrentContextDecision::Unknown;
}
auto decision = _swift_task_isIsolatingCurrentContextSwift(
identity, swift_getObjectType(identity),
executor.getSerialExecutorWitnessTable());
return getIsIsolatingCurrentContextDecisionFromInt(decision);
}
extern "C" SWIFT_CC(swift) bool swift_task_isMainExecutorImpl(
SerialExecutorRef executor) {
HeapObject *identity = executor.getIdentity();
return executor.hasSerialExecutorWitnessTable() &&
_swift_task_isMainExecutorSwift(
identity, swift_getObjectType(identity),
executor.getSerialExecutorWitnessTable());
}