Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Continuation cleanup API #15876

Merged
merged 2 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private static void execute(Continuation cont) {
cont.runnable.run();
} finally {
cont.finished = true;
yieldImpl(true);
yieldImpl();
}
}

Expand Down Expand Up @@ -211,7 +211,7 @@ public static boolean yield(ContinuationScope scope) {
}
throw new IllegalStateException("Continuation is pinned: " + reason);
}
return yieldImpl(false);
return yieldImpl();
}

protected void onPinned(Pinned reason) {
Expand Down Expand Up @@ -247,6 +247,6 @@ public PreemptStatus tryPreempt(Thread t) {
/* Continuation Native APIs */
private native boolean createContinuationImpl();
private native boolean enterImpl();
private static native boolean yieldImpl(boolean isFinished);
private static native boolean yieldImpl();

}
3 changes: 3 additions & 0 deletions runtime/j9vm/javanextvmi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@ JVM_VirtualThreadUnmountEnd(JNIEnv *env, jobject thread, jboolean lastUnmount)

if (lastUnmount) {
vmFuncs->freeTLS(currentThread, threadObj);
/* CleanupContinuation */
j9object_t contObj = (j9object_t)J9VMJAVALANGVIRTUALTHREAD_CONT(currentThread, threadObj);
vmFuncs->freeContinuation(currentThread, contObj);
}

/* Allow thread to be inspected again. */
Expand Down
6 changes: 1 addition & 5 deletions runtime/oti/VMHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2072,11 +2072,7 @@ class VM_VMHelpers
cleanupContinuationObject(J9VMThread *vmThread, J9Object *objectPtr)
{
#if JAVA_SPEC_VERSION >= 19
J9VMContinuation *j9vmContinuation = J9VMJDKINTERNALVMCONTINUATION_VMREF(vmThread, objectPtr);
if (NULL != j9vmContinuation) {
/* clean up J9VMContinuation, set vmref = NULL */
J9VMJDKINTERNALVMCONTINUATION_SET_VMREF(vmThread, objectPtr, NULL);
}
vmThread->javaVM->internalVMFunctions->freeContinuation(vmThread, objectPtr);
#endif /* JAVA_SPEC_VERSION >= 19 */
}
};
Expand Down
7 changes: 3 additions & 4 deletions runtime/oti/j9nonbuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -4890,10 +4890,6 @@ typedef struct J9InternalVMFunctions {
#endif /* defined(J9VM_OPT_CRIU_SUPPORT) */
j9object_t (*getClassNameString)(struct J9VMThread *currentThread, j9object_t classObject, jboolean internAndAssign);
j9object_t* (*getDefaultValueSlotAddress)(struct J9Class *clazz);
#if JAVA_SPEC_VERSION >= 19
BOOLEAN (*createContinuation)(struct J9VMThread *currentThread, j9object_t continuationObject);
void (*freeTLS)(struct J9VMThread *currentThread, j9object_t threadObj);
#endif /* JAVA_SPEC_VERSION >= 19 */
#if JAVA_SPEC_VERSION >= 16
void * ( *createUpcallThunk)(struct J9UpcallMetaData *data);
void * ( *getArgPointer)(struct J9UpcallNativeSignature *nativeSig, void *argListPtr, int argIdx);
Expand All @@ -4907,6 +4903,9 @@ typedef struct J9InternalVMFunctions {
U_8 * (JNICALL *native2InterpJavaUpcallStruct)(struct J9UpcallMetaData *data, void *argsListPointer);
#endif /* JAVA_SPEC_VERSION >= 16 */
#if JAVA_SPEC_VERSION >= 19
BOOLEAN (*createContinuation)(struct J9VMThread *currentThread, j9object_t continuationObject);
void (*freeContinuation)(struct J9VMThread *currentThread, j9object_t continuationObject);
void (*freeTLS)(struct J9VMThread *currentThread, j9object_t threadObj);
UDATA (*walkContinuationStackFrames)(struct J9VMThread *currentThread, struct J9VMContinuation *continuation, J9StackWalkState *walkState);
#endif /* JAVA_SPEC_VERSION >= 19 */
} J9InternalVMFunctions;
Expand Down
9 changes: 9 additions & 0 deletions runtime/oti/vm_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -4335,6 +4335,15 @@ enterContinuation(struct J9VMThread *currentThread, j9object_t continuationObjec
BOOLEAN
yieldContinuation(struct J9VMThread *currentThread);

/**
* @brief Free the native memory allocated by Continuation
*
* @param currentThread the thread unmounting Continuation.
* @param continuationObject
*/
void
freeContinuation(J9VMThread *currentThread, j9object_t continuationObject);

/**
* Determine if the current continuation is pinned.
*
Expand Down
6 changes: 1 addition & 5 deletions runtime/vm/BytecodeInterpreter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5193,21 +5193,17 @@ class INTERPRETER_CLASS
return rc;
}

/* jdk.internal.vm.Continuation: private static native boolean yieldImpl(boolean isFinished); */
/* jdk.internal.vm.Continuation: private static native boolean yieldImpl(); */
VMINLINE VM_BytecodeAction
yieldContinuationImpl(REGISTER_ARGS_LIST)
{
VM_BytecodeAction rc = EXECUTE_BYTECODE;
UDATA isFinished = *(I_32*)_sp;

buildInternalNativeStackFrame(REGISTER_ARGS);
updateVMStruct(REGISTER_ARGS);

/* store the current Continuation state and swap to carrier thread stack */
yieldContinuation(_currentThread);
if (isFinished) {
/* CleanupContinuation */
}

VMStructHasBeenUpdated(REGISTER_ARGS);
restoreInternalNativeStackFrame(REGISTER_ARGS);
Expand Down
27 changes: 25 additions & 2 deletions runtime/vm/ContinuationHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ enterContinuation(J9VMThread *currentThread, j9object_t continuationObject)
currentThread->callOutCount = 0;

if (started) {
/* resuming Continuation from yield */
/* resuming Continuation from yieldImpl */
VM_OutOfLineINL_Helpers::restoreInternalNativeStackFrame(currentThread);
VM_OutOfLineINL_Helpers::returnSingle(currentThread, JNI_TRUE, 1);
VM_OutOfLineINL_Helpers::returnSingle(currentThread, JNI_TRUE, 0);
result = FALSE;
} else {
/* start new Continuation execution */
Expand Down Expand Up @@ -147,6 +147,29 @@ yieldContinuation(J9VMThread *currentThread)
return result;
}

void
freeContinuation(J9VMThread *currentThread, j9object_t continuationObject)
{
J9VMContinuation *continuation = J9VMJDKINTERNALVMCONTINUATION_VMREF(currentThread, continuationObject);
if (NULL != continuation) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible for this to be NULL

Copy link
Contributor Author

@fengxue-IS fengxue-IS Sep 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this continuation is freed after termination, GC can also call free second time when the continuation object is no longer referenced via the VMHelper wrapper.

PORT_ACCESS_FROM_VMC(currentThread);
/* Free all stack used by continuation */
J9JavaStack *currentStack = continuation->stackObject;
do {
J9JavaStack *previous = currentStack->previous;

freeJavaStack(currentThread->javaVM, currentStack);
currentStack = previous;
} while (NULL != currentStack);

/* Free the J9VMContinuation struct */
j9mem_free_memory(continuation);

/* Update Continuation object's vmRef field */
J9VMJDKINTERNALVMCONTINUATION_SET_VMREF(currentThread, continuationObject, NULL);
}
}

jint
isPinnedContinuation(J9VMThread *currentThread)
{
Expand Down
2 changes: 1 addition & 1 deletion runtime/vm/bindnatv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ static inlMapping mappings[] = {
#endif /* JAVA_SPEC_VERSION >= 16 */
#if JAVA_SPEC_VERSION >= 19
{ "Java_jdk_internal_vm_Continuation_enterImpl__", J9_BCLOOP_SEND_TARGET_ENTER_CONTINUATION },
{ "Java_jdk_internal_vm_Continuation_yieldImpl__Z", J9_BCLOOP_SEND_TARGET_YIELD_CONTINUATION },
{ "Java_jdk_internal_vm_Continuation_yieldImpl__", J9_BCLOOP_SEND_TARGET_YIELD_CONTINUATION },
{ "Java_jdk_internal_vm_Continuation_isPinnedImpl__", J9_BCLOOP_SEND_TARGET_ISPINNED_CONTINUATION },
#endif /* JAVA_SPEC_VERSION >= 19 */
};
Expand Down
7 changes: 3 additions & 4 deletions runtime/vm/intfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,6 @@ J9InternalVMFunctions J9InternalFunctions = {
#endif /* defined(J9VM_OPT_CRIU_SUPPORT) */
getClassNameString,
getDefaultValueSlotAddress,
#if JAVA_SPEC_VERSION >= 19
createContinuation,
freeTLS,
#endif /* JAVA_SPEC_VERSION >= 19 */
#if JAVA_SPEC_VERSION >= 16
createUpcallThunk,
getArgPointer,
Expand All @@ -432,6 +428,9 @@ J9InternalVMFunctions J9InternalFunctions = {
native2InterpJavaUpcallStruct,
#endif /* JAVA_SPEC_VERSION >= 16 */
#if JAVA_SPEC_VERSION >= 19
createContinuation,
freeContinuation,
freeTLS,
walkContinuationStackFrames,
#endif /* JAVA_SPEC_VERSION >= 19 */
};