KT-51297: Allow suspend from any thread from ObjC

This patch adds a binary compiler argument to allow not crashing if a
suspend function is called from a non-Main dispatcher from ObjC or
Swift. This is relevant for people using the new memory model, in which
this restriction may be relaxed.
This commit is contained in:
Ahmed El-Helw
2022-05-19 22:32:27 +04:00
committed by SvyatoslavScherbina
parent 2dac366dbc
commit 93cb4d71d8
9 changed files with 69 additions and 2 deletions
@@ -16,6 +16,7 @@ RUNTIME_WEAK int32_t Kotlin_gcSchedulerType = 2;
RUNTIME_WEAK int32_t Kotlin_workerExceptionHandling = 0;
RUNTIME_WEAK int32_t Kotlin_freezingEnabled = 1;
RUNTIME_WEAK int32_t Kotlin_freezingChecksEnabled = 1;
RUNTIME_WEAK int32_t Kotlin_suspendFunctionsFromAnyThreadFromObjC = 0;
RUNTIME_WEAK const Kotlin_getSourceInfo_FunctionType Kotlin_getSourceInfo_Function = nullptr;
#ifdef KONAN_ANDROID
RUNTIME_WEAK int32_t Kotlin_printToAndroidLogcat = 1;
@@ -37,6 +38,10 @@ ALWAYS_INLINE bool compiler::freezingChecksEnabled() noexcept {
return Kotlin_freezingChecksEnabled != 0;
}
ALWAYS_INLINE bool compiler::suspendFunctionsFromAnyThreadFromObjCEnabled() noexcept {
return Kotlin_suspendFunctionsFromAnyThreadFromObjC != 0;
}
ALWAYS_INLINE compiler::GCSchedulerType compiler::getGCSchedulerType() noexcept {
return static_cast<compiler::GCSchedulerType>(Kotlin_gcSchedulerType);
}
@@ -80,6 +80,7 @@ ALWAYS_INLINE inline std::string_view runtimeLogs() noexcept {
bool freezingEnabled() noexcept;
bool freezingChecksEnabled() noexcept;
bool suspendFunctionsFromAnyThreadFromObjCEnabled() noexcept;
ALWAYS_INLINE inline int getSourceInfo(void* addr, SourceInfo *result, int result_size) {
@@ -37,7 +37,7 @@ extern "C" OBJ_GETTER(Kotlin_ObjCExport_createContinuationArgumentImpl,
KRef completionHolder, const TypeInfo** exceptionTypes);
extern "C" OBJ_GETTER(Kotlin_ObjCExport_createContinuationArgument, id completion, const TypeInfo** exceptionTypes) {
if (pthread_main_np() != 1) {
if (!kotlin::compiler::suspendFunctionsFromAnyThreadFromObjCEnabled() && pthread_main_np() != 1) {
[NSException raise:NSGenericException
format:@"Calling Kotlin suspend functions from Swift/Objective-C is currently supported only on main thread"];
}