From 973cc7bbdb0333b4d1e241c59499e88c086fb02c Mon Sep 17 00:00:00 2001 From: SvyatoslavScherbina Date: Fri, 12 Oct 2018 09:28:09 +0300 Subject: [PATCH] Improve support for using frozen objects from other threads (#2202) --- runtime/src/main/cpp/Memory.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/runtime/src/main/cpp/Memory.cpp b/runtime/src/main/cpp/Memory.cpp index 6e3a6e1748f..a406f5bed89 100644 --- a/runtime/src/main/cpp/Memory.cpp +++ b/runtime/src/main/cpp/Memory.cpp @@ -27,6 +27,7 @@ #include "MemoryPrivate.hpp" #include "Natives.h" #include "Porting.h" +#include "Runtime.h" // If garbage collection algorithm for cyclic garbage to be used. // We are using the Bacon's algorithm for GC, see @@ -351,11 +352,6 @@ namespace { // TODO: can we pass this variable as an explicit argument? THREAD_LOCAL_VARIABLE MemoryState* memoryState = nullptr; -// TODO: use widely. -inline void EnsureRuntimeInitialized() { - RuntimeCheck(memoryState != nullptr, "Unable to execute Kotlin code on uninitialized thread"); -} - constexpr int kFrameOverlaySlots = sizeof(FrameOverlay) / sizeof(ObjHeader**); inline bool isFreeable(const ContainerHeader* header) { @@ -420,10 +416,15 @@ void KRefSharedHolder::initRefOwner() { void KRefSharedHolder::verifyRefOwner() const { // Note: checking for 'permanentOrFrozen()' and retrieving 'type_info()' // are supposed to be correct even for unowned object. - if (owner_ != memoryState && !obj_->container()->permanentOrFrozen()) { - EnsureRuntimeInitialized(); - // TODO: add some info about the owner. - ThrowIllegalObjectSharingException(obj_->type_info(), obj_); + if (owner_ != memoryState) { + // Initialized runtime is required to throw the exception below + // or to provide proper execution context for shared objects: + if (memoryState == nullptr) Kotlin_initRuntimeIfNeeded(); + + if (!obj_->container()->permanentOrFrozen()) { + // TODO: add some info about the owner. + ThrowIllegalObjectSharingException(obj_->type_info(), obj_); + } } }