Fix unchecked runtime shutdown (#4575)

This commit is contained in:
Alexander Shabalin
2020-12-08 10:17:52 +03:00
committed by Stanislav Erokhin
parent b6497d07cf
commit 783517dbe8
7 changed files with 106 additions and 4 deletions
@@ -0,0 +1,23 @@
import leakMemory.*
import kotlin.native.concurrent.*
import kotlin.test.*
import kotlinx.cinterop.*
val global = AtomicInt(0)
fun ensureInititalized() {
kotlin.native.initRuntimeIfNeeded()
// Leak memory
StableRef.create(Any())
global.value = 1
}
fun main() {
kotlin.native.internal.Debugging.forceCheckedShutdown = true
assertTrue(global.value == 0)
// Created a thread, made sure Kotlin is initialized there.
test_RunInNewThread(staticCFunction(::ensureInititalized))
assertTrue(global.value == 1)
// Now exiting. With checked shutdown we will fail, complaining there're
// unfinished threads with runtimes.
}
@@ -0,0 +1,15 @@
#include "leakMemory.h"
#include <atomic>
#include <thread>
extern "C" void test_RunInNewThread(void (*f)()) {
std::atomic<bool> haveRun(false);
std::thread t([f, &haveRun]() {
f();
haveRun = true;
while (true) {}
});
t.detach();
while (!haveRun) {}
}
@@ -0,0 +1,5 @@
package leakMemory
---
void test_RunInNewThread(void (*)());
@@ -0,0 +1,9 @@
#ifdef __cplusplus
extern "C" {
#endif
void test_RunInNewThread(void (*)());
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,23 @@
import leakMemory.*
import kotlin.native.concurrent.*
import kotlin.test.*
import kotlinx.cinterop.*
val global = AtomicInt(0)
fun ensureInititalized() {
kotlin.native.initRuntimeIfNeeded()
// Leak memory
StableRef.create(Any())
global.value = 1
}
fun main() {
kotlin.native.internal.Debugging.forceCheckedShutdown = false
assertTrue(global.value == 0)
// Created a thread, made sure Kotlin is initialized there.
test_RunInNewThread(staticCFunction(::ensureInititalized))
assertTrue(global.value == 1)
// Now exiting. With unchecked shutdown, we exit quietly, even though there're
// unfinished threads with runtimes.
}