diff --git a/runtime/src/launcher/cpp/launcher.cpp b/runtime/src/launcher/cpp/launcher.cpp index ca80467efed..2641cf67595 100644 --- a/runtime/src/launcher/cpp/launcher.cpp +++ b/runtime/src/launcher/cpp/launcher.cpp @@ -45,15 +45,11 @@ extern "C" KInt Konan_run_start(int argc, const char** argv) { } extern "C" RUNTIME_USED int Konan_main(int argc, const char** argv) { - RuntimeState* state = InitRuntime(); - - if (state == nullptr) { - return 2; - } + Kotlin_initRuntimeIfNeeded(); KInt exitStatus = Konan_run_start(argc, argv); - DeinitRuntime(state); + Kotlin_deinitRuntimeIfNeeded(); return exitStatus; } diff --git a/runtime/src/main/cpp/Runtime.cpp b/runtime/src/main/cpp/Runtime.cpp index 5f7541314eb..73e98353545 100644 --- a/runtime/src/main/cpp/Runtime.cpp +++ b/runtime/src/main/cpp/Runtime.cpp @@ -45,6 +45,24 @@ void InitOrDeinitGlobalVariables(int initialize) { THREAD_LOCAL_VARIABLE RuntimeState* runtimeState = nullptr; +RuntimeState* initRuntime() { + SetKonanTerminateHandler(); + RuntimeState* result = konanConstructInstance(); + if (!result) return nullptr; + result->memoryState = InitMemory(); + // Keep global variables in state as well. + InitOrDeinitGlobalVariables(true); + konan::consoleInit(); + return result; +} + +void deinitRuntime(RuntimeState* state) { + if (state != nullptr) { + InitOrDeinitGlobalVariables(false); + DeinitMemory(state->memoryState); + konanDestructInstance(state); + } +} } // namespace extern "C" { @@ -59,29 +77,9 @@ void AppendToInitializersTail(InitNode *next) { initTailNode = next; } -// TODO: properly use RuntimeState. -RuntimeState* InitRuntime() { - SetKonanTerminateHandler(); - RuntimeState* result = konanConstructInstance(); - result->memoryState = InitMemory(); - // Keep global variables in state as well. - InitOrDeinitGlobalVariables(true); - konan::consoleInit(); - runtimeState = result; - return result; -} - -void DeinitRuntime(RuntimeState* state) { - if (state != nullptr) { - InitOrDeinitGlobalVariables(false); - DeinitMemory(state->memoryState); - konanDestructInstance(state); - } -} - void Kotlin_initRuntimeIfNeeded() { if (runtimeState == nullptr) { - runtimeState = InitRuntime(); + runtimeState = initRuntime(); // Register runtime deinit function at thread cleanup. konan::onThreadExit(Kotlin_deinitRuntimeIfNeeded); #ifndef KONAN_WASM @@ -98,7 +96,7 @@ void Kotlin_initRuntimeIfNeeded() { void Kotlin_deinitRuntimeIfNeeded() { if (runtimeState != nullptr) { - DeinitRuntime(runtimeState); + deinitRuntime(runtimeState); runtimeState = nullptr; } } diff --git a/runtime/src/main/cpp/Runtime.h b/runtime/src/main/cpp/Runtime.h index 59e237c8134..a097a4f2c6b 100644 --- a/runtime/src/main/cpp/Runtime.h +++ b/runtime/src/main/cpp/Runtime.h @@ -24,8 +24,6 @@ struct InitNode; extern "C" { #endif -RuntimeState* InitRuntime(); -void DeinitRuntime(RuntimeState* state); void Kotlin_initRuntimeIfNeeded(); void Kotlin_deinitRuntimeIfNeeded(); diff --git a/runtime/src/main/cpp/Worker.cpp b/runtime/src/main/cpp/Worker.cpp index 659462ee7d5..5f97b6fe59f 100644 --- a/runtime/src/main/cpp/Worker.cpp +++ b/runtime/src/main/cpp/Worker.cpp @@ -364,7 +364,7 @@ extern "C" void ReportUnhandledException(KRef e); void* workerRoutine(void* argument) { Worker* worker = reinterpret_cast(argument); - RuntimeState* state = InitRuntime(); + Kotlin_initRuntimeIfNeeded(); while (true) { Job job = worker->getJob(); if (job.function == nullptr) { @@ -391,7 +391,7 @@ void* workerRoutine(void* argument) { job.future->storeResultUnlocked(result); } - DeinitRuntime(state); + Kotlin_deinitRuntimeIfNeeded(); konanDestructInstance(worker); diff --git a/samples/videoplayer/src/main/kotlin/DecoderWorker.kt b/samples/videoplayer/src/main/kotlin/DecoderWorker.kt index 7a91d805aeb..2a596d3519b 100644 --- a/samples/videoplayer/src/main/kotlin/DecoderWorker.kt +++ b/samples/videoplayer/src/main/kotlin/DecoderWorker.kt @@ -393,9 +393,9 @@ class DecodeWorker { fun nextAudioFrame(size: Int) = decodeWorker.schedule( TransferMode.CHECKED, - { size as Int? }) { input -> state!!.nextAudioFrame(input!!) }.result() + { size }) { input -> state!!.nextAudioFrame(input) }.result() fun audioVideoSynced() = decodeWorker.schedule( TransferMode.CHECKED, - { null }) { _ -> state!!.audioVideoSynced() as Boolean? }.consume { it -> it!! } + { null }) { _ -> state!!.audioVideoSynced() }.result() }