[K/N][Runtime] Separate thread registering and making it 'Runnable'

The new GC will require threads to suspend right after registering
if Stop-The-World is requested. This patch changes the initial
thread state to kNative and adds a separate state switch right
after thread registering. This switch suspends if it is necessary.
This commit is contained in:
Ilya Matveev
2021-06-02 16:56:10 +07:00
committed by Space
parent e240b8a8ee
commit cece652412
11 changed files with 100 additions and 24 deletions
@@ -95,8 +95,13 @@ extern "C" MemoryState* InitMemory(bool firstRuntime) {
}
extern "C" void DeinitMemory(MemoryState* state, bool destroyRuntime) {
// We need the native state to avoid a deadlock on unregistering the thread.
// The deadlock is possible if we are in the runnable state and the GC already locked
// the thread registery and waits for threads to suspend or go to the native state.
AssertThreadState(state, ThreadState::kNative);
auto* node = mm::FromMemoryState(state);
if (destroyRuntime) {
ThreadStateGuard guard(state, ThreadState::kRunnable);
node->Get()->gc().PerformFullGC();
// TODO: Also make sure that finalizers are run.
}