From 8c5d7a0202f89a98f1a2f3e0bacee3f043d97b24 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Mon, 11 Sep 2017 09:15:22 +0300 Subject: [PATCH] Added runtime init (#846) --- runtime/src/main/cpp/Runtime.cpp | 15 +++++++++++++++ runtime/src/main/kotlin/konan/runtime.kt | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 runtime/src/main/kotlin/konan/runtime.kt diff --git a/runtime/src/main/cpp/Runtime.cpp b/runtime/src/main/cpp/Runtime.cpp index 8b958813784..cea7cbc2f12 100644 --- a/runtime/src/main/cpp/Runtime.cpp +++ b/runtime/src/main/cpp/Runtime.cpp @@ -45,6 +45,8 @@ void InitOrDeinitGlobalVariables(int initialize) { } } +THREAD_LOCAL_VARIABLE RuntimeState* runtimeState = nullptr; + } // namespace extern "C" { @@ -67,6 +69,7 @@ RuntimeState* InitRuntime() { // Keep global variables in state as well. InitOrDeinitGlobalVariables(true); konan::consoleInit(); + runtimeState = result; return result; } @@ -78,4 +81,16 @@ void DeinitRuntime(RuntimeState* state) { } } +void Kotlin_initRuntimeIfNeeded() { + if (runtimeState == nullptr) + runtimeState = InitRuntime(); +} + +void Kotlin_deinitRuntimeIfNeeded() { + if (runtimeState != nullptr) { + DeinitRuntime(runtimeState); + runtimeState = nullptr; + } +} + } // extern "C" diff --git a/runtime/src/main/kotlin/konan/runtime.kt b/runtime/src/main/kotlin/konan/runtime.kt new file mode 100644 index 00000000000..17cfed65661 --- /dev/null +++ b/runtime/src/main/kotlin/konan/runtime.kt @@ -0,0 +1,24 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package konan + +// Initializes Kotlin runtime for the current thread, if not inited already. +@SymbolName("Kotlin_initRuntimeIfNeeded") +external public fun initRuntimeIfNeeded(): Unit + +// Deiitializes Kotlin runtime for the current thread, if was inited. +@SymbolName("Kotlin_deinitRuntimeIfNeeded") +external public fun deinitRuntimeIfNeeded(): Unit