From 92cb9c95bbe6141a5e25e65355a85bf1ae24c569 Mon Sep 17 00:00:00 2001 From: Babneet Singh Date: Wed, 30 Aug 2023 19:31:10 -0400 Subject: [PATCH] Store J9VMThread->scopedValueCache in the Continuation object When a virtual thread is mounted, the corresponding carrier thread unmounts and no longer runs. Similarly, when a virtual thread is unmounted, the corresponding carrier thread mounts and resumes its operations. When a virtual thread is mounted, the scopedValueCache for the carrier thread is stored in the Continuation object's scopedValueCache field. When a virtual thread is unmounted, the carrier thread's scopedValueCache, which is stored in the Continuation object's scopedValueCache field, is swapped with J9VMThread->scopedValueCache. This assures a one to one mapping between a java.lang.Thread and scopedValueCache. The live thread's scopedValueCache is always kept in J9VMThread->scopedValueCache whereas the unmounted thread's scopedValueCache is stored in the corresponding Continuation object's scopedValueCache field. Signed-off-by: Babneet Singh --- .../share/classes/jdk/internal/vm/Continuation.java | 7 +++++++ runtime/oti/ContinuationHelpers.hpp | 6 +++++- runtime/oti/vmconstantpool.xml | 1 + runtime/vm/ContinuationHelpers.cpp | 4 ++-- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/jcl/src/java.base/share/classes/jdk/internal/vm/Continuation.java b/jcl/src/java.base/share/classes/jdk/internal/vm/Continuation.java index aa986cd8cdc..e0e4c611c51 100644 --- a/jcl/src/java.base/share/classes/jdk/internal/vm/Continuation.java +++ b/jcl/src/java.base/share/classes/jdk/internal/vm/Continuation.java @@ -37,6 +37,13 @@ public class Continuation { private long vmRef; /* J9VMContinuation */ protected Thread vthread; /* Parent VirtualThread */ + /* The live thread's scopedValueCache is always kept in J9VMThread->scopedValueCache + * whereas the unmounted thread's scopedValueCache is stored in this field. This + * field is modified in ContinuationHelpers.hpp::swapFieldsWithContinuation. This + * assures a one to one mapping between a java.lang.Thread and scopedValueCache. + */ + private Object[] scopedValueCache; + private final ContinuationScope scope; private final Runnable runnable; private Continuation parent; diff --git a/runtime/oti/ContinuationHelpers.hpp b/runtime/oti/ContinuationHelpers.hpp index 946825138b6..02a75e40888 100644 --- a/runtime/oti/ContinuationHelpers.hpp +++ b/runtime/oti/ContinuationHelpers.hpp @@ -51,7 +51,7 @@ class VM_ContinuationHelpers { public: static VMINLINE void - swapFieldsWithContinuation(J9VMThread *vmThread, J9VMContinuation *continuation, bool swapJ9VMthreadSavedRegisters = true) + swapFieldsWithContinuation(J9VMThread *vmThread, J9VMContinuation *continuation, j9object_t continuationObject, bool swapJ9VMthreadSavedRegisters = true) { /* Helper macro to swap fields between the two J9Class structs. */ #define SWAP_MEMBER(fieldName, fieldType, class1, class2) \ @@ -84,6 +84,10 @@ class VM_ContinuationHelpers { } threadELS->i2jState = tempI2J; SWAP_MEMBER(oldEntryLocalStorage, J9VMEntryLocalStorage*, threadELS, continuation); + + j9object_t scopedValueCache = J9VMJDKINTERNALVMCONTINUATION_SCOPEDVALUECACHE(vmThread, continuationObject); + J9VMJDKINTERNALVMCONTINUATION_SET_SCOPEDVALUECACHE(vmThread, continuationObject, vmThread->scopedValueCache); + vmThread->scopedValueCache = scopedValueCache; } static VMINLINE ContinuationState volatile * diff --git a/runtime/oti/vmconstantpool.xml b/runtime/oti/vmconstantpool.xml index efd48dae710..6049e0c5f5f 100644 --- a/runtime/oti/vmconstantpool.xml +++ b/runtime/oti/vmconstantpool.xml @@ -413,6 +413,7 @@ SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-ex + diff --git a/runtime/vm/ContinuationHelpers.cpp b/runtime/vm/ContinuationHelpers.cpp index 692574afef4..a350b848754 100644 --- a/runtime/vm/ContinuationHelpers.cpp +++ b/runtime/vm/ContinuationHelpers.cpp @@ -233,7 +233,7 @@ enterContinuation(J9VMThread *currentThread, j9object_t continuationObject) currentThread->javaVM->memoryManagerFunctions->preMountContinuation(currentThread, continuationObject); } - VM_ContinuationHelpers::swapFieldsWithContinuation(currentThread, continuation, started); + VM_ContinuationHelpers::swapFieldsWithContinuation(currentThread, continuation, continuationObject, started); currentThread->currentContinuation = continuation; /* Reset counters which determine if the current continuation is pinned. */ @@ -285,7 +285,7 @@ yieldContinuation(J9VMThread *currentThread, BOOLEAN isFinished) } currentThread->currentContinuation = NULL; - VM_ContinuationHelpers::swapFieldsWithContinuation(currentThread, continuation); + VM_ContinuationHelpers::swapFieldsWithContinuation(currentThread, continuation, continuationObject); /* We need a full fence here to preserve happens-before relationship on PPC and other weakly * ordered architectures since learning/reservation is turned on by default. Since we have the