Skip to content

Commit

Permalink
Store J9VMThread->scopedValueCache in the Continuation object
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
babsingh committed Aug 31, 2023
1 parent 022a2a4 commit 92cb9c9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion runtime/oti/ContinuationHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down Expand Up @@ -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;

This comment has been minimized.

Copy link
@tajila

tajila Sep 5, 2023

Contributor

@dmitripivkine do we need to use a barrier for this write to j9vmthread?

This comment has been minimized.

Copy link
@dmitripivkine

dmitripivkine Sep 5, 2023

Contributor

@dmitripivkine do we need to use a barrier for this write to j9vmthread?

@amicic Would you please help?

This comment has been minimized.

Copy link
@amicic

amicic Sep 5, 2023

Contributor

Not on thread (or more generally hard root) slots.

Yes on continuation (or more generally any heap) object slots, but I presume the GET/SET macros already do it.

}

static VMINLINE ContinuationState volatile *
Expand Down
1 change: 1 addition & 0 deletions runtime/oti/vmconstantpool.xml
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-ex
<fieldref class="jdk/internal/vm/Continuation" name="state" signature="J" versions="19-"/>
<fieldref class="jdk/internal/vm/Continuation" name="parent" signature="Ljdk/internal/vm/Continuation;" versions="19-"/>
<fieldref class="jdk/internal/vm/Continuation" name="vthread" signature="Ljava/lang/Thread;" versions="19-"/>
<fieldref class="jdk/internal/vm/Continuation" name="scopedValueCache" signature="[Ljava/lang/Object;" versions="19-"/>

<!-- Field references needed to support Foreign Linker API. -->
<fieldref class="openj9/internal/foreign/abi/InternalDowncallHandler" name="cifNativeThunkAddr" signature="J" flags="opt_openjdkFfi" versions="17-"/>
Expand Down
4 changes: 2 additions & 2 deletions runtime/vm/ContinuationHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 92cb9c9

Please sign in to comment.