Rework runtime init (#1127)
This commit is contained in:
@@ -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) {
|
extern "C" RUNTIME_USED int Konan_main(int argc, const char** argv) {
|
||||||
RuntimeState* state = InitRuntime();
|
Kotlin_initRuntimeIfNeeded();
|
||||||
|
|
||||||
if (state == nullptr) {
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
KInt exitStatus = Konan_run_start(argc, argv);
|
KInt exitStatus = Konan_run_start(argc, argv);
|
||||||
|
|
||||||
DeinitRuntime(state);
|
Kotlin_deinitRuntimeIfNeeded();
|
||||||
|
|
||||||
return exitStatus;
|
return exitStatus;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,6 +45,24 @@ void InitOrDeinitGlobalVariables(int initialize) {
|
|||||||
|
|
||||||
THREAD_LOCAL_VARIABLE RuntimeState* runtimeState = nullptr;
|
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
|
} // namespace
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@@ -59,29 +77,9 @@ void AppendToInitializersTail(InitNode *next) {
|
|||||||
initTailNode = 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() {
|
void Kotlin_initRuntimeIfNeeded() {
|
||||||
if (runtimeState == nullptr) {
|
if (runtimeState == nullptr) {
|
||||||
runtimeState = InitRuntime();
|
runtimeState = initRuntime();
|
||||||
// Register runtime deinit function at thread cleanup.
|
// Register runtime deinit function at thread cleanup.
|
||||||
konan::onThreadExit(Kotlin_deinitRuntimeIfNeeded);
|
konan::onThreadExit(Kotlin_deinitRuntimeIfNeeded);
|
||||||
#ifndef KONAN_WASM
|
#ifndef KONAN_WASM
|
||||||
@@ -98,7 +96,7 @@ void Kotlin_initRuntimeIfNeeded() {
|
|||||||
|
|
||||||
void Kotlin_deinitRuntimeIfNeeded() {
|
void Kotlin_deinitRuntimeIfNeeded() {
|
||||||
if (runtimeState != nullptr) {
|
if (runtimeState != nullptr) {
|
||||||
DeinitRuntime(runtimeState);
|
deinitRuntime(runtimeState);
|
||||||
runtimeState = nullptr;
|
runtimeState = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,8 +24,6 @@ struct InitNode;
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
RuntimeState* InitRuntime();
|
|
||||||
void DeinitRuntime(RuntimeState* state);
|
|
||||||
void Kotlin_initRuntimeIfNeeded();
|
void Kotlin_initRuntimeIfNeeded();
|
||||||
void Kotlin_deinitRuntimeIfNeeded();
|
void Kotlin_deinitRuntimeIfNeeded();
|
||||||
|
|
||||||
|
|||||||
@@ -364,7 +364,7 @@ extern "C" void ReportUnhandledException(KRef e);
|
|||||||
void* workerRoutine(void* argument) {
|
void* workerRoutine(void* argument) {
|
||||||
Worker* worker = reinterpret_cast<Worker*>(argument);
|
Worker* worker = reinterpret_cast<Worker*>(argument);
|
||||||
|
|
||||||
RuntimeState* state = InitRuntime();
|
Kotlin_initRuntimeIfNeeded();
|
||||||
while (true) {
|
while (true) {
|
||||||
Job job = worker->getJob();
|
Job job = worker->getJob();
|
||||||
if (job.function == nullptr) {
|
if (job.function == nullptr) {
|
||||||
@@ -391,7 +391,7 @@ void* workerRoutine(void* argument) {
|
|||||||
job.future->storeResultUnlocked(result);
|
job.future->storeResultUnlocked(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
DeinitRuntime(state);
|
Kotlin_deinitRuntimeIfNeeded();
|
||||||
|
|
||||||
konanDestructInstance(worker);
|
konanDestructInstance(worker);
|
||||||
|
|
||||||
|
|||||||
@@ -393,9 +393,9 @@ class DecodeWorker {
|
|||||||
|
|
||||||
fun nextAudioFrame(size: Int) = decodeWorker.schedule(
|
fun nextAudioFrame(size: Int) = decodeWorker.schedule(
|
||||||
TransferMode.CHECKED,
|
TransferMode.CHECKED,
|
||||||
{ size as Int? }) { input -> state!!.nextAudioFrame(input!!) }.result()
|
{ size }) { input -> state!!.nextAudioFrame(input) }.result()
|
||||||
|
|
||||||
fun audioVideoSynced() = decodeWorker.schedule(
|
fun audioVideoSynced() = decodeWorker.schedule(
|
||||||
TransferMode.CHECKED,
|
TransferMode.CHECKED,
|
||||||
{ null }) { _ -> state!!.audioVideoSynced() as Boolean? }.consume { it -> it!! }
|
{ null }) { _ -> state!!.audioVideoSynced() }.result()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user