Rework runtime init (#1127)

This commit is contained in:
Nikolay Igotti
2017-12-11 15:09:03 +03:00
committed by GitHub
parent ccc630d528
commit e80f7421c7
5 changed files with 26 additions and 34 deletions
+2 -6
View File
@@ -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;
}
+20 -22
View File
@@ -45,6 +45,24 @@ void InitOrDeinitGlobalVariables(int initialize) {
THREAD_LOCAL_VARIABLE RuntimeState* runtimeState = nullptr;
RuntimeState* initRuntime() {
SetKonanTerminateHandler();
RuntimeState* result = konanConstructInstance<RuntimeState>();
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<RuntimeState>();
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;
}
}
-2
View File
@@ -24,8 +24,6 @@ struct InitNode;
extern "C" {
#endif
RuntimeState* InitRuntime();
void DeinitRuntime(RuntimeState* state);
void Kotlin_initRuntimeIfNeeded();
void Kotlin_deinitRuntimeIfNeeded();
+2 -2
View File
@@ -364,7 +364,7 @@ extern "C" void ReportUnhandledException(KRef e);
void* workerRoutine(void* argument) {
Worker* worker = reinterpret_cast<Worker*>(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);
@@ -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()
}