Make Kotlin_initRuntimeIfNeeded switch to native state

This commit is contained in:
Alexander Shabalin
2021-05-20 13:48:49 +00:00
committed by Space
parent f40110eb83
commit eac3f507b6
16 changed files with 61 additions and 7 deletions
@@ -74,6 +74,7 @@ namespace {
void launchMain() {
Kotlin_initRuntimeIfNeeded();
Kotlin_mm_switchThreadStateRunnable();
{
ObjHolder args;
@@ -53,6 +53,7 @@ extern "C" RUNTIME_USED int Init_and_run_start(int argc, const char** argv, int
#endif
Kotlin_initRuntimeIfNeeded();
Kotlin_mm_switchThreadStateRunnable();
KInt exitStatus = Konan_run_start(argc, argv);
@@ -19,6 +19,7 @@ inline bool isForeignRefAccessible(ObjHeader* object, ForeignRefContext context)
// If runtime has not been initialized on this thread, then the object is either unowned or shared.
// In the former case initialized runtime is required to throw exceptions
// in the latter case -- to provide proper execution context for caller.
// TODO: Can this be called in uninitialized state in the new MM?
Kotlin_initRuntimeIfNeeded();
return IsForeignRefAccessible(object, context);
@@ -47,6 +47,7 @@ static inline OBJ_GETTER(refFromObjCOrNSNull, id obj) {
static inline OBJ_GETTER(invokeAndAssociate, KRef (*func)(KRef* result), id obj) {
Kotlin_initRuntimeIfNeeded();
// TODO: Does this need a switch to runnable state?
KRef kotlinObj = func(OBJ_RESULT);
@@ -56,4 +57,4 @@ static inline OBJ_GETTER(invokeAndAssociate, KRef (*func)(KRef* result), id obj)
}
#endif // KONAN_OBJC_INTEROP
#endif // RUNTIME_OBJCEXPORTCOLLECTIONS_H
#endif // RUNTIME_OBJCEXPORTCOLLECTIONS_H
+15 -1
View File
@@ -140,6 +140,9 @@ RuntimeState* initRuntime() {
InitOrDeinitGlobalVariables(INIT_THREAD_LOCAL_GLOBALS, result->memoryState);
RuntimeAssert(result->status == RuntimeStatus::kUninitialized, "Runtime must still be in the uninitialized state");
result->status = RuntimeStatus::kRunning;
kotlin::SwitchThreadState(result->memoryState, kotlin::ThreadState::kNative);
return result;
}
@@ -172,6 +175,8 @@ void deinitRuntime(RuntimeState* state, bool destroyRuntime) {
void Kotlin_deinitRuntimeCallback(void* argument) {
auto* state = reinterpret_cast<RuntimeState*>(argument);
// This callback may be called from any state, make sure it runs in the runnable state.
kotlin::SwitchThreadState(state->memoryState, kotlin::ThreadState::kRunnable, /* reentrant = */ true);
deinitRuntime(state, false);
}
@@ -378,9 +383,18 @@ KBoolean Kotlin_Debugging_isThreadStateNative() {
return kotlin::GetThreadState() == kotlin::ThreadState::kNative;
}
KBoolean Kotlin_Debugging_isPermanent(KRef obj) {
return obj->permanent();
}
RUNTIME_NOTHROW void Kotlin_initRuntimeIfNeededFromKotlin() {
switch (CurrentMemoryModel) {
case MemoryModel::kExperimental:
return;
case MemoryModel::kStrict:
case MemoryModel::kRelaxed:
Kotlin_initRuntimeIfNeeded();
}
}
} // extern "C"
@@ -33,6 +33,7 @@ enum DestroyRuntimeMode {
DestroyRuntimeMode Kotlin_getDestroyRuntimeMode();
// For experimental MM, if runtime gets initialized, it will be in the native state after this.
RUNTIME_NOTHROW void Kotlin_initRuntimeIfNeeded();
void Kotlin_deinitRuntimeIfNeeded();
@@ -881,6 +881,7 @@ void* workerRoutine(void* argument) {
// to see there's already a worker created for this thread.
::g_worker = worker;
Kotlin_initRuntimeIfNeeded();
SwitchThreadState(worker->memoryState(), ThreadState::kRunnable);
do {
if (worker->processQueueElement(true) == JOB_TERMINATE) break;
@@ -11,7 +11,7 @@ import kotlin.native.internal.UnhandledExceptionHookHolder
/**
* Initializes Kotlin runtime for the current thread, if not inited already.
*/
@GCUnsafeCall("Kotlin_initRuntimeIfNeeded")
@GCUnsafeCall("Kotlin_initRuntimeIfNeededFromKotlin")
external public fun initRuntimeIfNeeded(): Unit
/**
+2 -1
View File
@@ -13,8 +13,9 @@
#include "InitializationScheme.hpp"
#include "KAssert.h"
#include "Natives.h"
#include "Porting.h"
#include "ObjectOps.hpp"
#include "Porting.h"
#include "Runtime.h"
#include "StableRefRegistry.hpp"
#include "ThreadData.hpp"
#include "ThreadRegistry.hpp"
@@ -61,6 +61,7 @@ static void injectToRuntime();
+(instancetype)allocWithZone:(NSZone*)zone {
Kotlin_initRuntimeIfNeeded();
// TODO: Does this need a switch to runnable state?
KotlinBase* result = [super allocWithZone:zone];
@@ -327,6 +327,7 @@ static inline id KSet_getElement(KRef set, id object) {
-(instancetype)init {
if (self = [super init]) {
Kotlin_initRuntimeIfNeeded();
// TODO: Does this need a switch to runnable state?
ObjHolder holder;
KRef set = Kotlin_MutableSet_createWithCapacity(8, holder.slot());
self->setHolder.init(set);
@@ -338,6 +339,7 @@ static inline id KSet_getElement(KRef set, id object) {
- (instancetype)initWithCapacity:(NSUInteger)numItems {
if (self = [super init]) {
Kotlin_initRuntimeIfNeeded();
// TODO: Does this need a switch to runnable state?
ObjHolder holder;
KRef set = Kotlin_MutableSet_createWithCapacity(objCCapacityToKotlin(numItems), holder.slot());
self->setHolder.init(set);
@@ -483,6 +485,7 @@ static inline id KMap_get(KRef map, id aKey) {
-(instancetype)init {
if (self = [super init]) {
Kotlin_initRuntimeIfNeeded();
// TODO: Does this need a switch to runnable state?
ObjHolder holder;
KRef map = Kotlin_MutableMap_createWithCapacity(8, holder.slot());
self->mapHolder.init(map);
@@ -499,6 +502,7 @@ static inline id KMap_get(KRef map, id aKey) {
- (instancetype)initWithCapacity:(NSUInteger)numItems {
if (self = [super init]) {
Kotlin_initRuntimeIfNeeded();
// TODO: Does this need a switch to runnable state?
ObjHolder holder;
KRef map = Kotlin_MutableMap_createWithCapacity(objCCapacityToKotlin(numItems), holder.slot());
self->mapHolder.init(map);