Native: improve thread state switching for Obj-C interop

This commit is contained in:
Svyatoslav Scherbina
2021-06-08 12:34:16 +03:00
committed by Space
parent 85ab4f68df
commit 79d4047e86
4 changed files with 29 additions and 7 deletions
@@ -125,20 +125,21 @@ template <ErrorPolicy errorPolicy>
void BackRefFromAssociatedObject::addRef() {
static_assert(errorPolicy != ErrorPolicy::kDefaultValue, "Cannot use default return value here");
// Can be called both from Native state (if ObjC or Swift code adds RC)
// and from Runnable state (Kotlin_ObjCExport_refToObjC).
if (atomicAdd(&refCount, 1) == 1) {
if (obj_ == nullptr) return; // E.g. after [detach].
kotlin::CalledFromNativeGuard guard(/* reentrant */ true);
// There are no references to the associated object itself, so Kotlin object is being passed from Kotlin,
// and it is owned therefore.
kotlin::AssertThreadState(kotlin::ThreadState::kRunnable);
ensureRefAccessible<errorPolicy>(obj_, context_); // TODO: consider removing explicit verification.
// Foreign reference has already been deinitialized (see [releaseRef]).
// Create a new one:
context_ = InitForeignRef(obj_);
} else {
// Can be called both from Native state (if ObjC or Swift code adds RC)
// and from Runnable state (Kotlin_ObjCExport_refToObjC).
}
}