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

Support JEP491 (Part 1) #20566

Merged
merged 4 commits into from
Nov 14, 2024
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
21 changes: 18 additions & 3 deletions jcl/src/java.base/share/classes/java/lang/Object.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
*/
package java.lang;

/*[IF JAVA_SPEC_VERSION >= 19]*/
/*[IF (JAVA_SPEC_VERSION >= 19) & (JAVA_SPEC_VERSION < 24)]*/
import jdk.internal.misc.Blocker;
/*[ENDIF] JAVA_SPEC_VERSION >= 19 */
/*[ENDIF] (JAVA_SPEC_VERSION >= 19) & (JAVA_SPEC_VERSION < 24) */

/**
* Object is the root of the java class hierarchy. All non-base types
Expand Down Expand Up @@ -283,7 +283,7 @@ public final void wait(long time, int frac) throws InterruptedException {
} finally {
Blocker.end(blockerRC);
}
/*[ELSE] JAVA_SPEC_VERSION < 23 */
/*[ELSEIF JAVA_SPEC_VERSION < 24] */
if (!Thread.currentThread().isVirtual()) {
waitImpl(time, frac);
} else {
Expand All @@ -294,6 +294,21 @@ public final void wait(long time, int frac) throws InterruptedException {
Blocker.end(blocking);
}
}
/*[ELSE] JAVA_SPEC_VERSION < 24 */
if ((time < 0) || (frac < 0)) {
throw new IllegalArgumentException("timeout value is negative");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we may already throw these exceptions internally

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we do, I added this to match RI's bytecode as it would be a faster path to avoid having to call into native.

}
if (!Thread.currentThread().isVirtual()) {
waitImpl(time, frac);
} else {
try {
waitImpl(time, frac);
} catch (InterruptedException e) {
// Clear virtual thread's interrupt status
Thread.currentThread().getAndClearInterrupt();
throw e;
}
}
/*[ENDIF] JAVA_SPEC_VERSION < 19 */
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*[INCLUDE-IF JAVA_SPEC_VERSION >= 21]*/
/*[INCLUDE-IF (JAVA_SPEC_VERSION >= 21) & (JAVA_SPEC_VERSION < 24)]*/
/*
* Copyright IBM Corp. and others 2023
*
Expand Down
2 changes: 2 additions & 0 deletions jcl/src/java.base/share/classes/java/lang/ref/Reference.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ public void runFinalization() {
/*[ENDIF] JAVA_SPEC_VERSION >= 11 */

/*[IF JAVA_SPEC_VERSION >= 19]*/
/*[IF JAVA_SPEC_VERSION < 24] */
public <T> ReferenceQueue<T> newNativeReferenceQueue() {
return new NativeReferenceQueue<>();
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */

public void startThreads() {
throw new UnsupportedOperationException();
Expand Down
18 changes: 18 additions & 0 deletions jcl/src/java.base/share/classes/java/lang/ref/ReferenceQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
import sun.misc.Cleaner;
/*[ENDIF] JAVA_SPEC_VERSION >= 9 */

/*[IF JAVA_SPEC_VERSION >= 24]*/
import jdk.internal.vm.Continuation;

/*[ENDIF] JAVA_SPEC_VERSION >= 24*/

/*[IF CRIU_SUPPORT]*/
import openj9.internal.criu.NotCheckpointSafe;
/*[ENDIF] CRIU_SUPPORT */
Expand Down Expand Up @@ -89,6 +94,14 @@ public Reference<? extends T> poll () {
if(empty) {
return null;
}

/*[IF JAVA_SPEC_VERSION >= 24]*/
boolean isVirtual = false;
if (Thread.currentThread().isVirtual()) {
isVirtual = true;
Continuation.pin();
}
/*[ENDIF] JAVA_SPEC_VERSION >= 24*/
synchronized(this) {
if(empty) {
return null;
Expand All @@ -104,6 +117,11 @@ public Reference<? extends T> poll () {
empty = true;
}
}
/*[IF JAVA_SPEC_VERSION >= 24]*/
if (isVirtual) {
Continuation.unpin();
}
/*[ENDIF] JAVA_SPEC_VERSION >= 24*/
return ref;
}

Expand Down
2 changes: 2 additions & 0 deletions runtime/j9vm/exports.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ if(NOT JAVA_SPEC_VERSION LESS 24)
jvm_add_exports(jvm
JVM_IsContainerized
JVM_IsStaticallyLinked
JVM_VirtualThreadPinnedEvent
JVM_TakeVirtualThreadListToUnblock
)
endif()

Expand Down
4 changes: 4 additions & 0 deletions runtime/j9vm/j9vmnatives.xml
Original file line number Diff line number Diff line change
Expand Up @@ -482,5 +482,9 @@ SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-ex
<!-- Additions for Java 24 (General) -->
<export name="JVM_IsContainerized"/>
<export name="JVM_IsStaticallyLinked"/>

<!-- Additions for Java 24 (VirtualThread JEP491) -->
<export name="JVM_VirtualThreadPinnedEvent"/>
<export name="JVM_TakeVirtualThreadListToUnblock"/>
</exports>
</exportlists>
14 changes: 14 additions & 0 deletions runtime/j9vm/javanextvmi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,20 @@ JVM_IsStaticallyLinked(void)
/* OpenJDK removed static builds using --enable-static-build. */
return JNI_FALSE;
}

JNIEXPORT void JNICALL
JVM_VirtualThreadPinnedEvent(JNIEnv* env, jclass clazz, jstring op)
{
// TODO: emit JFR Event
return;
}

JNIEXPORT jobject JNICALL
JVM_TakeVirtualThreadListToUnblock(JNIEnv* env, jclass ignored)
{
// TODO: return the unblocked list
return NULL;
}
#endif /* JAVA_SPEC_VERSION >= 24 */

} /* extern "C" */
17 changes: 17 additions & 0 deletions runtime/jvmti/jvmtiHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -900,16 +900,33 @@ getVirtualThreadState(J9VMThread *currentThread, jthread thread)
case JVMTI_VTHREAD_STATE_UNPARKED:
case JVMTI_VTHREAD_STATE_YIELDING:
case JVMTI_VTHREAD_STATE_YIELDED:
#if JAVA_SPEC_VERSION >= 24
case JVMTI_VTHREAD_STATE_UNBLOCKED:
case JVMTI_VTHREAD_STATE_WAITING:
case JVMTI_VTHREAD_STATE_TIMED_WAITING:
#endif /* JAVA_SPEC_VERSION >= 24 */
rc = JVMTI_JAVA_LANG_THREAD_STATE_RUNNABLE;
break;
case JVMTI_VTHREAD_STATE_PINNED:
case JVMTI_VTHREAD_STATE_PARKED:
#if JAVA_SPEC_VERSION >= 24
case JVMTI_VTHREAD_STATE_WAIT:
#endif /* JAVA_SPEC_VERSION >= 24 */
rc = JVMTI_JAVA_LANG_THREAD_STATE_WAITING | JVMTI_THREAD_STATE_PARKED;
break;
case JVMTI_VTHREAD_STATE_TIMED_PINNED:
case JVMTI_VTHREAD_STATE_TIMED_PARKED:
#if JAVA_SPEC_VERSION >= 24
case JVMTI_VTHREAD_STATE_TIMED_WAIT:
#endif /* JAVA_SPEC_VERSION >= 24 */
rc = JVMTI_JAVA_LANG_THREAD_STATE_TIMED_WAITING | JVMTI_THREAD_STATE_PARKED;
break;
#if JAVA_SPEC_VERSION >= 24
case JVMTI_VTHREAD_STATE_BLOCKING:
case JVMTI_VTHREAD_STATE_BLOCKED:
rc = JVMTI_JAVA_LANG_THREAD_STATE_BLOCKED;
break;
#endif /* JAVA_SPEC_VERSION >= 24 */
case JVMTI_VTHREAD_STATE_TERMINATED:
rc = JVMTI_JAVA_LANG_THREAD_STATE_TERMINATED;
break;
Expand Down
9 changes: 9 additions & 0 deletions runtime/oti/jvmtiInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,15 @@ typedef struct jvmtiGcp_translation {
#define JVMTI_VTHREAD_STATE_UNPARKED 9
#define JVMTI_VTHREAD_STATE_YIELDING 10
#define JVMTI_VTHREAD_STATE_YIELDED 11
#if JAVA_SPEC_VERSION >= 24
#define JVMTI_VTHREAD_STATE_BLOCKING 12
#define JVMTI_VTHREAD_STATE_BLOCKED 13
#define JVMTI_VTHREAD_STATE_UNBLOCKED 14
#define JVMTI_VTHREAD_STATE_WAITING 15
#define JVMTI_VTHREAD_STATE_WAIT 16
#define JVMTI_VTHREAD_STATE_TIMED_WAITING 17
#define JVMTI_VTHREAD_STATE_TIMED_WAIT 18
#endif /* JAVA_SPEC_VERSION >= 24 */
#define JVMTI_VTHREAD_STATE_TERMINATED 99
#define JVMTI_VTHREAD_STATE_SUSPENDED (1 << 8)
#endif /* JAVA_SPEC_VERSION >= 19 */
Expand Down
4 changes: 4 additions & 0 deletions runtime/redirector/forwarders.m4
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,7 @@ _IF([JAVA_SPEC_VERSION >= 24],
[_X(JVM_IsContainerized, JNICALL, false, jboolean, void)])
_IF([JAVA_SPEC_VERSION >= 24],
[_X(JVM_IsStaticallyLinked, JNICALL, false, jboolean, void)])
_IF([JAVA_SPEC_VERSION >= 24],
[_X(JVM_VirtualThreadPinnedEvent, JNICALL, false, void, JNIEnv* env, jclass clazz, jstring op)])
_IF([JAVA_SPEC_VERSION >= 24],
[_X(JVM_TakeVirtualThreadListToUnblock, JNICALL, false, jobject, JNIEnv* env, jclass ignored)])