From bae42b7eee26c16db4769ee1ea83f2193847d691 Mon Sep 17 00:00:00 2001 From: Babneet Singh Date: Tue, 8 Nov 2022 13:40:06 -0500 Subject: [PATCH] Delay appending VirtualThread objects to liveVirtualThreadList Currently, VirtualThread objects are appended to liveVirtualThreadList in JVM_VirtualThreadMountBegin. To assist GC with synchronizing while scanning continuations and VirtualThread objects, VirtualThread objects are appended to liveVirtualThreadList in JVM_VirtualThreadMountEnd. This delay will prevent race conditions for the GC while scanning the VirtualThread objects. Related: #16259 Signed-off-by: Babneet Singh --- runtime/j9vm/javanextvmi.cpp | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/runtime/j9vm/javanextvmi.cpp b/runtime/j9vm/javanextvmi.cpp index 0f1498e04f3..18dc3520700 100644 --- a/runtime/j9vm/javanextvmi.cpp +++ b/runtime/j9vm/javanextvmi.cpp @@ -280,6 +280,21 @@ JVM_VirtualThreadMountBegin(JNIEnv *env, jobject thread, jboolean firstMount) */ J9OBJECT_I64_STORE(currentThread, threadObj, vm->virtualThreadInspectorCountOffset, -1); + f_monitorExit(vm->liveVirtualThreadListMutex); + vmFuncs->internalExitVMToJNI(currentThread); +} + +JNIEXPORT void JNICALL +JVM_VirtualThreadMountEnd(JNIEnv *env, jobject thread, jboolean firstMount) +{ + J9VMThread *currentThread = (J9VMThread *)env; + J9JavaVM *vm = currentThread->javaVM; + J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions; + + vmFuncs->internalEnterVMFromJNI(currentThread); + f_monitorEnter(vm->liveVirtualThreadListMutex); + j9object_t threadObj = J9_JNI_UNWRAP_REFERENCE(thread); + if (firstMount) { if (NULL == vm->liveVirtualThreadList) { J9Class *virtualThreadClass = J9OBJECT_CLAZZ(currentThread, J9_JNI_UNWRAP_REFERENCE(thread)); @@ -316,24 +331,7 @@ JVM_VirtualThreadMountBegin(JNIEnv *env, jobject thread, jboolean firstMount) J9OBJECT_OBJECT_STORE(currentThread, rootPrev, vm->virtualThreadLinkNextOffset, threadObj); J9OBJECT_OBJECT_STORE(currentThread, root, vm->virtualThreadLinkPreviousOffset, threadObj); } - } - f_monitorExit(vm->liveVirtualThreadListMutex); - vmFuncs->internalExitVMToJNI(currentThread); -} - -JNIEXPORT void JNICALL -JVM_VirtualThreadMountEnd(JNIEnv *env, jobject thread, jboolean firstMount) -{ - J9VMThread *currentThread = (J9VMThread *)env; - J9JavaVM *vm = currentThread->javaVM; - J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions; - - vmFuncs->internalEnterVMFromJNI(currentThread); - f_monitorEnter(vm->liveVirtualThreadListMutex); - j9object_t threadObj = J9_JNI_UNWRAP_REFERENCE(thread); - - if (firstMount) { TRIGGER_J9HOOK_VM_VIRTUAL_THREAD_STARTED(vm->hookInterface, currentThread); }