Skip to content

Commit

Permalink
8306028: separate ThreadStart/ThreadEnd events posting code in JVMTI …
Browse files Browse the repository at this point in the history
…VTMS transitions

8304444: Reappearance of NULL in jvmtiThreadState.cpp

Reviewed-by: pchilanomate, lmesnik
  • Loading branch information
Serguei Spitsyn committed May 2, 2023
1 parent dda3abc commit 0bd6a00
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
4 changes: 3 additions & 1 deletion make/data/hotspot-symbols/symbols-unix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -217,6 +217,8 @@ JVM_DefineModule
JVM_SetBootLoaderUnnamedModule

# Virtual thread notifications for JVMTI
JVM_VirtualThreadStart
JVM_VirtualThreadEnd
JVM_VirtualThreadMount
JVM_VirtualThreadUnmount
JVM_VirtualThreadHideFrames
Expand Down
10 changes: 8 additions & 2 deletions src/hotspot/share/include/jvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -1144,10 +1144,16 @@ JVM_GetEnclosingMethodInfo(JNIEnv* env, jclass ofClass);
* Virtual thread support.
*/
JNIEXPORT void JNICALL
JVM_VirtualThreadMount(JNIEnv* env, jobject vthread, jboolean hide, jboolean first_mount);
JVM_VirtualThreadStart(JNIEnv* env, jobject vthread);

JNIEXPORT void JNICALL
JVM_VirtualThreadUnmount(JNIEnv* env, jobject vthread, jboolean hide, jboolean last_unmount);
JVM_VirtualThreadEnd(JNIEnv* env, jobject vthread);

JNIEXPORT void JNICALL
JVM_VirtualThreadMount(JNIEnv* env, jobject vthread, jboolean hide);

JNIEXPORT void JNICALL
JVM_VirtualThreadUnmount(JNIEnv* env, jobject vthread, jboolean hide);

JNIEXPORT void JNICALL
JVM_VirtualThreadHideFrames(JNIEnv* env, jobject vthread, jboolean hide);
Expand Down
31 changes: 18 additions & 13 deletions src/java.base/share/classes/java/lang/VirtualThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,22 +205,19 @@ private void runContinuation() {
}

// set state to RUNNING
boolean firstRun;
int initialState = state();
if (initialState == STARTED && compareAndSetState(STARTED, RUNNING)) {
// first run
firstRun = true;
} else if (initialState == RUNNABLE && compareAndSetState(RUNNABLE, RUNNING)) {
// consume parking permit
setParkPermit(false);
firstRun = false;
} else {
// not runnable
return;
}

// notify JVMTI before mount
notifyJvmtiMount(/*hide*/true, firstRun);
notifyJvmtiMount(/*hide*/true);

try {
cont.run();
Expand Down Expand Up @@ -300,7 +297,7 @@ private void run(Runnable task) {

// first mount
mount();
notifyJvmtiMount(/*hide*/false, /*first*/true);
notifyJvmtiStart();

// emit JFR event if enabled
if (VirtualThreadStartEvent.isTurnedOn()) {
Expand Down Expand Up @@ -328,7 +325,7 @@ private void run(Runnable task) {

} finally {
// last unmount
notifyJvmtiUnmount(/*hide*/true, /*last*/true);
notifyJvmtiEnd();
unmount();

// final state
Expand Down Expand Up @@ -438,14 +435,14 @@ <V> V executeOnCarrierThread(Callable<V> task) throws Exception {
@ChangesCurrentThread
private boolean yieldContinuation() {
// unmount
notifyJvmtiUnmount(/*hide*/true, /*last*/false);
notifyJvmtiUnmount(/*hide*/true);
unmount();
try {
return Continuation.yield(VTHREAD_SCOPE);
} finally {
// re-mount
mount();
notifyJvmtiMount(/*hide*/false, /*first*/false);
notifyJvmtiMount(/*hide*/false);
}
}

Expand All @@ -462,7 +459,7 @@ private void afterYield() {
setState(PARKED);

// notify JVMTI that unmount has completed, thread is parked
notifyJvmtiUnmount(/*hide*/false, /*last*/false);
notifyJvmtiUnmount(/*hide*/false);

// may have been unparked while parking
if (parkPermit && compareAndSetState(PARKED, RUNNABLE)) {
Expand All @@ -478,7 +475,7 @@ private void afterYield() {
setState(RUNNABLE);

// notify JVMTI that unmount has completed, thread is runnable
notifyJvmtiUnmount(/*hide*/false, /*last*/false);
notifyJvmtiUnmount(/*hide*/false);

// external submit if there are no tasks in the local task queue
if (currentThread() instanceof CarrierThread ct && ct.getQueuedTaskCount() == 0) {
Expand Down Expand Up @@ -508,7 +505,7 @@ private void afterTerminate(boolean notifyContainer, boolean executed) {
assert (state() == TERMINATED) && (carrierThread == null);

if (executed) {
notifyJvmtiUnmount(/*hide*/false, /*last*/true);
notifyJvmtiUnmount(/*hide*/false);
}

// notify anyone waiting for this virtual thread to terminate
Expand Down Expand Up @@ -1086,11 +1083,19 @@ private void setCarrierThread(Thread carrier) {

@IntrinsicCandidate
@JvmtiMountTransition
private native void notifyJvmtiMount(boolean hide, boolean firstMount);
private native void notifyJvmtiStart();

@IntrinsicCandidate
@JvmtiMountTransition
private native void notifyJvmtiUnmount(boolean hide, boolean lastUnmount);
private native void notifyJvmtiEnd();

@IntrinsicCandidate
@JvmtiMountTransition
private native void notifyJvmtiMount(boolean hide);

@IntrinsicCandidate
@JvmtiMountTransition
private native void notifyJvmtiUnmount(boolean hide);

@IntrinsicCandidate
@JvmtiMountTransition
Expand Down
10 changes: 6 additions & 4 deletions src/java.base/share/native/libjava/VirtualThread.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -32,9 +32,11 @@
#define VIRTUAL_THREAD "Ljava/lang/VirtualThread;"

static JNINativeMethod methods[] = {
{ "notifyJvmtiMount", "(ZZ)V", (void *)&JVM_VirtualThreadMount },
{ "notifyJvmtiUnmount", "(ZZ)V", (void *)&JVM_VirtualThreadUnmount },
{ "notifyJvmtiHideFrames", "(Z)V", (void *)&JVM_VirtualThreadHideFrames },
{ "notifyJvmtiStart", "()V", (void *)&JVM_VirtualThreadStart },
{ "notifyJvmtiEnd", "()V", (void *)&JVM_VirtualThreadEnd },
{ "notifyJvmtiMount", "(Z)V", (void *)&JVM_VirtualThreadMount },
{ "notifyJvmtiUnmount", "(Z)V", (void *)&JVM_VirtualThreadUnmount },
{ "notifyJvmtiHideFrames", "(Z)V", (void *)&JVM_VirtualThreadHideFrames },
};

JNIEXPORT void JNICALL
Expand Down

0 comments on commit 0bd6a00

Please sign in to comment.